-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1056 [HAOI2008]排名系统(Splay_pointer)(=1862).cpp
145 lines (145 loc) · 3.19 KB
/
1056 [HAOI2008]排名系统(Splay_pointer)(=1862).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <cstdio>
#include <cstring>
#define P 1000003
bool plined;
struct Splay *root,*null;
void nullinit();
struct Splay{
Splay *l,*r,*pa; int size; char *Name; int Score;
inline void update(){
size=l->size+r->size+1;
}
#define x this
void zig(){
Splay *y=x->pa,*z=y->pa;
x->pa=z; (y==z->l?z->l:z->r)=x;
x->r->pa=y; y->l=x->r;
y->pa=x; x->r=y;
y->update();
}
void zag(){
Splay *y=x->pa,*z=y->pa;
x->pa=z; (y==z->l?z->l:z->r)=x;
x->l->pa=y; y->r=x->l;
y->pa=x; x->l=y;
y->update();
}
void splay(){ splay(root); }
void splay(Splay* &root){
Splay *y,*z,*rtpa=root->pa;
while((y=x->pa)!=rtpa){
z=y->pa;
if(z==rtpa) x==y->l?x->zig():x->zag(); else
if(y==z->l) x==y->l?(y->zig(),x->zig()):(x->zag(),x->zig());
else x==y->r?(y->zag(),x->zag()):(x->zig(),x->zag());
}
update();
root=x;
}
#undef x
void ins(Splay* k){
Splay *x=this,*y=x->pa;
while(x!=null){
y=x;
x=k->Score>x->Score?x->l:x->r;
}
(k->Score>y->Score?y->l:y->r)=k;
k->pa=y;
k->splay();
}
Splay* cutl(){
Splay *x=this,*y;
while (x->l!=null) x=x->l;
x->r->pa=x->pa; (x==x->pa->l?x->pa->l:x->pa->r)=x->r;
for (y=x->pa;y!=null;y=y->pa) --y->size;
x->r=x->pa=null;
return x;
}
Splay* cutr(){
Splay *x=this,*y;
while (x->r!=null) x=x->r;
x->l->pa=x->pa; (x==x->pa->l?x->pa->l:x->pa->r)=x->l;
for (y=x->pa;y!=null;y=y->pa) --y->size;
x->l=x->pa=null;
return x;
}
void cut(){
splay();
root=(l!=null?l->cutr():r!=null?r->cutl():null);
root->l=l; root->l->pa=root;
root->r=r; root->r->pa=root;
root->update();
l=r=pa=null;
size=1;
}
int Rank(){
splay();
return l->size+1;
}
Splay* No(int k){
if(k>size) k=size+1;
Splay* x=this;
int mid;
while (x!=null){
mid=x->l->size+1;
if (k<mid) x=x->l; else
if (k>mid) x=x->r,k-=mid; else
return x;
}
return null;
}
void List(const int rank,const int sum){
root->No(rank-1)->splay(root);
root->No(rank+sum)->splay(root->r);
plined=false; root->r->l->pline(); putchar('\n');
}
void pline(){
if (this==null) return;
l->pline();
plined?putchar(' '):plined=true;
printf("%s",Name);
r->pline();
}
} spl[250000],*newspl=spl;
#define newSplay (*newspl=(Splay){null,null,null,1,0,0},newspl++)
inline void nullinit(){
null->l=null->r=null->pa=null; null->size=null->Score=0; null->Name="null";
}
Splay *h[P];
typedef char NameType[12];
NameType Names[250001],*newName=Names;
int main(){
int n; scanf("%d",&n);
char opt,*i; int v;
null=new Splay; nullinit(); root=null;
Splay *tmp,*tt=new Splay;
tt->size=-1;
tmp=newSplay;
tmp->size=0;
tmp->Score=0x7FFFFFFF;
tmp->l=tt;
root->ins(tmp);
tmp=newSplay;
tmp->size=0;
tmp->Score=-1;
tmp->r=tt;
root->ins(tmp);
while(n--){
getchar(); opt=getchar();
if(opt=='+'){
scanf("%s",newName); for(v=0,i=*newName;*i;++i) v=(v*27+*i)%P;
while(h[v]&&strcmp(h[v]->Name,*newName)) if(++v==P) v=0;
if(h[v]) h[v]->cut();
else h[v]=newSplay;
h[v]->Name=*newName++;
scanf("%d",&h[v]->Score);
root->ins(h[v]);
}else
if(opt=='?'&&scanf("%d",&v)!=1){
scanf("%s",newName); for(v=0,i=*newName;*i;++i) v=(v*27+*i)%P;
while(h[v]&&strcmp(h[v]->Name,*newName)) if(++v==P) v=0;
printf("%d\n",h[v]->Rank());
}else
root->List(v,10);
}
}