hdu 3436 Queue-jumpers(splay tree)

Queue-jumpers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1225    Accepted Submission(s): 273


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
  
  
3 9 5 Top 1 Rank 3 Top 7 Rank 6 Rank 8 6 2 Top 4 Top 5 7 4 Top 5 Top 2 Query 1 Rank 6
 

Sample Output
  
  
Case 1: 3 5 8 Case 2: Case 3: 3 6
 

Author
wzc1989
 

Source
 

Recommend
zhouzeyong

题目思路:splay,也可以用线段树或树状数组,主要难点在离散化,这个离散化是把点和点这间的区间都离散化,离散化后的点要保存原来区间的长度和起始点等信息,这样就可用splay模拟了,top操作可以先删点再插入,也可以将要删的点伸展到根结点,然后将它的左子树接到它后继的左边,这样它便成为整棵树最左边的结点了。由于人的编号和结点的index是一一对应的,不会改变,所以可以把这个关系存起来,这样访问的时候就可以直接通过人的编号找到结点了。

splay  tree 参考:http://blog.csdn.net/wings_of_liberty/article/details/7747731

树状数组参考:http://www.cppblog.com/Yuan/archive/2010/08/18/123871.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值