#P12049. KMN の培养皿

KMN の培养皿

Background


Please read the Hint section carefully.
KMN is a top biology competition contestant. She has a Petri dish with many cells inside. Each cell occupies some grid cells. Each grid cell has a letter indicating which cell occupies it. The same cell is represented by the same letter. Different adjacent cells are represented by different letters.

Each time, KMN asks you about a rectangle. If we cut the Petri dish with this rectangle, how many cells will be contained in the cut-out part (inside the rectangle)? If one cell is cut into multiple pieces, they are counted as multiple cells.
However, these cells may also split or merge, so you also need to maintain update operations.

Problem Description

There is an n×nn \times n colored matrix.

A connected component is defined by the familiar 4-connected grid graph: starting from a grid cell AA, each step moves to a same-colored grid cell whose Manhattan distance is at most 11. If you can reach another grid cell BB, then AA and BB are in the same connected component.

There are qq operations.

  • Point update: modify the color of cell (x,y)(x,y).
  • Range query: given (l,r,u,d)(l,r,u,d), ask: if we only keep the submatrix formed by rows [u,d][u,d] and columns [l,r][l,r], how many connected components are there? Note: when determining whether two cells are in the same connected component using the definition above, you are not allowed to move outside the queried region.

Each query is independent.

Input Format

The first line contains a positive integer nn.
The next nn lines each contain nn lowercase letters, representing the colored matrix. The number of colors in the matrix does not exceed 2626, corresponding to the 2626 lowercase letters.

The next line contains a positive integer qq, the number of operations.
The next qq lines each start with a positive integer opop, indicating the operation type.

  • Update operation: op=1op=1. The same line then contains two positive integers x,yx,y and a character cc, meaning to change the character at (x,y)(x,y) to cc.
  • Query operation: op=2op=2. The same line then contains four positive integers u,l,d,ru, l, d, r, with meanings as above.

Output Format

Output q÷2q \div 2 lines, each containing one positive integer, the answer. It is guaranteed that type 22 operations make up exactly half of all operations.

1
a
1
2 1 1 1 1
1
4
aabb
aaaa
aaaa
bbaa
3
2 1 1 4 4
2 2 2 3 3
2 1 1 2 4
3
1
2
5
aaaaa
ababa
aabaa
ababa
aaaaa
9
2 1 1 5 5
1 3 3 a
2 1 1 5 5
1 2 3 b
1 4 3 b
1 3 2 b
1 3 4 b
2 1 1 5 5
2 3 1 3 5
6
5
3
5

Hint

The full score of this problem is 3×1053 \times 10^5 points.
For 100%100\% of the testdata:
1l,r,u,dn5001 \le l,r,u,d \le n \le 500.
1q50001 \le q \le 5000.

It is guaranteed that the numbers of type 11 and type 22 operations are equal.
The SubTasks of this problem are only used to group testdata of similar sizes. There is no bundling relationship.
Test point information:
|SubTask ID|n=n=|q=q=| |:-:|:-:|:-:| |11|5050|10001000| |22|5050|50005000| |33|100100|10001000| |44|100100|50005000| |55|300300|10001000| |66|300300|30003000| |77|300300|50005000| |88|500500|10001000| |99|500500|30003000| |1010|500500|50005000|

For 100%100\% of the testdata, it is guaranteed that KMN is a biology competition master, so even for a 500cm×500cm500\operatorname{cm} \times 500\operatorname{cm} Petri dish she can observe every grid cell and its changes accurately.
This problem uses Special Judge. When a test point has xx type II operations, then for each type II operation: if your answer matches the standard answer, you get 10000x\frac{10000}{x} points.
Therefore, if your program cannot solve a certain query, please output a single integer on one line (it must be within the int range), to ensure that the later queries can still be judged and scored normally.

Translated by ChatGPT 5