Under construction

Still being built. Things may change

Boardle

Interview

Time limit1sMemory limit256 MB

Summary
Given Q directional answers about a hidden treasure on an N by M grid, count the cells where the treasure can still be.
Level

Easy3 of 10

Topics
Intervals, Implementation
Solved
No attempts yet

Problem

The Boardle game is played on an N×MN \times M grid. The two opposite corners of the grid are (1,1)(1, 1) and (N,M)(N, M). The treasure is hidden in one of the NMNM cells, and you win the game by finding it.

To find the treasure, you asked QQ questions of the following form.

  • Relative to the point (x,y)(x, y), in which direction is the treasure?

The answer is one of east, west, south, north, northeast, northwest, southeast, southwest, or same point. East is +x+x, west is −x-x, north is +y+y, and south is −y-y. Use the answers to the QQ questions to count the cells where the treasure could still be.

Input

The first line contains two integers NN and MM, the size of the grid.

The second line contains the number of questions QQ.

From the third line to line Q+2Q+2, line i+2i+2 gives the information for the ii-th question in the form xix_i yiy_i did_i. xix_i and yiy_i are the position of the point asked about in the ii-th question. did_i is an integer from 1 to 9 and corresponds to each direction as shown in the table below.

did_i123456789
DirectionEastWestSouthNorthNortheastNorthwestSoutheastSouthwestSame point

If 1≤di≤41 \le d_i \le 4, the treasure is in exactly that direction. For example, if di=4d_i = 4, the treasure is directly north of the chosen point, not northwest or northeast of it.

+645
y291
-837
-x+

Output

Print the number of cells where the treasure can be. It is guaranteed that at least one candidate cell exists.

Constraints

  • 1≤N,M≤1091 \le N, M \le 10^9
  • 1≤Q≤1051 \le Q \le 10^5
  • 1≤xi≤N1 \le x_i \le N (1≤i≤Q1 \le i \le Q)
  • 1≤yi≤M1 \le y_i \le M (1≤i≤Q1 \le i \le Q)
  • 1≤di≤91 \le d_i \le 9 (1≤i≤Q1 \le i \le Q)

Examples2

  1. Example 1

    Input
    3 3
    1
    1 1 5
    
    Expected output
    4
    
  2. Example 2

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