luogu#P7963. [NOIP2021] 棋局

    ID: 7305 远端评测题 6000ms 1024MiB 尝试: 0 已通过: 0 难度: 9 上传者: 标签>并查集2021NOIP 提高组O2优化线段树合并

[NOIP2021] 棋局

Background

After losing at mahjong for a whole night, Xiao z and Xiao c uninstalled all card games on their phones. But how could that stop their determination to slack off in class? Now they set their eyes on board games, yet aside from playing Aeroplane Chess every day, they only know the rough rules of almost all other board games.

“Since we both can play but only a little, why don’t we make our own mash-up?”

So, with their wild imagination, a wonderful game that mixes Go, Chinese Chess, and Junqi (junqi, i.e., Chinese Army Chess) was born.

Problem Description

The game is played on a grid-shaped board with nn rows and mm columns. Pieces are placed on the intersections of the grid lines. We denote the top-left intersection as (1,1)(1,1) and the bottom-right intersection as (n,m)(n,m).

Pieces come in two colors, black and white, and each player uses one color.

Besides color, each piece also has a level. Let coli\mathit{col}_i be the color of piece ii, and lvi\mathit{lv}_i be the level of piece ii. In addition, each grid edge on the board has one of 44 states. For the ii-th grid edge, let its state be opti\mathit{opt}_i.

On a player’s turn, they may choose one of their own pieces and move it along grid edges to another intersection; this is called a move. Formally, a move is defined as choosing a coordinate sequence (x0,y0),(x1,y1),,(xk,yk)(x_0,y_0),(x_1,y_1),\ldots,(x_k,y_k), where kk is any chosen positive integer, (x0,y0)(x_0,y_0) is the piece’s starting position, and (xk,yk)(x_k,y_k) is the final position. It must satisfy:

  • For any i=0,1,,k1i=0,1,\ldots,k-1, the intersections (xi,yi)(x_i,y_i) and (xi+1,yi+1)(x_{i+1},y_{i+1}) must be directly connected by a grid edge; that is, the move must follow grid edges.
  • For any iji\not=j, we must have (xi,yi)(xj,yj)(x_i,y_i)\ne(x_j,y_j); that is, no position may be visited twice during the move. In particular, (x0,y0)(xk,yk)(x_0,y_0)\ne(x_k,y_k), meaning the piece cannot stay in place (or return to its starting point).
  • For any i=1,,k1i=1,\ldots,k-1, there must be no piece on (xi,yi)(x_i,y_i); that is, the piece cannot pass through existing pieces.
  • If there is no piece on (xk,yk)(x_k,y_k), it is a normal move; otherwise it is a capture. During a capture, let the moving piece have color col1\mathit{col}_1 and level lv1\mathit{lv}_1, and the captured piece have color col2\mathit{col}_2 and level lv2\mathit{lv}_2. Then it must hold that $\mathit{col}_1\ne\mathit{col}_2,\mathit{lv}_1\geq\mathit{lv}_2$. In other words, you may only capture a piece of a different color whose level is not higher than yours.

Note that from the definition above, a piece is not allowed to keep moving forward after capturing.

The meanings of grid edge states are as follows:

  • If opti=0\mathit{opt}_i=0, it means the road is blocked, and the move cannot pass through this grid edge.
  • If opti=1\mathit{opt}_i=1, it means the grid edge is a “normal road”. In each move, a piece may pass through at most 11 normal road.
  • If opti=2\mathit{opt}_i=2, it means the grid edge is a “straight road”. In each move, a piece may pass through any number of straight roads, but it must keep moving only horizontally or only vertically, and cannot turn. For example, going from (1,1)(1,1) to (1,3)(1,3) via (1,2)(1,2) along straight roads is allowed, but going from (1,1)(1,1) to (2,2)(2,2) via (1,2)(1,2) is not.
  • If opti=3\mathit{opt}_i=3, it means the grid edge is a “connected road”. In each move, a piece may pass through any number of connected roads, and may turn freely along the way.

It is also required that within a single move, all grid edges passed through must have the same state. For example, moving from (1,1)(1,1) to (1,2)(1,2) via a straight road and then from (1,2)(1,2) to (1,3)(1,3) via a connected road is not allowed.

Other details such as how to decide the winner are irrelevant to this problem and are omitted.

After developing this board game, Xiao z and Xiao c came up with a training strategy to improve: the board starts empty, and then Xiao c places one piece onto some empty intersection each time. Xiao z needs to quickly compute: if we choose this newly placed piece to make exactly one move, how many positions on the board can it reach in total? Note that since this is only mental training, they will not actually move the piece.

Poor Xiao z found his computing ability is not enough to solve this, so he asks you for help.

Input Format

Each test contains multiple test cases.

The first line contains a positive integer TT, the number of test cases.

For each test case:

The first line contains three positive integers n,m,qn, m, q, representing the number of rows, columns, and the number of turns.

The next nn lines each contain a string of length m1m - 1, consisting of characters 0\texttt{0}, 1\texttt{1}, 2\texttt{2}, 3\texttt{3}. The jj-th character in the ii-th line represents the state of the grid edge connecting intersection (i,j)(i, j) to intersection (i,j+1)(i, j + 1).

The next n1n - 1 lines each contain a string of length mm, consisting of characters 0\texttt{0}, 1\texttt{1}, 2\texttt{2}, 3\texttt{3}. The jj-th character in the ii-th line represents the state of the grid edge connecting intersection (i,j)(i, j) to intersection (i+1,j)(i + 1, j).

The next qq lines each contain 44 non-negative integers coli,lvi,xi,yi\mathit{col}_i , \mathit{lv}_i , x_i , y_i, meaning that in turn ii, a piece with color coli\mathit{col}_i and level lvi\mathit{lv}_i is placed at intersection (xi,yi)(x_i , y_i). Here coli=0\mathit{col}_i = 0 denotes a black piece, and coli=1\mathit{col}_i = 1 denotes a white piece. It is guaranteed that there was no piece on (xi,yi)(x_i , y_i) before.

Output Format

For each test case, output qq lines. Each line contains a non-negative integer, representing the number of intersections that the ii-th newly placed piece can reach after it is placed.

1
3 3 5
13
22
23
010
233
0 1 2 3
1 2 2 1
1 3 1 2
0 2 3 2
1 3 2 2

4
3
3
3
2

2
2 3 4
22
33
123
0 2 1 2
0 1 2 1
1 2 1 3
0 3 2 2
3 2 3
3
1
3
32
32
0 2 1 2
1 2 3 2
0 1 2 2

3
4
4
2
5
5
1

见附件中的 chess/chess3.in
见附件中的 chess/chess3.ans
见附件中的 chess/chess4.in
见附件中的 chess/chess4.ans

Hint

[Sample Explanation #1]

After placing piece 11, the positions it can reach are (2,1),(2,2),(3,2),(3,3)(2, 1),(2, 2),(3, 2),(3, 3).

After placing piece 22, the positions it can reach are (2,2),(2,3),(3,1)(2, 2),(2, 3),(3, 1).

After placing piece 33, the positions it can reach are (1,1),(1,3),(2,2)(1, 1),(1, 3),(2, 2).

After placing piece 44, the positions it can reach are (2,2),(3,1),(3,3)(2, 2),(3, 1),(3, 3).

After placing piece 55, the positions it can reach are (2,3),(3,2)(2, 3),(3, 2).

[Constraints]

Test Point ID n×mn \times m \le qq \le Special Property
121 \sim 2 100100 5050 None
363 \sim 6 50005000 20002000
787 \sim 8 2×1052 \times {10}^5 105{10}^5 No “straight roads” and no “connected roads”
9119 \sim 11 No “connected roads”
121412 \sim 14 No “straight roads”
151615 \sim 16 lvi=i\mathit{lv}_i = i
171817 \sim 18 lvi=qi+1\mathit{lv}_i = q - i + 1
192119 \sim 21 20002000 n,m1000n, m \le 1000
222522 \sim 25 105{10}^5 None

For 100%100\% of the testdata, 1T51 \le T \le 5, 2n,m1052 \le n, m \le {10}^5, 4n×m2×1054 \le n \times m \le 2 \times {10}^5, 1qmin{105,n×m}1 \le q \le \min \{ {10}^5, n \times m \}, 1lviq1 \le \mathit{lv}_i \le q, 1xin1 \le x_i \le n, 1yim1 \le y_i \le m, coli{0,1}\mathit{col}_i \in \{ 0, 1 \}.

Note: Since the input and output size of this problem is large, it is recommended to use faster I/O methods.

Translated by ChatGPT 5