Neighbors
Consider a rectangle on the Cartesian plane with vertices at ((0, 0)) and ((w, h)), where (w) and (h) are positive integers. Within this rectangle, there are (N) mountain peaks located at grid points, which are points with integer coordinates. Each person wants to build a house, but the following rules apply:
- Only one house can be built at each grid point. - Houses cannot be built on mountain peaks.
Thus, there are ((w+1) (h+1) - N) possible locations for houses. Some locations are more desirable than others. A grid point ((x, y)) is said to have a northern neighbor if there is a peak at ((x, y+d)), where (d) is a positive integer. Similarly, southern, eastern, and western neighbors are defined. Therefore, each grid point that is not a peak can have between 0 and 4 neighbors. The more neighbors a location has, the more desirable it is. Your task is to count how many grid points (excluding peaks) have exactly 0, 1, 2, 3, or 4 neighbors.
Write a program that:
- Reads the terrain description from standard input. - Calculates the number of grid points with exactly 0, 1, 2, 3, and 4 neighbors. - Outputs the result to standard output.
Input
The first line contains three integers (w), (h), (N) ((1 w, h 10^9)), ((1 N 500000)), separated by spaces. The next (N) lines each contain two integers (x) and (y) ((0 x w)), ((0 y h)), separated by a space, representing the locations of the peaks. All peaks are at distinct points.
Output
The output should be a single line containing 5 integers, separated by spaces, representing the number of grid points (excluding peaks) that have exactly 0, 1, 2, 3, and 4 neighbors, respectively.