Given up to 2000 zombie cells on an N by M grid with Chebyshev distance spreading, count how many cells end up at level Q.
Hard8GeometrySortingImplementationMathNo attempts yetTime limit2sMemory limit512 MBYour country has zombies, and that is the problem. You work at the Forensic Institute for Zoology and Zombie Emerging Studies (FIZZES), and your job is to report how bad the situation is.
The country is mapped onto an N×M grid of cells, and every cell gets one non-negative integer. You know the exact location of every zombie, and no two zombies share a cell. The numbers are written in this order.
Two cells touch when they share a side or a corner, so one cell touches up to eight cells. The number in a cell is the level of concern the institute has about zombie spread at that spot.
For N=5, M=6 with zombies in row 2 column 4 and row 3 column 3, the numbers come out like this.
2 2 1 1 1 2
2 1 1 0 1 2
2 1 0 1 1 2
2 1 1 1 2 2
2 2 2 2 2 3
Your boss gives you an integer Q. Determine how many cells are marked with Q.
The first line contains two space-separated integers N and M (1≤N≤109, 1≤M≤109), the number of rows and the number of columns of the grid.
The second line contains K (1≤K≤2000), the number of zombies.
Each of the next K lines contains two space-separated integers ri and ci (1≤ri≤N, 1≤ci≤M), the row and the column of the i-th zombie. No two zombies share a cell, so (ri,ci)=(rj,cj) whenever i=j.
The last line contains the integer Q (0≤Q≤N+M).
Print the number of cells in the grid that are marked with Q.