NOIp2017 lined up (tree line)

Title Description

Sylvia Is a girl's love of learning.

Some time ago, Sylvia I attended the school of military training. As we all know, when the military need to stand square.

Sylvia square where there are n-\ Times m n- × m pupils, the number of matrix rows is  n- n-number of column  m m.

For ease of administration, at the beginning of the training instructors, according to front to back, left to right in order of square to students from 1 to  n-\ Times m n- × m compiled on the number (see examples below). Namely: the initial, first  I I row  j student column number is j (. 1-I) \ Times m + j ( I - . 1 ) × m + j.

However, in practice, when the square, because students often have a variety of things to leave. In a day, a total of happened  q such q pieces leaving the team event. Each leave event may be the number of (X, Y) (. 1 \ Le X \ Le n-,. 1 \ Le Y \ Le m) ( X , Y ) ( . 1 X n- , . 1 Y m ) is described, It represents  the X- the X-line  the y- the y-listed students leave.

After the students leave, the team there is a vacancy. To tidy team, instructors will be issued this order two instructions:

  1. Left par. Then the first column to remain intact, all the students to fill the gap left. After following the instruction not difficult to find, at the gap  x row x  m m columns.

  2. Look ahead together. Then the first line to remain intact, all the students to fill the vacancy forward. After following the instruction not difficult to find, at the gap  n of n row  m m columns.

Instructors provisions can not have two or more students at the same time leaving the team. That left the band after a student rejoin former student next to leave. Therefore, when each of the students to leave a rejoin, and only the ranks of the  n- n-row  m m a column space, then naturally the student to fill this position.

Because the station square is really boring, so  Sylvia you want to calculate every leaving the team event, the number of students leaving the team is.

Note: Each student numbers will not leave with the occurrence of an event of change, leaving the team after the incident in the square number of students may be out of order.

Input Format

The input common-  Q. 1 + Q + . 1 line.

The first row contains three space-separated positive integers  n-, m, Q n- , m , Q, represents a square matrix size is  n- n-rows  m m columns, a total occurred  Q Q incident.

Next  Q Q lines in accordance with the sequence of events described  Q Q pieces of event. Each row is two integers  the X-, the y- the X- , the y-, separated by a space, leaving the team event indicates that the student was leaving the team ranked first in  the X- the X-line  the y- the y-column.

Output Format

In accordance with the input sequence of events, each event output line an integer representing the students left the band leaving the team event number.

Sample input and output

Input # 1
2 2 3 
1 1 
2 2 
1 2 
Output # 1
1
1
4

Description / Tips

Sample Description 1 [O]

The queue process shown above, each row describes an event. In the first incident, numbered 1 Classmate leaving the team, then empty the first column in the first row. Subsequently all the students standard flush left, then the number  2 students 2 is moved by one step to the left, to the first line vacancy second column. All students then marked up together, then the number . 4 4 students step up, then the vacancies move to the second column of the second row. The last number is 1 1 students return to fill the vacancy in.

[Agreed] with the scale data

To ensure that each event data satisfies  . 1 \ Le X \ n-Le,. 1 \ Le Y \ Le m . 1 X n- , . 1 Y m

Guess you like

Origin www.cnblogs.com/ajmddzp/p/11706727.html