#P2596. [ZJOI2006] 书架

[ZJOI2006] 书架

Description

Xiao T has a very large bookcase. Its structure is a bit special: the books in the bookcase are stacked in a single column from top to bottom. She labels each book with the positive integers from 11 to nn.

When Xiao T reads, each time she takes out one book, and after finishing it, she puts it back into the bookcase and then takes the next one. Because the books are so attractive, she often forgets the exact position where the book was originally placed. However, Xiao T has a very good memory, so when she puts a book back, she can at least place it near where it was taken from. For example, if when she took the book there were xx books above it, then when putting it back, there can only be x1x - 1, xx, or x+1x + 1 books above it.

Of course, there are special cases, such as when the phone rings or a friend visits while she is reading. At these times, the careless Xiao T may casually put the book at the very top or the very bottom of all the books in the bookcase, and then walk away.

Over time, the order of the books in Xiao T’s bookcase becomes more and more chaotic, making it increasingly difficult to find a specific labeled book. So she asks you to write a library management program to process her operations while reading and answer two types of queries:

  • What is the position of the book with ID xx in the bookcase.
  • What is the ID of the ii-th book from top to bottom.

Input Format

The first line contains two integers, representing the number of books nn and the number of commands mm.

The second line contains nn integers. The ii-th integer pip_i is the ID of the ii-th book from the top at the beginning.

Then there are mm lines, each describing an operation. Each line starts with a string opop.

  • If opop is Top, followed by an integer ss, move the book with ID ss to the very top.
  • If opop is Bottom, followed by an integer ss, move the book with ID ss to the very bottom.
  • If opop is Insert, followed by two integers s,ts, t, then if there are xx books above the book with ID ss, when putting it back there should be x+tx + t books above it.
  • If opop is Ask, followed by an integer ss, ask how many books are above the book with ID ss.
  • If opop is Query, followed by an integer ss, ask for the ID of the ss-th book from the top.

Output Format

For each query, output a single integer on its own line as the answer.

10 10
1 3 2 7 5 8 10 4 9 6
Query 3
Top 5
Ask 6
Bottom 3
Ask 3
Top 6
Insert 4 -1
Query 5
Query 2
Ask 2

2
9
9
7
5
3

Hint

Constraints

For 100%100\% of the testdata, it is guaranteed that:

  • 3n,m8×1043 \leq n, m \leq 8 \times 10^4.
  • pip_i is a permutation of 11 to nn.
  • 1sn1 \leq s \leq n, 1t1-1 \leq t \leq 1, and opop is one of the five strings above.
  • When there is no book above the book with ID ss, the operation Insert s -1 will not be performed.
  • When there is no book below the book with ID ss, the operation Insert s 1 will not be performed.

Translated by ChatGPT 5