-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAVLTree_Speed.c
215 lines (198 loc) · 3.77 KB
/
AVLTree_Speed.c
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//P6136 5.5s
#include <stdio.h>
#include <stdlib.h>
#define INF 2147483647
struct tree
{
struct tree* ls;
struct tree* rs;
int siz,con,data,height;
};
typedef struct tree stu;
typedef struct tree* ptr;
int tot;
stu pol[1600000];
int read()
{
int q=0,w=1;
char c=getchar();
while (c>'9' || c<'0')
{
if(c=='-') w=-1;
c=getchar();
}
while (c>='0' && c<='9')
{
q=q*10+c-'0';
c=getchar();
}
return w*q;
}
int size(ptr now)
{return now?now->siz:0;}
int h(ptr now)
{return now?now->height:0;}
void pushup(ptr now)
{
if (now==NULL) return;
now->height=1;
now->siz=now->con;
now->siz+=size(now->ls);
now->siz+=size(now->rs);
if (h(now->ls)>h(now->rs))
now->height+=h(now->ls);
else now->height+=h(now->rs);
return;
}
void left(ptr* now)
{
ptr tmp=(*now)->rs;
(*now)->rs=tmp->ls;
tmp->ls=*now;
tmp->siz=(*now)->siz;
pushup(*now),pushup(tmp);
*now=tmp; return;
}
void right(ptr* now)
{
ptr tmp=(*now)->ls;
(*now)->ls=tmp->rs;
tmp->rs=*now;
tmp->siz=(*now)->siz;
pushup(*now),pushup(tmp);
*now=tmp; return;
}
void balance(ptr *now)
{
if (*now==NULL) return;
if (h((*now)->ls)-h((*now)->rs)==2)
{
if (h((*now)->ls->ls)>h((*now)->ls->rs)) right(now);
else left(&(*now)->ls),right(now); return;
}
if (h((*now)->rs)-h((*now)->ls)==2)
{
if (h((*now)->rs->rs)>h((*now)->rs->ls)) left(now);
else right(&(*now)->rs),left(now); return;
}
return;
}
void ins(ptr* now,int num)
{
if (*now==NULL)
{
*now=&pol[tot++];
(*now)->siz=(*now)->con=1;
(*now)->data=num,(*now)->height=0;
(*now)->ls=(*now)->rs=NULL; return;
}
if ((*now)->data==num)
{
(*now)->con++;
pushup(*now); return;
}
if ((*now)->data>num) ins(&(*now)->ls,num);
else ins(&(*now)->rs,num);
pushup(*now); balance(now); return;
}
void del(ptr* now,int num)
{
if (*now==NULL) return;
if ((*now)->data==num)
{
if ((*now)->con>1) (*now)->con--;
else
{
ptr cng=*now;
if ((*now)->ls==NULL) *now=(*now)->rs;
else if ((*now)->rs==NULL) *now=(*now)->ls;
else
{
cng=(*now)->rs;
while (cng->ls) cng=cng->ls;
(*now)->data=cng->data;
(*now)->con=cng->con,cng->con=1;
del(&(*now)->rs,cng->data);
}
}
}
else
{
if ((*now)->data>num) del(&(*now)->ls,num);
else del(&(*now)->rs,num);
}
pushup(*now); balance(now); return;
}
int fnd(ptr now,int num)
{
if (now==NULL) return 0;
if (now->data==num) return size(now->ls);
if (now->data>num) return fnd(now->ls,num);
return size(now->ls)+now->con+fnd(now->rs,num);
}
int kth(ptr now,int num)
{
if (now==NULL) return INF;
if (size(now->ls)>=num) return kth(now->ls,num);
if (size(now->ls)+now->con>=num) return now->data;
return kth(now->rs,num-size(now->ls)-now->con);
}
int qan(ptr now,int num,int ans)
{
if (now->data>=num)
{
if (now->ls==NULL) return ans;
return qan(now->ls,num,ans);
}
if (now->rs==NULL) return now->data;
return qan(now->rs,num,now->data);
}
int hou(ptr now,int num,int ans)
{
if (now->data<=num)
{
if (now->rs==NULL) return ans;
return hou(now->rs,num,ans);
}
if (now->ls==NULL) return now->data;
return hou(now->ls,num,now->data);
}
int main()
{
ptr root=NULL;
int n,m,x,y,ans=0,last=0;
n=read(),m=read();
while (n--)
{
x=read();
ins(&root,x);
}
while (m--)
{
x=read(),y=read(),y^=last;
if (x==1) {ins(&root,y); continue;}
if (x==2) {del(&root,y); continue;}
if (x==3)
{
last=fnd(root,y)+1;
ans^=last; continue;
}
if (x==4)
{
last=kth(root,y);
ans^=last; continue;
}
if (x==5)
{
last=qan(root,y,-INF);
ans^=last; continue;
}
if (x==6)
{
last=hou(root,y,INF);
ans^=last; continue;
}
}
printf("%d\n",ans);
return 0;
}