#P3285. [SCOI2014] 方伯伯的OJ

[SCOI2014] 方伯伯的OJ

Description

Uncle Fang is building his OJ. Now he is handling the user ranking problem on the OJ. There are nn registered users on the OJ, numbered 1n1\sim n, and initially they are ranked in ascending order of their IDs.

Depending on his mood, Uncle Fang performs the following four operations to modify users’ ranks and IDs:

  1. Operation format 1  x  y1\ \ x\ \ y means: change the user whose ID is xx to have ID yy, while keeping their rank unchanged. After this operation, output that user’s position in the queue. It is guaranteed that xx appears in the queue, and yy is an ID not currently present in the ranking.
  2. Operation format 2  x2\ \ x means: move the user whose ID is xx to the first position. After this operation, output the rank of user xx before the move.
  3. Operation format 3  x3\ \ x means: move the user whose ID is xx to the last position. After this operation, output the rank of user xx before the move.
  4. Operation format 4  k4\ \ k means: query the ID of the user whose current rank is kk. After this operation, output that user’s ID.

To prevent others from snooping on his work, Uncle Fang encrypts his operations by changing the four formats to:

  • 1  x+a  y+a1\ \ x+a\ \ y+a
  • 2  x+a2\ \ x+a
  • 3  x+a3\ \ x+a
  • 4  k+a4\ \ k+a
  • where aa is the output of the previous operation, and initially a=0a=0.

Example: if the previous operation’s output is 55, and this operation’s input is 1  13  151\ \ 13\ \ 15, since the input is encrypted, the operation you should process is 1  8  101\ \ 8\ \ 10.

You have intercepted all of Uncle Fang’s operations. Please produce the results.

Input Format

The first line contains two integers nn and mm, representing the initial number of users and the number of operations. Then follow mm lines, each containing one query in the formats described above.

Output Format

Output mm lines. The integer on the ii-th line is the output of the ii-th operation.

10 10
1 2 11
3 13
2 5
3 7
2 8
2 10
2 11
3 14
2 18
4 9
2
2
2
4
3
5
5
7
8
11

Hint

For 100%100\% of the testdata, 1n1081 \le n \le 10^8, 1m1051 \le m \le 10^5.

It is guaranteed that for all operations 1,2,31,2,3, xx already appears in the queue. For all operations 11, 1y2×1081 \le y \le 2\times 10^8, and yy does not appear in the queue.

For all operations 44, it is guaranteed that 1kn1 \le k \le n.

Translated by ChatGPT 5