#P16011. [CCO 2016 Day 2] Zombie Apocalypse

[CCO 2016 Day 2] Zombie Apocalypse

题目描述

Your country has a problem with zombies. That is, it has zombies, which are a problem. Thankfully, you are gainfully employed at the Forensic Institute for Zoology and Zombie Emerging Studies (FIZZES), and your job is simply to give a measure of how bad the problem is.

You have mapped out your country on an NN-by-MM array of cells marked with non-negative integers.

You have the exact locations of all the zombies, and know that no two zombies are in the same location. The cells containing a zombie are marked with 00. Next, all the unmarked cells touching a cell (where touching a cell means touching on any side or corner of a cell; so each cell touches up to 88 other cells) marked with 00 are marked with 11. Then, all the unmarked cells touching a cell marked with 11 are marked with 22. This process continues until all the cells are marked. These numbers indicate the level of concern your office has about the spread of zombies.

A small example is shown below.

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 has given you an integer QQ, and you must determine the number of cells which are marked with the integer QQ.

输入格式

The first line of input will contain two space-separated integers NN and M (1N109;1M109)M\ (1 \le N \le 10^9; 1 \le M \le 10^9) indicating the size of the grid. The next line will contain the number K (1K2000)K\ (1 \le K \le 2000), indicating the number of cells that contain zombies. The next KK lines each contain two space separated integers ri cir_i\ c_i indicating the row and column of the ith zombie (1riN;1ciM)(1 \le r_i \le N; 1 \le c_i \le M). No two zombies are in the same cell: thus if iji \ne j then (ri,ci)(rj,cj)(r_i, c_i) \ne (r_j, c_j). The last line will contain the integer Q (0QN+M)Q\ (0 \le Q \le N + M).

For 55 of the 2525 marks available, N1000N \le 1000 and M1000M \le 1000.

For an additional 55 of the 2525 marks available, K50K \le 50.

For an additional 55 of the 2525 marks available, N1000N \le 1000.

输出格式

Output the number of cells in the grid that are marked with the integer QQ.

5 6
2
3 3
2 4
2
15

提示

The sample input is the example shown above, which has 1515 22's.