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
| #include <cstdio>
const int N = 100005;
struct Splay {
int root = 0, id = 0, fa[N]{}, ch[N][2]{}, val[N]{}, cnt[N]{}, sz[N]{};
void pushUp(int x) { sz[x] = sz[ch[x][0]] + sz[ch[x][1]] + cnt[x]; }
bool get(int x) { return x == ch[fa[x]][1]; }
void clear(int x) {
ch[x][0] = ch[x][1] = fa[x] = val[x] = sz[x] = cnt[x] = 0;
}
void rotate(int x) {
int y = fa[x], z = fa[y], chk = get(x);
ch[y][chk] = ch[x][chk ^ 1];
if (ch[x][chk ^ 1]) fa[ch[x][chk ^ 1]] = y;
ch[x][chk ^ 1] = y;
fa[y] = x;
fa[x] = z;
if (z) ch[z][y == ch[z][1]] = x;
pushUp(y);
pushUp(x);
}
void splay(int x) {
for (int f = fa[x]; f = fa[x], f; rotate(x))
if (fa[f]) rotate(get(x) == get(f) ? f : x);
root = x;
}
void insert(int k) {
if (!root) {
val[++id] = k;
cnt[id]++;
root = id;
pushUp(root);
return;
}
int cur = root, f = 0;
while (1) {
// 等于就结束
if (val[cur] == k) {
cnt[cur]++;
pushUp(cur);
pushUp(f);
splay(cur);
break;
}
f = cur;
cur = ch[cur][val[cur] < k];
// 如果小就往左,大就往右,
// 如果当前点不存在 那就插入到这个位置
if (!cur) {
val[++id] = k;
cnt[id]++;
fa[id] = f;
ch[f][val[f] < k] = id;
pushUp(id);
pushUp(f);
splay(id);
break;
}
}
}
int rank(int k) {
int ans = 0, cur = root;
while (1) {
if (k < val[cur]) {
cur = ch[cur][0];
} else {
ans += sz[ch[cur][0]];
if (k == val[cur]) {
splay(cur);
return ans + 1;
}
ans += cnt[cur];
cur = ch[cur][1];
}
}
}
int kth(int k) {
int cur = root;
while (1) {
if (ch[cur][0] && k <= sz[ch[cur][0]]) {
cur = ch[cur][0];
} else {
k -= cnt[cur] + sz[ch[cur][0]];
if (k <= 0) {
splay(cur);
return val[cur];
}
cur = ch[cur][1];
}
}
}
int prev() {
int cur = ch[root][0];
if (!cur) return cur;
while (ch[cur][1]) cur = ch[cur][1];
splay(cur);
return cur;
}
int next() {
int cur = ch[root][1];
if (!cur) return cur;
while (ch[cur][0]) cur = ch[cur][0];
splay(cur);
return cur;
}
void del(int k) {
rank(k);
if (cnt[root] > 1) {
cnt[root]--;
pushUp(root);
return;
}
if (!ch[root][0] && !ch[root][1]) {
clear(root);
root = 0;
return;
}
if (!ch[root][0]) {
int cur = root;
root = ch[root][1];
fa[root] = 0;
clear(cur);
return;
}
if (!ch[root][1]) {
int cur = root;
root = ch[root][0];
fa[root] = 0;
clear(cur);
return;
}
int cur = root;
int x = prev();
fa[ch[cur][1]] = x;
ch[x][1] = ch[cur][1];
clear(cur);
pushUp(root);
}
} tree;
void work() {
int n, opt, x;
for (scanf("%d", &n); n; --n) {
scanf("%d%d", &opt, &x);
if (opt == 1)
tree.insert(x);
else if (opt == 2)
tree.del(x);
else if (opt == 3)
printf("%d\n", tree.rank(x));
else if (opt == 4)
printf("%d\n", tree.kth(x));
else if (opt == 5)
tree.insert(x), printf("%d\n", tree.val[tree.prev()]), tree.del(x);
else
tree.insert(x), printf("%d\n", tree.val[tree.next()]), tree.del(x);
}
}
|