原题链接:http://ac.jobdu.com/problem.php?pid=1541
简答题如下:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
const int Max_N = ;
struct Node{
int v, s;
Node *pre, *ch[];
inline void set(int _v = , int _s = , Node *p = NULL){
v = _v, s = _s;
pre = ch[] = ch[] = p;
}
inline void push_up(){
s = ch[]->s + ch[]->s + ;
}
inline void link(Node *x, bool d){
ch[d] = x;
x->pre = this;
}
};
struct SplayTree{
Node *tail, *root, *null;
Node stack[Max_N], *ptr[Max_N];
void init(){
tail = &stack[];
null = tail++;
null->set();
root = null;
}
inline Node *newNode(int v){
Node *p = tail++;
p->set(v, , null);
return p;
}
inline void rotate(Node *x, int d){
Node *y = x->pre;
y->ch[!d] = x->ch[d];
if (x->ch[d] != null) x->ch[d]->pre = y;
x->pre = y->pre;
if (y->pre != null) y->pre->ch[y->pre->ch[] != y] = x;
x->ch[d] = y;
y->pre = x;
y->push_up(), x->push_up();
if (y == root) root = x;
}
inline void update(Node *x){
if (x != null){
update(x->ch[]);
update(x->ch[]);
x->push_up();
}
}
void gogo(int n){
int i, a, b;
for (i = ; i <= n; i++) ptr[i] = newNode(i);
for (i = ; i <= n; i++){
scanf("%d %d", &a, &b);
if (- == a || - == b){
if (- == a && b != -) ptr[i]->link(ptr[b], );
if (- == b && a != -) ptr[i]->link(ptr[a], );
} else {
ptr[i]->link(ptr[a], );
ptr[i]->link(ptr[b], );
}
ptr[i]->push_up();
if (ptr[i]->pre == null) root = ptr[i];
}
update(root);
}
inline void work(){
int i, t;
char buf[];
scanf("%d", &t);
while (t--){
scanf("%s %d", buf, &i);
if ('s' == buf[]){
printf("%d\n", ptr[i]->s);
} else if ('r' == buf[]){
if (ptr[i] == root) continue;
rotate(ptr[i], ptr[i]->pre->ch[] == ptr[i]);
} else {
if (ptr[i] == root) printf("-1\n");
else printf("%d\n", ptr[i]->pre->v);
}
}
}
}spt;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n;
while (~scanf("%d", &n)){
spt.init(), spt.gogo(n), spt.work();
}
return ;
}