#P4121. [WC2005] 双面棋盘

    ID: 3055 远端评测题 700ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2005线段树并查集WC/CTSC/集训队生成树Link-Cut Tree,LCT

[WC2005] 双面棋盘

Description

Jiajia has an n×nn \times n 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 1,2,3,,n1, 2, 3, \ldots, n, and the columns from left to right as 1,2,3,,n1, 2, 3, \ldots, n. Thus, each cell can be represented by board coordinates (x,y)(x, y). In the figure above, there are 88 cells with the black side up, and the other 1717 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 55 black connected components and 33 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 nn, the side length of the board.
Each of the next nn lines contains nn integers, each either 00 or 11, representing the initial state. 00 means white, and 11 means black.

The next line contains an integer mm, the number of operations.
Each of the next mm lines contains two integers xx, yy (1x,yn1 \le x, y \le n), indicating to flip the cell at coordinates (x,y)(x, y).

Output Format

Output contains mm lines, one for each operation. Each line contains two integers bb, ww, 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 (3,2)(3, 2), the board becomes:

There are 44 black connected components and 33 white connected components.

After flipping (2,3)(2, 3), the board becomes:

There are 55 black connected components and 22 white connected components.

【Constraints】
For 100%100\% of the testdata, 1n2001 \le n \le 200, 1m1041 \le m \le 10^4.

Translated by ChatGPT 5