This page is still under construction.

Parts of this page are still being built. What you see may change.

Square Overlap

Time limit1sMemory limit128 MB

Summary
Given N centers of equal K by K squares, find the single overlapping pair and print its shared area, 0 if none overlap, or -1 if two or more pairs overlap.
Level

Medium7 of 10

Topics
Sorting, Sliding window, Geometry, Binary search
Solved
No attempts yet

Problem

You are planning to build NN square fenced-in pastures, each of size exactly K×KK \times K. Pasture ii is centered at the integer-coordinate point (xi,yi)(x_i, y_i). Because every pasture is an axis-aligned square, pasture ii covers the region [xi−K/2, xi+K/2]×[yi−K/2, yi+K/2][x_i - K/2,\ x_i + K/2] \times [y_i - K/2,\ y_i + K/2].

While drafting the plans, you worry that two pastures may accidentally overlap, meaning the two squares share a region of positive area (merely touching along an edge or at a corner does not count). No two pastures share the same center point.

Given the center of every planned pasture, compute the area shared by the two overlapping pastures. Print 00 if no two pastures overlap, and print −1-1 if more than one pair of pastures overlaps.

Input

  • The first line contains two space-separated integers NN and KK (2≤N≤50,0002 \le N \le 50{,}000, 1≤K≤1,000,0001 \le K \le 1{,}000{,}000). KK is guaranteed to be even.
  • Each of the next NN lines contains two integers xix_i and yiy_i (−1,000,000≤xi,yi≤1,000,000-1{,}000{,}000 \le x_i, y_i \le 1{,}000{,}000), the center of pasture ii.

Output

  • Print a single line containing the area shared by the two overlapping pastures. Print 00 if no two pastures overlap, and print −1-1 if more than one pair of pastures overlaps.

Notes

Two pastures ii and jj overlap with positive area if and only if ∣xi−xj∣<K|x_i - x_j| < K and ∣yi−yj∣<K|y_i - y_j| < K. When they do, their shared area equals (K−∣xi−xj∣)×(K−∣yi−yj∣)(K - |x_i - x_j|) \times (K - |y_i - y_j|).

Examples3

  1. Example 1

    Input
    4 6
    0 0
    8 4
    -2 1
    0 7
    
    Expected output
    20
    
  2. Example 2

    Input
    2 4
    0 0
    1 1
    
    Expected output
    9
    
  3. Example 3

    Input
    3 4
    0 0
    1 0
    2 0
    
    Expected output
    -1