Block Breaker
Time limit2sMemory limit512 MB
Blocks drop in a grid when a knocked block has a dropped left/right neighbor and a dropped front/back neighbor; after each of q moves, report how many blocks fall.
- Level
Hard8 of 10
- Topics
- Union-find, Simulation, Matrix, Implementation
- Solved
- No attempts yet
Problem
Consider a rectangular frame of size hanging horizontally in the air. Initially, the frame is filled tightly with square blocks of size . Because of the friction with the frame and with each other, the blocks are stable and will not drop.
However, the blocks can be knocked down. When a block is knocked down, other remaining blocks may also drop, since the friction from the remaining blocks may no longer hold them. Formally, a block drops if it is knocked or unstable. A block is unstable when at least one of its left and right neighbors has dropped and at least one of its front and back neighbors has also dropped. In this definition, the frame can be regarded as a huge block that is always stable.
Now you, the block breaker, want to knock down blocks. Formally, you will make moves. On the -th move, you choose position . If a block is still at the chosen position, you knock it down; otherwise, nothing happens. After each move, you wait until no unstable blocks are going to drop before making the next move.
For example, look at the following illustration. The frame has size , and blocks and have already dropped. If we knock down block , it drops, and then the last remaining block also drops because it becomes unstable.

You are given a sequence of moves to make. For each move, find how many blocks drop as a result. In particular, if nothing happens during a move, the answer for that move is 0.
Input
The first line contains one positive integer , the number of test cases. () For each test case:
The first line contains three positive integers , , and , the dimensions of the frame and the number of moves. (, )
Each of the following lines contains two positive integers and , describing the next move to make. (, )
Output
For each test case, output lines. Each line must contain a non-negative integer: the number of blocks that drop as a result of the corresponding move.