题目链接:
名次树:Luogu P3369 【模板】普通平衡树
区间翻转:Luogu P3391 【模板】文艺平衡树(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
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
#include <cstdlib>
#include <iostream>
#include <algorithm>

class Splay {
public:
Splay() : rt(new BTNode(-2147483647)), root(new BTNode(0, rt)), sz(0), tot(1) {
root->set(rt, right);
root->cnt = root->sz = 0;
}
int size() {return sz;}
int cap() {return tot;}
void insert(int key) {
iterator it = add(root, key);
it->splay(root), ++sz;
root = it;
}
void erase(int key) {
iterator it = find(key);
move(it), --sz;
}
int rankOfKey(int key) {
int res = 0;
iterator it = root;
while (it) {
if (it->key == key) {
if (it->son[left])
res += it->son[left]->sz;
break;
} else if (it->key > key) {
it = it->son[left];
} else {
res += it->cnt;
if (it->son[left])
res += it->son[left]->sz;
it = it->son[right];
}
}
if (it) it->splay(root), root = it;
return res + 1;
}
int findByRank(int rank) {
if (rank > sz || rank < 1)
return -2147483648;
iterator it = root;
while (it) {
int temp = 0;
if (it->son[left])
temp += it->son[left]->sz;
if (rank > temp && rank <= temp + it->cnt)
break;
else if (rank <= temp)
it = it->son[left];
else
rank -= temp + it->cnt,
it = it->son[right];
}
it->splay(root), root = it;
return it->key;
}
void debug() {
deb(root);
}
int previous(int val) {
insert(val);
iterator it = find(val)->son[left];
while (it && it->son[right])
it = it->son[right];
if (!it) return -2147483646;
int res = it->key;
erase(val);
return res;
}
int next(int val) {
insert(val);
iterator it = find(val)->son[right];
while (it && it->son[left])
it = it->son[left];
if (!it) return 2147483647;
int res = it->key;
erase(val);
return res;
}
private:
static const int clock_rotate = 0, antl_rotate = 1;
static const int left = 0, right = 1;
struct BTNode {
typedef BTNode *iterator;
iterator son[2], fa;
int key, sz, cnt;
static const int left = 0, right = 1;
//构造函数
BTNode(int key, iterator father = nullptr) {
this->key = key;
this->fa = father;
this->son[right] = this->son[left] = nullptr;
this->sz = this->cnt = 1;
}
//在操作以后更新当前节点对应的子树大小
inline void update() {
sz = cnt;
if (son[left]) sz += son[left]->sz;
if (son[right]) sz += son[right]->sz;
}
//确定当前节点是父节点的左子树还是右子树
inline int identify() {
return fa->son[right] == this;
}
/*建立父子关系
mode表示将 this 建成 it 的哪个儿子*/
inline void set(iterator it, int mode) {
if (this) this->fa = it;
if (it) it->son[mode] = this;
}
//自适应旋转(基础操作)
inline void rotate() {
int mode = this->identify(), pos = this->fa->identify();
iterator son_node = this->son[mode ^ 1], fa_node = this->fa;
iterator fa_node_fa = fa_node->fa;
son_node->set(fa_node, mode);
fa_node->set(this, mode ^ 1);
this->set(fa_node_fa, pos);
fa_node->update(), this->update();
}
/*上旋节点(伸展)
把 this 旋转为 it*/
void splay(iterator it) {
if (this == it)
return;
it = it->fa;
while (this->fa != it) {
iterator father = this->fa;
if (father->fa == it)
this->rotate();
else if (this->identify() == father->identify())
father->rotate(), this->rotate();
else
this->rotate(), this->rotate();
}
}
} *rt, *root;
typedef BTNode *iterator;
int sz, tot;
//查找元素,返回迭代器
iterator find(int val) {
if (!sz) return nullptr;
iterator it = root;
while (it) {
if (it->key == val)
break;
else if (it->key > val)
it = it->son[left];
else
it = it->son[right];
}
if (it) it->splay(root), root = it;
return it;
}
//内部实现:插入元素
iterator add(iterator it, int key) {
if (!sz) {
it->key = key;
it->cnt = 1;
it->update();
return it;
} else if (key == it->key) {
++(it->cnt);
it->update();
return it;
} else {
int mode = int (key > it->key);
iterator res;
if (!(it->son[mode]))
res = it->son[mode] = new BTNode(key, it),
++tot;
else
res = add(it->son[mode], key);
it->update();
return res;
}
}
//内部实现:删除元素
void move(iterator it) {
if (!it || --(it->cnt))
return;
if (!(it->son[left]) && !(it->son[right])) {
if (tot > 1)
it->fa->son[it->identify()] = nullptr,
it->fa->update(), delete it, --tot;
else
it->sz = it->cnt = 0,
it->key = 0;
return;
}
it->splay(root), root = it;
if (!it->son[left]) {
it->son[right]->set(rt, right),
root = it->son[right], delete it, --tot;
} else {
iterator pos = it->son[left];
while (pos->son[right])
pos = pos->son[right];
pos->splay(root->son[left]);
root->son[right]->set(pos, right);
pos->set(rt, right), root = pos, delete it, --tot;
}
}
//通过中序遍历来帮忙查错
void deb(iterator it) {
if (it->son[left])
deb(it->son[left]);
std::cout << "--- " << it->key << std::endl;
if (it->son[right])
deb(it->son[right]);
}
};

int main() {
std::ios::sync_with_stdio(false);
int n;
Splay s;
std::cin >> n;
while (n--) {
int op, v;
std::cin >> op >> v;
if (op == 1) {
s.insert(v);
} else if (op == 2) {
s.erase(v);
} else if (op == 3) {
std::cout << s.rankOfKey(v) << std::endl;
} else if (op == 4) {
std::cout << s.findByRank(v) << std::endl;
} else if (op == 5) {
std::cout << s.previous(v) << std::endl;
} else if (op == 6) {
std::cout << s.next(v) << std::endl;
} else {
std::cout << "----------------DEBUG------------------" << std::endl;
s.debug(), std::cout << "The size of the tree is: " << s.size() << std::endl;
std::cout << "The count of the nodes is: " << s.cap() << std::endl;
std::cout << "--------------DEBUG END----------------" << std::endl;
}
}
return 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
#include <iostream>
#include <algorithm>

struct BTNode;
typedef BTNode *iterator;

// O2情况下不允许把空指针当作参数 this 传至成员函数中
#define setFather(this, fa, mode) \
if (this) this->father = fa; \
if (fa) fa->son[mode] = this;

struct BTNode {
int key, sz, cnt;
bool in_array, rev;
iterator son[2], father;
BTNode(int key, bool in_array) {
sz = 1, cnt = 1, rev = false;
son[0] = son[1] = father = 0;
this->key = key, this->in_array = in_array;
}

int identify() {
return int(this == father->son[1]);
}
void update() {
this->sz = this->cnt;
if (son[0]) this->sz += son[0]->sz;
if (son[1]) this->sz += son[1]->sz;
}
void rotate() {
if (!father || !(father->father))
return;
iterator father = this->father, grandpa = father->father;
int pos_father = this->identify(), pos_grandpa = father->identify();
iterator son = this->son[pos_father ^ 1];
setFather(this, grandpa, pos_grandpa);
setFather(son, father, pos_father);
setFather(father, this, pos_father ^ 1);
father->update(), this->update();
}
void splay(iterator to) {
if (this == to) return;
to = to->father;
while (this->father != to) {
iterator upper = this->father;
if (upper->father == to)
this->rotate();
else if (this->identify() == upper->identify())
upper->rotate(), this->rotate();
else
this->rotate(), this->rotate();
}
}
void pushDown() {
if (!(this->rev)) return;
if (son[0]) son[0]->rev ^= 1;
if (son[1]) son[1]->rev ^= 1;
this->rev = false;
std::swap(son[0], son[1]);
}
};

iterator root, rt;
int n, m;

void build() {
iterator it = root;
for (int i = n; i >= 0; --i) {
it->son[0] = new BTNode(i, bool(i));
setFather(it->son[0], it, 0);
it = it->son[0];
}
while (it != rt)
it->update(),
it = it->father;
}

iterator findByRank(int rank) {
iterator it = root;
while (it) {
int temp = 0;
it->pushDown();
if (it->son[0])
temp += it->son[0]->sz;
if (rank <= temp)
it = it->son[0];
else if (rank <= temp + it->cnt)
break;
else
rank -= temp + it->cnt,
it = it->son[1];
}
return it;
}

void init() {
rt = new BTNode(19260817, false);
rt->cnt = rt->sz = 0;
rt->son[0] = new BTNode(n + 1, false);
setFather(rt->son[0], rt, 0); root = rt->son[0];
build();
}

void modify(int l, int r) {
r += 2;
iterator left = findByRank(l), right = findByRank(r);
// std::cout << left->key << ' ' << right->key << std::endl;
left->splay(root), root = left;
right->splay(root->son[1]);
iterator it = right->son[0];
it->rev ^= 1;
}

void printAns(iterator pos) {
pos->pushDown();
if (pos->son[0])
printAns(pos->son[0]);
if (pos->in_array)
std::cout << pos->key << ' ';
if (pos->son[1])
printAns(pos->son[1]);
}

int main() {
std::cin >> n >> m;
init();
int l, r;
while (m--) {
std::cin >> l >> r;
modify(l, r);
}
printAns(root);
return 0;
}

 评论