#P3635. [APIO2012] 苦无

[APIO2012] 苦无

Description

Kunai is a knife-shaped weapon used by ninjas, who attack by throwing it. There are NN ninjas gathered on a chessboard-like plaza with HH rows and WW columns. Each ninja stands at the center of a cell, and no two ninjas share the same cell. Each ninja holds one kunai and faces one of the four directions: up, down, left, or right. At time 00, all ninjas simultaneously throw their kunai in the direction they are facing.

Each kunai keeps its initial direction and flies at unit speed. If, at some moment, more than one kunai is at the same position, they collide and disappear. Kunai are very small and can be treated as point masses. Also, because ninjas move extremely fast, they will not be hit by any kunai.

In the following example, we use arrows to represent kunai, and the direction of an arrow is the direction of the corresponding kunai. In these figures, all kunai collide and disappear.

In the figures below, the two bold arrows represent kunai that will not collide. In the second and third figures, one of the bold-arrow kunai collides with a thin-arrow kunai and disappears, so it will not collide with the other bold-arrow kunai.

Your task is to compute, after sufficient time has passed, how many cells in this W×HW \times H plaza are traversed by at least one kunai.

Input Format

The first line contains two integers WW, HH separated by a space, indicating the plaza has WW columns and HH rows. The second line contains an integer NN, the number of ninjas.

The next NN lines each contain three integers XiX_i, YiY_i, DiD_i separated by spaces. The ii-th ninja is at column XiX_i (counted from left to right) and row YiY_i (counted from top to bottom). No two ninjas share the same position. The facing direction of the ii-th ninja is given by DiD_i:

  • Di=0D_i = 0: facing right;
  • Di=1D_i = 1: facing up;
  • Di=2D_i = 2: facing left;
  • Di=3D_i = 3: facing down.

Output Format

Output one integer: after sufficient time has passed, the number of cells in the W×HW \times H plaza that are traversed by at least one kunai.

5 4 
5 
3 3 2 
3 2 0 
4 2 2 
5 4 1 
1 1 3 
11

Hint

For all testdata, 1N1051 \le N \le 10^5, 1W1091 \le W \le 10^9, 1H1091 \le H \le 10^9; coordinate ranges are 1XiW1 \le X_i \le W, 1YiH1 \le Y_i \le H.

  • In 10%10\% of the testdata, N1000N \le 1000, W1000W \le 1000, H1000H \le 1000.
  • In 40%40\% of the testdata, N1000N \le 1000.

Translated by ChatGPT 5