#P3960. [NOIP 2017 提高组] 列队

    ID: 2196 远端评测题 2000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2017线段树平衡树树状数组NOIp 提高组

[NOIP 2017 提高组] 列队

Description

Sylvia is a girl who loves studying.

Some time ago, Sylvia took part in the school’s military training. As is well known, during training they must stand in a square formation.

There are n×mn \times m students in Sylvia’s formation, with nn rows and mm columns.

For convenience, at the beginning of training, the instructor numbers the students from 11 to n×mn \times m in order from front to back and from left to right (see the example below). That is: initially, the student at row ii, column jj has ID (i1)×m+j(i-1)\times m + j.

However, during the formation drills, students often need to leave the formation for various reasons. In one day, there are qq such leave events. Each leave event is described by a pair (x,y)(x, y) (1xn,1ym1 \le x \le n, 1 \le y \le m), meaning the student at row xx, column yy leaves.

After a student leaves, an empty spot appears. To keep the formation tidy, the instructor issues the following two commands in order:

  1. Align left. The first column remains still, and all students shift left to fill the gap. It is easy to see that after this command, the empty spot is at row xx, column mm.
  2. Align forward. The first row remains still, and all students shift forward to fill the gap. It is easy to see that after this command, the empty spot is at row nn, column mm.

The instructor stipulates that no two or more students may leave at the same time. That is, after the previously departed student has returned, the next student may leave. Therefore, when each departed student is to return, there is exactly one empty spot at row nn, column mm, and the student naturally fills that position.

Because standing in formation is really boring, Sylvia wants to compute, for each leave event, the ID number of the student who leaves.

Note: A student’s ID never changes due to leave events, though the order of IDs in the formation may become scrambled after events.

Input Format

The input consists of q+1q+1 lines.

The first line contains 33 positive integers nn, mm, and qq, separated by spaces, indicating a formation of nn rows and mm columns, with a total of qq events.

The next qq lines describe the qq events in the order they occur. Each line contains two integers x,yx, y, separated by a space, indicating that in this leave event, the student currently at row xx, column yy leaves.

Output Format

For each event, in the input order, output one line with a single integer: the ID of the student who leaves in that event.

2 2 3 
1 1 
2 2 
1 2 
1
1
4

Hint

【Explanation for Sample Input/Output 11

$$\begin{matrix} \begin{bmatrix} 1 & 2 \\ 3 & 4 \\ \end{bmatrix} & \Rightarrow & \begin{bmatrix} & 2 \\ 3 & 4 \\ \end{bmatrix} & \Rightarrow & \begin{bmatrix} 2 & \\ 3 & 4 \\ \end{bmatrix} & \Rightarrow & \begin{bmatrix} 2 & 4 \\ 3 & \\ \end{bmatrix} & \Rightarrow & \begin{bmatrix} 2 & 4 \\ 3 & 1 \\ \end{bmatrix} \\[1em] \begin{bmatrix} 2 & 4 \\ 3 & 1 \\ \end{bmatrix} & \Rightarrow & \begin{bmatrix} 2 & 4 \\ 3 & \\ \end{bmatrix} & \Rightarrow & \begin{bmatrix} 2 & 4 \\ 3 & \\ \end{bmatrix} & \Rightarrow & \begin{bmatrix} 2 & 4 \\ 3 & \\ \end{bmatrix} & \Rightarrow & \begin{bmatrix} 2 & 4 \\ 3 & 1 \\ \end{bmatrix}\\[1em] \begin{bmatrix} 2 & 4 \\ 3 & 1 \\ \end{bmatrix} & \Rightarrow & \begin{bmatrix} 2 & \\ 3 & 1 \\ \end{bmatrix} & \Rightarrow & \begin{bmatrix} 2 & \\ 3 & 1 \\ \end{bmatrix} & \Rightarrow & \begin{bmatrix} 2 & 1 \\ 3 & \\ \end{bmatrix} & \Rightarrow & \begin{bmatrix} 2 & 1 \\ 3 & 4 \\ \end{bmatrix} \end{matrix}$$

The lineup process is shown above, with each row describing one event. In the first event, the student with ID 11 leaves, so the empty spot is at row 11, column 11. Next, everyone aligns left; the student with ID 22 moves one step left, and the empty spot moves to row 11, column 22. Then everyone aligns forward; the student with ID 44 moves one step up, and the empty spot moves to row 22, column 22. Finally, the student with ID 11 returns and fills the empty spot.

【Constraints and Notes】

Test point ID nn mm qq Other notes
161\sim 6 103\le 10^3 500\le 500 None
7107\sim 10 5×104\le 5\times 10^4
111211\sim 12 =1=1 105\le 10^5 All events have x=1x=1
131413\sim 14 3×105\le 3\times 10^5
151615\sim 16 3×105\le 3\times 10^5
171817\sim 18 105\le 10^5 None
192019\sim 20 3×105\le 3\times 10^5

The testdata guarantees that each event satisfies 1xn,1ym1 \le x \le n,1 \le y \le m.

Translated by ChatGPT 5