QOJ.ac

QOJ

実行時間制限: 8 s メモリ制限: 1024 MB 満点: 100

#15340. Double-sided Chessboard

統計

Jiajia has an $n \times n$ black and white chessboard. Each cell has two sides, one white and one black. Jiajia places the chessboard flat on a table, so exactly one side of each cell is facing up, as shown in the figure below:

We number the rows from top to bottom as $1, 2, 3, \dots, n$, and the columns from left to right as $1, 2, 3, \dots, n$. Each cell can be represented by its coordinates $(x, y)$. In the figure above, there are 8 cells with the black side facing up, and 17 cells with the white side facing up.

If two cells of the same color share a common edge, we say these two cells belong to the same connected component. The figure above has 5 black connected components and 3 white connected components.

Jiajia can flip one cell every minute (i.e., white becomes black, and black becomes white), and then calculate the current number of black connected components and white connected components. Can you calculate this faster?

Input

The first line of the input contains a positive integer $n$, the side length of the grid. The following $n$ lines each contain $n$ integers, either 0 or 1, representing the initial state. 0 represents white, and 1 represents black. The next line contains an integer $m$, the number of operations. The following $m$ lines each contain two integers $x, y$ ($1 \le x, y \le n$), representing flipping the cell at coordinates $(x, y)$.

Output

The output contains $m$ lines, each corresponding to an operation. Each line includes two integers $b, w$, representing the number of black regions and white regions, respectively.

Constraints

  • $1 \le n \le 200$
  • $1 \le m \le 10,000$

Examples

Input 1

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

Output 1

4 3
5 2

Note

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

There are 4 black regions and 3 white regions

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

There are 5 black regions and 2 white regions

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.