n的范围很大,但q的范围比较小.可以把top,query操作用到的点分离出来,没用到的段缩成点
对于top 把x转到根,删除后加到开头位置
对于query 旋转到根直接输出
对于rank,递归
time limit: 2000/1000 ms (java/others) memory limit: 32768/32768 k (java/others)
total submission(s): 2216 accepted submission(s): 561
problem description
ponyo and garfield are waiting outside the box-office for their favorite movie. because queuing is so boring, that they want to play a game to kill the time. the game is called “queue-jumpers”. suppose that there are n people numbered from 1 to n stand in a
line initially. each time you should simulate one of the following operations:
1. top x :take person x to the front of the queue
2. query x: calculate the current position of person x
3. rank x: calculate the current person at position x
where x is in [1, n].
ponyo is so clever that she plays the game very well while garfield has no idea. garfield is now turning to you for help.
input
in the first line there is an integer t, indicates the number of test cases.(t<=50)
in each case, the first line contains two integers n(1<=n<=10^8), q(1<=q<=10^5). then there are q lines, each line contain an operation as said above.
output
for each test case, output “case d:“ at first line where d is the case number counted from one, then for each “query x” operation ,output the current position of person x at a line, for each “rank x” operation, output the current person at position x at a line.
sample input
sample output
author
wzc1989
source