#P4121. [WC2005] 双面棋盘
[WC2005] 双面棋盘
Description
Jiajia has an black-and-white board. Each cell has two faces: one white and one black. Jiajia lays the board flat on the table, so exactly one face of each cell is up, as shown in the figure below:

We number the rows from top to bottom as , and the columns from left to right as . Thus, each cell can be represented by board coordinates . In the figure above, there are cells with the black side up, and the other cells have the white side up.
If two cells of the same color share a common edge, we say these two same-colored cells belong to the same connected component. In the figure above, there are black connected components and white connected components.
Jiajia can flip one cell per minute (i.e., white becomes black, and black becomes white), then count how many black connected components and white connected components there are. Can you compute it faster?
Input Format
The first line contains a positive integer , the side length of the board.
Each of the next lines contains integers, each either or , representing the initial state. means white, and means black.
The next line contains an integer , the number of operations.
Each of the next lines contains two integers , (), indicating to flip the cell at coordinates .
Output Format
Output contains lines, one for each operation. Each line contains two integers , , denoting the numbers of black connected components and white connected components.
5
0 1 0 0 0
0 1 1 1 0
1 0 0 0 1
0 0 1 0 0
1 0 0 0 0
2
3 2
2 3
4 3
5 2
Hint
【Sample Explanation】
After flipping , the board becomes:

There are black connected components and white connected components.
After flipping , the board becomes:

There are black connected components and white connected components.
【Constraints】
For of the testdata, , .
Translated by ChatGPT 5
京公网安备 11011102002149号