Square Overlap
Time limit1sMemory limit128 MB
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 square fenced-in pastures, each of size exactly . Pasture is centered at the integer-coordinate point . Because every pasture is an axis-aligned square, pasture covers the region .
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 if no two pastures overlap, and print if more than one pair of pastures overlaps.
Input
- The first line contains two space-separated integers and (, ). is guaranteed to be even.
- Each of the next lines contains two integers and (), the center of pasture .
Output
- Print a single line containing the area shared by the two overlapping pastures. Print if no two pastures overlap, and print if more than one pair of pastures overlaps.
Notes
Two pastures and overlap with positive area if and only if and . When they do, their shared area equals .