写的最恶心的一个离散化orz……splay的操作其实很简单 用treap也可以写。。
因为N太大必须离散化 = =把 T 和 Q 操作的数字给提出来 然后其他的保存区间
我写的比较orz我是直接把每个操作的节点的地址给保存了 = =
T操作:把操作节点提根 然后右子树最小的儿子提到根节点的右儿子 把根节点的左儿子接到右儿子上(删除根节点)然后再把操作节点插入到前面那个哨兵的后面 再splay(为了pushup = =)
Q操作:把操作节点提根 然后查询rank就行了。。
R操作:名次树一样的操作 找到包含第R个数的节点 然后再输出那个数就行了
btw这道题有一个神奇的树状数组解法:http://www.cppblog.com/Yuan/archive/2010/08/18/123871.html
Queue-jumpers
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2546 Accepted Submission(s): 673
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
39 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
wzc1989Source
2010 ACM-ICPC Multi-University Training Contest(1)——Host by FZU
Code
#include <cstdio>
#include <iostream>
#include <algorithm>
#define lc c[0]
#define rc c[1]
using namespace std;
int readint()
{
char c;int sign = 1, n = 0; c = getchar();
while(c > '9' || c < '0'){ if(c == '-')sign = -1; c = getchar(); }
while(c <= '9' && c >= '0'){ n = n*10 + c-'0'; c = getchar();}
return n*sign;
}
const int inf = 0x3f3f3f3f;
const int maxn = 101000;
int T;
int N, Q;
struct operation{
char s; int x;
}o[maxn];
int sa[maxn], cnt, c;
bool cmp(int a, int b)
{
return o[a].x < o[b].x;
}
struct Splay{
struct node{
node *c[2], *f;
int l, r, s;
int size, sum;
inline bool right(){ return f -> rc == this; }
inline void setch(node *p, bool r) { c[r] = p; p -> f = this; }
}T[maxn], *root, *null, *op[maxn];
inline node *NewNode(int l = 0, int r = 0)
{
T[cnt].lc = T[cnt].rc = T[cnt].f = null;
T[cnt].l = l; T[cnt].r = r; T[cnt].s = 1;
T[cnt].size = T[cnt].sum = r - l + 1;
return &T[cnt++];
}
inline void pushup(node *p)
{
p -> sum = p -> size + p -> lc -> sum + p -> rc -> sum;
p -> s = 1 + p -> lc -> s + p -> rc -> s;
}
inline void init()
{
cnt = 0;
null = NewNode(); null -> s = null -> sum = 0;
root = NewNode(); node *p = NewNode();
root -> setch(p, 1); pushup(root);
}
void rotate(node *p)
{
node *fa = p -> f; bool r = p -> right();
fa -> f -> setch(p, fa -> right());
fa -> setch(p -> c[r ^ 1], r);
p -> setch(fa, r ^ 1);
pushup(fa);
}
void splay(node *p, node *fa)
{
while(p -> f != fa)
{
if(p -> f -> f == fa) rotate(p);
else if(p -> right() == p -> f -> right())
{
rotate(p -> f);
rotate(p);
}
else
{
rotate(p);
rotate(p);
}
}
pushup(p);
if(p -> f == null) root = p;
}
node *find(int k)
{
node *p;
for(p = root; ; )
{
int rank = p -> lc -> s + 1;
if(rank == k) return p;
if(rank > k) p = p -> lc;
else {
p = p -> rc;
k -= rank;
}
}
}
node *getrange(int l, int r)
{
--l; ++r;
splay(find(l), null); splay(find(r), root);
return root -> rc -> lc;
}
inline void pushrange() { pushup(root); pushup(root -> rc); }
node *insert(int k, int l, int r)
{
node *p = getrange(k, k - 1);
p = NewNode(l, r);
root -> rc -> setch(p, 0);
pushrange(); return p;
}
void top(int x)
{
splay(op[x], null); node *temp = root;
if(temp -> lc -> s == 1)return;
node *p; for(p = root -> rc; p -> lc != null; p = p -> lc);
splay(p, root);
root -> rc -> setch(root -> lc, 0);
root = root -> rc; root -> f = null; pushup(root);
temp -> rc = temp -> lc = null;
for(p = root; p -> lc != null; p = p -> lc);
if(p -> rc == null) p -> setch(temp, 1);
else
{
for(p = p -> rc; p -> lc != null; p = p -> lc);
p -> setch(temp, 0);
}
splay(temp, null);
}
void query(int x)
{
splay(op[x], null);
printf("%d\n", root -> lc -> sum);
}
void Rank(int x)
{
++x;
for(node *p = root; ; )
{
int lsize = p -> lc -> sum;
if(lsize >= x) p = p -> lc;
else if(lsize < x && lsize + p -> size >= x)
{
printf("%d\n", p -> l + x - lsize - 1);
break;
}
else
{
x -= lsize + p -> size;
p = p -> rc;
}
}
}
void pre()
{
sort(sa + 1, sa + c + 1, cmp); int kk = 1;
if(o[sa[1]].x > 1) insert(++kk, 1, o[sa[1]].x - 1);
for(int i = 1; i <= c; ++i)
{
if(i - 1 && o[sa[i]].x - o[sa[i-1]].x > 1)
{
insert(++kk, o[sa[i-1]].x + 1, o[sa[i]].x - 1);
}
op[sa[i]] = insert(++kk, o[sa[i]].x, o[sa[i]].x);
while(o[sa[i + 1]].x == o[sa[i]].x)
{
op[sa[i + 1]] = op[sa[i]];
++i;
}
}
if(N - o[sa[c]].x >= 1) insert(++kk, o[sa[c]].x + 1, N);
}
}s;
int main()
{
T = readint();
for(int cas = 1; cas <= T; ++cas)
{
N = readint(); Q = readint(); s.init(); c = 0; char st[10];
for(int i = 1; i <= Q; ++i)
{
scanf(" %s%d", st, &o[i].x); o[i].s = st[0];
if(o[i].s == 'T' || o[i].s == 'Q') sa[++c] = i;
}
s.pre(); printf("Case %d:\n", cas);
for(int i = 1; i <= Q; ++i)
{
switch(o[i].s)
{
case 'T' : s.top(i); break;
case 'Q' : s.query(i); break;
case 'R' : s.Rank(o[i].x); break;
}
}
}
return 0;
}