King of Data Structures
Time limit1sMemory limit1024 MB
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 rows and columns. The cell in row and column is written . At the start, every cell of the lawn has grass.
When you enter four integers , , , into the robot, it starts at and is set to move in the direction . It is always true that . 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.
- If there is no grass at , stop mowing.
- Remove the grass at .
- If at least one of the conditions , , , or is true, stop mowing.
- Change to , then return to step 1.
As part of the performance test, Heuijiro must answer queries of the following kinds.
- : Start a robot set to direction at , then wait until that robot finishes mowing.
- : After all mowing jobs started before this query have finished, in order, print the state of . The state is if there is grass at , and if there is no grass.
- : 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 queries exactly.
Input
The first line contains the number of rows , the number of columns , and the number of queries , separated by spaces. (, )
From the second line onward, each of the lines contains one query. Every query is one of the kinds described in the statement.
For every query, and . For every query of type , .
Output
For every query of type and type , print the answer on its own line.