Under construction

Still being built. Things may change

King of Data Structures

Time limit1sMemory limit1024 MB

Summary
Robots mow a grid by moving in one direction and removing grass as they go, and queries ask whether a cell has grass or how many cells still do.
Level

Medium6 of 10

Topics
Union-find, Simulation, Array
Solved
No attempts yet

Problem

Heuijiro saw thick grass growing across the lawn near his house one day, so he decided to test the performance of a new lawn mower robot. The lawn is a two-dimensional grid with nn rows and mm columns. The cell in row rr and column cc is written (r,c)(r,c). At the start, every cell of the lawn has grass.

When you enter four integers dyd_y, dxd_x, yy, xx into the robot, it starts at (y,x)(y,x) and is set to move in the direction ⟨dy,dx⟩\langle d_y,d_x \rangle. It is always true that ∣dy∣+∣dx∣=1|d_y|+|d_x|=1. In other words, the robot always moves in one fixed direction and only to an adjacent cell up, down, left, or right. The robot works as follows.

  1. If there is no grass at (y,x)(y,x), stop mowing.
  2. Remove the grass at (y,x)(y,x).
  3. If at least one of the conditions y+dy<1y+d_y<1, y+dy>ny+d_y>n, x+dx<1x+d_x<1, or x+dx>mx+d_x>m is true, stop mowing.
  4. Change (y,x)(y, x) to (y+dy,x+dx)(y+d_y, x+d_x), then return to step 1.

As part of the performance test, Heuijiro must answer QQ queries of the following kinds.

  • 1 dy dx y x1 \ d_y \ d_x \ y \ x: Start a robot set to direction ⟨dy,dx⟩\langle d_y,d_x \rangle at (y,x)(y,x), then wait until that robot finishes mowing.
  • 2 y x2 \ y \ x: After all mowing jobs started before this query have finished, in order, print the state of (y,x)(y,x). The state is 00 if there is grass at (y,x)(y,x), and 11 if there is no grass.
  • 33: Print the number of cells that still have grass on the lawn.

If you cannot answer every query exactly, you cannot verify that the robot works correctly. Write a program that answers the given QQ queries exactly.

Input

The first line contains the number of rows nn, the number of columns mm, and the number of queries QQ, separated by spaces. (1≤n,m≤10001 \le n,m \le 1000, 1≤Q≤2×1051 \le Q \le 2\times 10^5)

From the second line onward, each of the QQ lines contains one query. Every query is one of the kinds described in the statement.

For every query, 1≤y≤n1 \le y \le n and 1≤x≤m1 \le x \le m. For every query of type 11, ∣dy∣+∣dx∣=1|d_y|+|d_x|=1.

Output

For every query of type 22 and type 33, print the answer on its own line.

Examples1

  1. Example 1

    Input
    3 3 8
    3
    2 2 2
    1 1 0 1 2
    1 0 -1 3 3
    2 2 2
    2 3 3
    2 3 1
    3
    
    Expected output
    9
    0
    1
    1
    0
    5