Under construction

Still being built. Things may change

Kunai

Time limit3sMemory limit256 MB

Summary
Ninjas on a huge grid throw kunai in four directions; kunai vanish when two arrive at the same point at the same instant, so count the squares any surviving kunai passes through.
Level

Hard8 of 10

Topics
Geometry, Hash map, Sorting, Implementation
Solved
No attempts yet

Problem

A kunai (a throwing blade) is a finely made, dagger-like weapon used by ninjas, who attack by throwing it at their enemies.

There is a grid of HH rows and WW columns of unit squares, holding NN ninjas. Every ninja stands at the center of a square, and each square holds at most one ninja. Each ninja carries one kunai and faces one of four directions: right, up, left, or down. At time 00, every ninja throws its kunai in the direction it is facing.

Every kunai flies in a straight line at a speed of one square per unit of time. If more than one kunai arrives at the same point at the same instant, those kunai collide and disappear. A kunai is small enough that its size can be ignored. The ninjas move so fast that they are never hit by a kunai. Unless it collides with another kunai, a kunai keeps moving in the same direction at the same speed and never slows down. Note that a collision may happen partway between two squares, not only at a square's center.

After enough time has passed, count how many of the W×HW \times H squares have been passed through by at least one kunai.

Input

The first line contains two integers WW and HH, separated by a single space, giving the size of the grid (1≤W≤1091 \le W \le 10^9, 1≤H≤1091 \le H \le 10^9).

The second line contains one integer NN, the number of ninjas (1≤N≤1051 \le N \le 10^5).

Each of the next NN lines contains three integers XiX_i, YiY_i, DiD_i describing ninja ii, who stands in the XiX_i-th column from the left and the YiY_i-th row from the top (1≤Xi≤W1 \le X_i \le W, 1≤Yi≤H1 \le Y_i \le H). No two ninjas occupy the same square. The direction ninja ii faces is given by DiD_i:

  • Di=0D_i = 0: ninja ii faces right.
  • Di=1D_i = 1: ninja ii faces up.
  • Di=2D_i = 2: ninja ii faces left.
  • Di=3D_i = 3: ninja ii faces down.

Output

Print the number of squares of the W×HW \times H grid that have been passed through by a kunai after enough time has passed.

Hint

Take the sample input. Let "kunai ii" be the kunai thrown by ninja ii. At time 0.50.5, kunai 22 and kunai 33 meet between their squares and disappear. At time 22, kunai 11 and kunai 55 collide and disappear. After time 22 there are no further collisions. In the end, exactly 1111 squares have been passed through by a kunai, so the answer is 1111.

Examples1

  1. Example 1

    Input
    5 4
    5
    3 3 2
    3 2 0
    4 2 2
    5 4 1
    1 1 3
    
    Expected output
    11