Splay 代码模板

前言

高三时候学的,先把代码摆在这,等有时间再更细的分析这段代码。

代码

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
struct Splay {
int root, cnt, que[MAXN];
Splay() {
root = 0, cnt = 0;
}
struct Node {
int father, leftSon, rightSon;
int value, size;
Node() {
father = 0, leftSon = 0, rightSon = 0;
value = 0, size = 0;
}
}node[MAXN];
void rotateLeft(int num, int fa) {
int &lson = node[num].leftSon;
int val = node[num].size;
node[num].size = node[fa].size;
node[fa].size = node[fa].size - val + node[lson].size;

node[num].father = node[fa].father;
node[fa].rightSon = node[num].leftSon;
if(node[num].leftSon) node[lson].father = fa;
node[num].leftSon = fa;
node[fa].father = num;
if(!node[num].father) return;
if(node[node[num].father].leftSon == fa) node[node[num].father].leftSon = num;
else if(node[node[num].father].rightSon == fa) node[node[num].father].rightSon = num;
return;
}
void rotateRight(int num, int fa) {
int &rson = node[num].rightSon;
int val = node[num].size;
node[num].size = node[fa].size;
node[fa].size = node[fa].size - val + node[rson].size;

node[num].father = node[fa].father;
node[fa].leftSon = node[num].rightSon;
if(node[num].rightSon) node[rson].father = fa;
node[num].rightSon = fa;
node[fa].father = num;
if(!node[num].father) return;
if(node[node[num].father].rightSon == fa) node[node[num].father].rightSon = num;
else if(node[node[num].father].leftSon == fa) node[node[num].father].leftSon = num;
return;
}
inline void sizeUpdate(int num, int opt) {
while(num) {
node[num].size += opt;
num = node[num].father;
}
}
void splay(int num) {
int &fa = node[num].father;
while(node[num].father != root && node[num].father) { //有父结点且父结点不为根结点
if(node[node[fa].father].leftSon == node[num].father) {
if(node[fa].leftSon == num) rotateRight(node[num].father, node[fa].father);
else rotateLeft(num, node[num].father);
rotateRight(num, node[num].father);
}
else {
if(node[fa].rightSon == num) rotateLeft(node[num].father, node[fa].father);
else rotateRight(num, node[num].father);
rotateLeft(num, node[num].father);
}
}
if(!node[num].father) {
root = num; //无父结点
return;
}
if(node[num].father == root){ //有父结点且父结点为根结点
if(node[node[num].father].leftSon == num) rotateRight(num, node[num].father);
else rotateLeft(num, node[num].father);
root = num;
return;
}
}
int find(int val, int s) {
int head = 0, tail = 0, num;
que[tail++] = s;
while(head < tail) {
num = que[head++];
Node &now = node[num];
if(val == now.value) break;
else if(val < now.value) {
if(now.leftSon) que[tail++] = now.leftSon;
else break;
}
else {
if(now.rightSon) que[tail++] = now.rightSon;
else break;
}
}
return num;
}
inline int pre(int num) {
while(node[num].rightSon) num = node[num].rightSon;
return num;
}
inline int nxt(int num) {
while(node[num].leftSon) {
num = node[num].leftSon;
}
return num;
}
inline void insertNode(int val) {
if(!root) {
cnt++;
root = cnt;
node[cnt].size++;
node[cnt].value = val;
return;
}
int s = find(val, root);
if(val == node[s].value) {
splay(s);
node[s].size++;
return;
}
cnt++;
node[cnt].father = s;
node[cnt].value = val;
if(val > node[s].value) node[s].rightSon = cnt;
else node[s].leftSon = cnt;
sizeUpdate(cnt, 1);
splay(cnt);
return;
}
inline void deleteNode(int val) {
int num = find(val, root);
splay(num);
Node &now = node[num];
int tot = now.size - node[now.leftSon].size - node[now.rightSon].size;
if(tot > 1) {
now.size--;
return;
}
if(!now.leftSon) {
now.size = 0;
root = now.rightSon;
node[now.rightSon].father = 0;
now.rightSon = 0;
}
else if(!now.rightSon) {
now.size = 0;
root = now.leftSon;
node[now.leftSon].father = 0;
now.leftSon = 0;
}
else {
int new_root = pre(now.leftSon);
splay(new_root);
sizeUpdate(num, -1);
node[now.rightSon].father = new_root;
node[new_root].rightSon = now.rightSon;
now.father = 0;
now.rightSon = 0;
}
return;
}
inline int getRank() {
return node[node[root].leftSon].size + 1;
}
int rankFind(int rk) {
int head = 0, tail = 0, num;
que[tail++] = root;
while(head < tail) {
num = que[head++];
//cout << rk << " " << node[node[num].leftSon].size << ' ' << head << ' ' << tail << endl;
Node &now = node[num];
if(rk >= node[now.leftSon].size + 1 && rk <= now.size - node[now.rightSon].size) break;
else if(rk <= node[now.leftSon].size) que[tail++] = now.leftSon;
else {
que[tail++] = now.rightSon;
rk -= now.size - node[now.rightSon].size;
}
}
return node[num].value;
}
};