Zth's Blog

记录学习路上的点滴

0%

普通平衡树

模板题

代码

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
#include <iostream>
#include <cstdio>

int n;

struct Bst
{
int root, tot;
int fa[212345], child[212345][2];
int key[212345], cnt[212345];
int sz[212345];

void Update(int x)
{
sz[x] = cnt[x] + sz[child[x][0]] + sz[child[x][1]];
}

int Get(int x)
{
return child[fa[x]][1] == x;
}

void Rotate(int x)
{
int y = fa[x], z = fa[fa[x]];
int lr = Get(x);

child[z][child[z][1] == y] = x;
fa[x] = z;

child[y][lr] = child[x][lr ^ 1];
fa[child[x][lr ^ 1]] = y;

child[x][lr ^ 1] = y;
fa[y] = x;

Update(y);
Update(x);
}

void Splay(int x, int s)
{
while(fa[x] != s)
{
if(fa[fa[x]] != s)
Rotate(Get(x) == Get(fa[x]) ? fa[x] : x);

Rotate(x);
}

if(! s) root = x;
}

void Find(int x)
{
int now = root;

while(key[now] != x) now = child[now][key[now] < x];

Splay(now, 0);
}

void Insert(int x)
{
int now = root, f = 0;

while(now && key[now] != x)
{
f = now;

now = child[now][key[now] < x];
}

if(now)
cnt[now] ++;
else
{
now = ++ tot;

if(f) child[f][key[f] < x] = now;
fa[now] = f;

child[now][0] = child[now][1] = 0;
key[now] = x;
cnt[now] = sz[now] = 1;
}

Update(now);

Splay(now, 0);
}

int Pre(int x)
{
Find(x);

int now = child[root][0];
while(child[now][1]) now = child[now][1];

return now;
}

int Next(int x)
{
Find(x);

int now = child[root][1];
while(child[now][0]) now = child[now][0];

return now;
}

void Delete(int x)
{
int pre = Pre(x), next = Next(x);

Splay(pre, 0);
Splay(next, pre);

int now = child[next][0];

if(cnt[now] > 1)
cnt[now] --, Update(now), Splay(now, 0);
else
child[next][0] = 0, Update(next), Splay(next, 0);
}

int Kth(int x)
{
x ++;

int now = root;

while(true)
{
if(child[now][0] && x <= sz[child[now][0]])
now = child[now][0];
else
{
int tmp = sz[child[now][0]] + cnt[now];

if(x <= tmp) return key[now];

x -= tmp;
now = child[now][1];
}
}
}
} bst;

int main()
{
bst.Insert(-1e9 - 1);
bst.Insert(1e9 + 1);

int z = 0;

scanf("%d", &n);
for(int i = 1; i <= n; i ++)
{
int opt, x;
scanf("%d%d", &opt, &x);

if(opt == 1// 插入一个元素
bst.Insert(x);
else if(opt == 2) // 删除一个元素
{
bst.Insert(x);

if(bst.cnt[bst.root] > 1) bst.Delete(x);

bst.Delete(x);
}
else if(opt == 3) // 查询x的排名, 即比x小的数的个数+1
{
bst.Insert(x);

printf("%d\n", bst.sz[bst.child[bst.root][0]]);

bst.Delete(x);
}
else if(opt == 4) // 排名为x的数
printf("%d\n", bst.Kth(x));
else if(opt == 5) // 小于x且最大的数, 注意是小于不是小于等于
{
bst.Insert(x);

printf("%d\n", bst.key[bst.Pre(x)]);

bst.Delete(x);
}
else // 大于x且最小的数
{
bst.Insert(x);

printf("%d\n", bst.key[bst.Next(x)]);

bst.Delete(x);
}
}

return 0;
}