#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 colored matrix.
A connected component is defined by the familiar 4-connected grid graph: starting from a grid cell , each step moves to a same-colored grid cell whose Manhattan distance is at most . If you can reach another grid cell , then and are in the same connected component.
There are operations.
- Point update: modify the color of cell .
- Range query: given , ask: if we only keep the submatrix formed by rows and columns , 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 .
The next lines each contain lowercase letters, representing the colored matrix. The number of colors in the matrix does not exceed , corresponding to the lowercase letters.
The next line contains a positive integer , the number of operations.
The next lines each start with a positive integer , indicating the operation type.
- Update operation: . The same line then contains two positive integers and a character , meaning to change the character at to .
- Query operation: . The same line then contains four positive integers , with meanings as above.
Output Format
Output lines, each containing one positive integer, the answer. It is guaranteed that type 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 points.
For of the testdata:
.
.
It is guaranteed that the numbers of type and type 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|||
|:-:|:-:|:-:|
||||
||||
||||
||||
||||
||||
||||
||||
||||
||||
For of the testdata, it is guaranteed that KMN is a biology competition master, so even for a Petri dish she can observe every grid cell and its changes accurately.
This problem uses Special Judge. When a test point has type II operations, then for each type II operation: if your answer matches the standard answer, you get 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