#P2202. Pasture Overlap
Pasture Overlap
Pasture Overlap
Farmer John is planning to build N fenced-in square pastures on his farm, each of size \(K \times K\). Each pasture is centered at a distinct point \((x_i, y_i)\), with the coordinates given as integers. In his haste, he might have accidentally placed the squares so that two or more of them overlap. Two squares overlap if they share a positive area in common.
The boundary of the i-th square is defined by \[ x_i - \frac{K}{2} \le x \le x_i + \frac{K}{2}, \quad y_i - \frac{K}{2} \le y \le y_i + \frac{K}{2} \] and the overlapping area between two squares with centers \((x_i, y_i)\) and \((x_j, y_j)\) is given by \[ \text{Area} = \max(0, K - |x_i - x_j|) \times \max(0, K - |y_i - y_j|). \]
Your task is to examine the given positions of all the squares. If no two squares overlap, output 0; if exactly one pair of squares overlaps, output the overlapping area; and if more than one pair overlaps, output -1.
inputFormat
The first line of input contains two integers N and K, where N (2 \(\le\) N \(\le\) 50,000) is the number of pastures and K (1 \(\le\) K \(\le\) 1,000,000) is the side length of each square.
Each of the next N lines contains two integers x and y (\(-1,000,000 \le x, y \le 1,000,000\)), representing the center coordinates of a pasture. It is guaranteed that no two pastures share the same center.
outputFormat
Output a single integer: the overlapping area if exactly one overlapping pair exists; output 0 if no squares overlap; and output -1 if more than one pair overlaps.
sample
3 5
0 0
10 10
-10 -10
0
</p>