现在我发现写各种数据结构用指针的好少啊……所以我想写一个指针版的SBT

明明指针版的又好理解又好康又好写又好记啊对吧

迷惑行为

一开始学平衡树的时候学的是替罪羊树……然后后来替罪羊树就愣是很久都没打出来……(虽然替罪羊树qs好写) 后看书的时候各种书上都没有替罪羊树(可能是因为名字就不太✨?)
又不能浪费看书的时间所以就转投了书上有的SBT。

迷惑行为++

第二天上机的时候又忘了一部分就看着别人的写 然后就看到这样一份代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int remove(int &x,int key)
{
int d_key;
tree[x].size--;
if ((key == tree[x].key) || (key < tree[x].key && tree[x].left == 0) ||
(key > tree[x].key && tree[x].right == 0))
{
d_key = tree[x].key;
if (tree[x].left && tree[x].right)
{
tree[x].key = remove(tree[x].left, tree[x].key + 1);
} else
{
x = tree[x].left + tree[x].right;
}
} else if (key > tree[x].key)
d_key = remove(tree[x].right, key);
else if (key < tree[x].key)
d_key = remove(tree[x].left, key);
return d_key;
}

看到这一行了没有:

1
x = tree[x].left + tree[x].right;

感觉好迷惑

两个指针相加……

总之是不知道想干什么

热心的JYC小同学热心的指出了热心的问题

前面有一句这个

1
if (tree[x].left && tree[x].right)

也就是说,这两个left,right至少有一个是零

这一句的值就是非零的那个……

是我太菜了

MODULE解析

定义Node

1
2
3
4
5
6
7
8
9
10
11
struct Node
{
Node *lch, *rch;//左儿子,右儿子
int val, size;//键值,字数
Node(int key)//初始化
{
lch=rch=NULL;
size=1;
val=key;
}
}rot(0);

左旋,右旋

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void L_rotate(Node *x)
{
Node *y = x->lch;
x->rch = y->lch;
y->lch = x;
y->size = x->size;
x->size = x->lch->size + x->rch->size + 1;
x = y;
}
void R_rotate(Node *x)
{
Node *y = x->lch;
x->lch = y->rch;
y->rch = x;
y->size = x->size;
x->size = x->lch->size + x->rch->size + 1;
x = y;
}

maintain

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
void maintain(Node *x,bool flag)
{
if (flag== false)
{
if (x->lch->lch->size>x->rch->size)
{
R_rotate(x);
} else if (x->lch->rch->size>x->rch->size)
{
R_rotate(x->lch);
R_rotate(x);
} else
{
return;
}
} else
{
if (x->rch->rch->size>x->lch->size)
{
L_rotate(x);
} else if (x->rch->lch->size>x->lch->size)
{
R_rotate(x->rch);
R_rotate(x);
} else
{
return;
}
}
maintain(x->lch, false);
maintain(x->rch, true);
maintain(x, true);
maintain(x, false);
}

插♂入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void insert(Node *x,int keys)
{
if (x == NULL)
{
x = new Node(keys);
} else
{
x->size++;
if (keys < x->val)
{
insert(x->lch, keys);
} else
{
insert(x->rch, keys);
}
maintain(x, keys >= x->val);
}
}

删除

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
int remove(Node *x,int keys)
{
int dekey;
--x->size;
if ((keys==x->val)||(keys<x->val&&x->lch==NULL)||(keys>x->val&&x->rch==NULL))
{
dekey=x->val;
if (x->lch&&x->rch)
{
x->val=remove(x->lch,x->val+1);
} else
{
if (x->lch==NULL)
{
x=x->rch;
} else if (x->rch==NULL)
{
x=x->lch;
} else
{
x=NULL;
}
}
} else if (keys>x->val)
{
dekey=remove(x->rch,keys);
} else if (keys<x->val)
{
dekey=remove(x->rch,keys);
}
return dekey;
}

查找最小值

1
2
3
4
5
6
int get_min()
{
Node *x;
for (x = &rot; x->lch; x=x->lch);
return x->val;
}

查找最大值

1
2
3
4
5
6
int get_max()
{
Node *x;
for (x = &rot; x->rch; x=x->rch);
return x->val;
}

查询第K大的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int find_Kth(Node *x,int K)
{
int tr=x->lch->size+1;
if (tr==K)
{
return x->val;
} else if (tr<K)
{
return find_Kth(x->rch,K-tr);
} else
{
return find_Kth(x->lch,K);
}
}

查询值的排名

1
2
3
4
5
6
7
8
9
10
11
int rank(Node *x,int keys)
{
if (keys<x->val)
{
return rank(x->lch,keys);
} else if (keys>x->val)
{
return rank(x->rch,keys);
}
return x->lch->size+1;
}

查询前驱

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int Lowwer(int w)
{
Node *now=rot;
int result=-2147483600;
while(now)
{
if(now->val<w&&now->val>result)
{
result=now->val;
}
if(w>now->val)
{
now=now->rch;
}
else
{
now=now->lch;
}
}
return result;
}

查询后继

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int Upper(int w)
{
Node *now=rot;
int result=2147483600;
while(now)
{
if(now->val>w&&now->val<result)
{
result=now->val;
}
if(w<now->val)
{
now=now->lch;
}
else
{
now=now->rch;
}
}
return result;
}

main

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
int main()
{
totN=read();
register char opt;
register long long K;
for(register long long i=1;i<=totN;++i)
{
opt=getchar();
K=read();
switch (opt)
{
case '1':
insert(rot,K);
break;
case '2':
remove(rot,K);
break;
case '3':
write(ranking(rot,K));
case '4':
write(find_Kth(rot,K));
case '5':
write(Lowwer(K));
case '6':
write(Upper(K));
default:
break;
}
}
return 0;
} // LikiBlaze Code

完整代码(整洁,不压行,代码界清流(WWF狂喜))(368行预警)

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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include <bits/stdc++.h>

using namespace std;

inline long long read()
{
long long x = 0;
int f = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
f = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return x * f;
}
void write(const long long &x)
{
if (!x)
{
putchar('0');
return;
}
char f[100];
long long tmp = x;
if (tmp < 0)
{
tmp = -tmp;
putchar('-');
}
long long s = 0;
while (tmp > 0)
{
f[s++] = tmp % 10 + '0';
tmp /= 10;
}
while (s > 0)
{
putchar(f[--s]);
}
}
inline double dread()
{
double r;
double x = 0, t = 0;
int s = 0, f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
{
if (c == '-')
{
f = -1;
}
if (c == '.')
{
goto readt;
}
}
for (; isdigit(c) && c != '.'; c = getchar())
{
x = x * 10 + c - '0';
}
readt:
for (; c == '.'; c = getchar())
;
for (; isdigit(c); c = getchar())
{
t = t * 10 + c - '0';
++s;
}
r = (x + t / pow(10, s)) * f;
return r;
}

inline void dwrite(long long x)
{
if (x == 0)
{
putchar(48);
return;
}
int bit[20], p = 0, i;
for (; x; x /= 10)
bit[++p] = x % 10;
for (i = p; i > 0; --i)
putchar(bit[i] + 48);
}
inline void write(double x, int k)
{
static int n = pow(10, k);
if (x == 0)
{
putchar('0');
putchar('.');
for (int i = 1; i <= k; ++i)
putchar('0');
return;
}
if (x < 0)
putchar('-'), x = -x;
long long y = (long long)(x * n) % n;
x = (long long)x;
dwrite(x), putchar('.');
int bit[10], p = 0, i;
for (; p < k; y /= 10)
bit[++p] = y % 10;
for (i = p; i > 0; i--)
putchar(bit[i] + 48);
}

struct Node
{
Node *lch, *rch;
int val, size;
Node(int key)
{
lch = rch = NULL;
size = 1;
val = key;
}
};

Node *rot = new Node(0);

void L_rotate(Node *x)
{
Node *y = x->lch;
x->rch = y->lch;
y->lch = x;
y->size = x->size;
x->size = x->lch->size + x->rch->size + 1;
x = y;
}

void R_rotate(Node *x)
{
Node *y = x->lch;
x->lch = y->rch;
y->rch = x;
y->size = x->size;
x->size = x->lch->size + x->rch->size + 1;
x = y;
}

void maintain(Node *x, bool flag)
{
if (flag == false)
{
if (x->lch->lch->size > x->rch->size)
{
R_rotate(x);
}
else if (x->lch->rch->size > x->rch->size)
{
R_rotate(x->lch);
R_rotate(x);
}
else
{
return;
}
}
else
{
if (x->rch->rch->size > x->lch->size)
{
L_rotate(x);
}
else if (x->rch->lch->size > x->lch->size)
{
R_rotate(x->rch);
R_rotate(x);
}
else
{
return;
}
}
maintain(x->lch, false);
maintain(x->rch, true);
maintain(x, true);
maintain(x, false);
}

void insert(Node *x, int keys)
{
if (x == NULL)
{
x = new Node(keys);
}
else
{
x->size++;
if (keys < x->val)
{
insert(x->lch, keys);
}
else
{
insert(x->rch, keys);
}
maintain(x, keys >= x->val);
}
}

int remove(Node *x, int keys)
{
int dekey;
--x->size;
if ((keys == x->val) || (keys < x->val && x->lch == NULL) || (keys > x->val && x->rch == NULL))
{
dekey = x->val;
if (x->lch && x->rch)
{
x->val = remove(x->lch, x->val + 1);
}
else
{
if (x->lch == NULL)
{
x = x->rch;
}
else if (x->rch == NULL)
{
x = x->lch;
}
else
{
x = NULL;
}
}
}
else if (keys > x->val)
{
dekey = remove(x->rch, keys);
}
else if (keys < x->val)
{
dekey = remove(x->rch, keys);
}
return dekey;
}

int get_min()
{
Node *x;
for (x = rot; x->lch; x = x->lch)
;
return x->val;
}
int get_max()
{
Node *x;
for (x = rot; x->rch; x = x->rch)
;
return x->val;
}
int find_Kth(Node *x, int K)
{
int tr = x->lch->size + 1;
if (tr == K)
{
return x->val;
}
else if (tr < K)
{
return find_Kth(x->rch, K - tr);
}
else
{
return find_Kth(x->lch, K);
}
}
int ranking(Node *x, int keys)
{
if (keys < x->val)
{
return ranking(x->lch, keys);
}
else if (keys > x->val)
{
return ranking(x->rch, keys);
}
return x->lch->size + 1;
}

int Upper(int w)
{
Node *now = rot;
int result = 2147483600;
while (now)
{
if (now->val > w && now->val < result)
{
result = now->val;
}
if (w < now->val)
{
now = now->lch;
}
else
{
now = now->rch;
}
}
return result;
}
int Lowwer(int w)
{
Node *now = rot;
int result = -2147483600;
while (now)
{
if (now->val < w && now->val > result)
{
result = now->val;
}
if (w > now->val)
{
now = now->rch;
}
else
{
now = now->lch;
}
}
return result;
}

long long totN;

int main()
{
totN = read();
register char opt;
register long long K;
for (register long long i = 1; i <= totN; ++i)
{
opt = getchar();
K = read();
switch (opt)
{
case '1':
insert(rot, K);
break;
case '2':
remove(rot, K);
break;
case '3':
write(ranking(rot, K));
case '4':
write(find_Kth(rot, K));
case '5':
write(Lowwer(K));
case '6':
write(Upper(K));
default:
break;
}
}
return 0;
} // LikiBlaze Code