#P5877. 棋盘游戏

棋盘游戏

Description

To improve kindergarten kids’ counting skills, Teacher Xiaohu gave a family game homework. He asked Xiaohu to take an empty Go board and randomly place some pieces (in two colors: black and white) on some cells. If a cell and one of its four neighbors (up, down, left, right) both contain pieces of the same color, then the two cells are considered connected. During this process, Xiaohu must keep counting how many connected components there are in total.

The figure below shows a 5×95\times 9 board, where . means an empty cell, * means a black piece, and @ means a white piece.

Then there are 44 connected components.

.  .  .  .  .  .  .  .  .
.  .  *  *  .  .  @  @  .
.  *  *  @  @  .  @  @  .
.  .  *  @  .  .  *  .  .
.  .  .  .  .  .  .  .  .

Big brother Dahu watched and thought: if the board is N×NN\times N and a total of MM pieces are placed, how can we solve this problem using a computer?

Input Format

The first line contains two integers: N,MN, M.

The next MM lines each contain three integers: c,x,yc, x, y. They represent, in order, the color of the piece to be placed (00 means white, 11 means black), the row coordinate, and the column coordinate of the cell where it is placed.

Output Format

Output MM lines in total. The ii-th line contains one integer, which is the number of connected components of pieces after placing the ii-th piece.

3 5    
1 1 1  
1 1 2  
0 2 2  
1 3 1  
1 2 1  

1 
1 
2 
3 
2

3 5
1 1 2
1 2 1
1 3 2
1 2 3
1 2 2

1
2
3
4
1

Hint

For 30%30\% of the testdata: 1N101\le N \le 10.

For 60%60\% of the testdata: 1N1001\le N\le 100.

For 100%100\% of the testdata: 1N5001\le N\le 500, 1MN×N1\le M \le N \times N, 0c1 0 \le c \le 1, 1x,yN 1\le x, y \le N.

Translated by ChatGPT 5