#P3960. [NOIP 2017 提高组] 列队
[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 students in Sylvia’s formation, with rows and columns.
For convenience, at the beginning of training, the instructor numbers the students from to in order from front to back and from left to right (see the example below). That is: initially, the student at row , column has ID .
However, during the formation drills, students often need to leave the formation for various reasons. In one day, there are such leave events. Each leave event is described by a pair (), meaning the student at row , column leaves.
After a student leaves, an empty spot appears. To keep the formation tidy, the instructor issues the following two commands in order:
- 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 , column .
- 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 , column .
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 , column , 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 lines.
The first line contains positive integers , , and , separated by spaces, indicating a formation of rows and columns, with a total of events.
The next lines describe the events in the order they occur. Each line contains two integers , separated by a space, indicating that in this leave event, the student currently at row , column 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 】
$$\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 leaves, so the empty spot is at row , column . Next, everyone aligns left; the student with ID moves one step left, and the empty spot moves to row , column . Then everyone aligns forward; the student with ID moves one step up, and the empty spot moves to row , column . Finally, the student with ID returns and fills the empty spot.
【Constraints and Notes】
| Test point ID | Other notes | |||
|---|---|---|---|---|
| None | ||||
| All events have | ||||
| None | ||||
The testdata guarantees that each event satisfies .
Translated by ChatGPT 5
京公网安备 11011102002149号