《生活在平衡树上》
pythoner713 · · 个人记录
经测试,三种平衡树的指标如下:
果然
模板 (可供以后复制):
// Splay
#include<bits/stdc++.h>
#define nb 100010
using namespace std;
int n, root, tot, fa[nb], cnt[nb], val[nb], size[nb], son[nb][2];
void update(int x){size[x] = size[son[x][0]] + size[son[x][1]] + cnt[x];}
void rotate(int x){
int y = fa[x], z = fa[y], k = (son[y][1] == x);
son[z][son[z][1] == y] = x, fa[x] = z;
son[y][k] = son[x][!k], fa[son[x][!k]] = y;
son[x][!k] = y, fa[y] = x;
update(x), update(y);
}
void splay(int x, int goal){
while(fa[x] != goal){
int y = fa[x], z = fa[y];
if(z != goal) (son[y][0] == x) ^ (son[z][0] == y) ? rotate(x) : rotate(y);
rotate(x);
}
if(!goal) root = x;
}
void insert(int x){
int f = 0, u = root;
while(u && x != val[u]){
f = u;
u = son[u][x > val[u]];
}
if(u) cnt[u]++;
else{
u = ++tot;
son[f][x > val[f]] = u;
fa[tot] = f;
cnt[tot] = 1;
val[tot] = x;
size[tot] = 1;
}
splay(u, 0);
}
int find_rank(int x){
int u = root;
if(!u) return -1;
while(son[u][x > val[u]] && x != val[u]){
u = son[u][x > val[u]];
}
splay(u, 0);
return size[son[root][0]];
}
int find_num(int x){
int u = root;
if(size[u] < x) return 0;
while(1){
int y = son[u][0];
if(x > size[y] + cnt[u]){
x -= size[y] + cnt[u];
u = son[u][1];
}
else if(x <= size[y]) u = y;
else return val[u];
}
}
int pre(int x, int opt){
find_rank(x);
int u = root;
if((val[u] > x && opt) || (val[u] < x && !opt)) return u;
for(u = son[u][opt]; son[u][!opt];) u = son[u][!opt];
return u;
}
int del(int x){
int xp = pre(x, 0), xn = pre(x, 1);
splay(xp, 0), splay(xn, xp);
int u = son[xn][0];
if(cnt[u] > 1) cnt[u]--, splay(u, 0);
else son[xn][0] = 0;
}
int main(){
ios::sync_with_stdio(0);
insert(-2e9), insert(2e9);
for(cin >> n; n; n--){
int x, opt;
cin >> opt >> x;
if(opt == 1) insert(x);
if(opt == 2) del(x);
if(opt == 3) cout << find_rank(x) << endl;
if(opt == 4) cout << find_num(x + 1) << endl;
if(opt == 5) cout << val[pre(x, 0)] << endl;
if(opt == 6) cout << val[pre(x, 1)] << endl;
}
return 0;
}
// Treap
#include<bits/stdc++.h>
#define nb 100010
using namespace std;
int n, root, tot, cnt[nb], key[nb], val[nb], size[nb], son[nb][2];
void up(int p){size[p] = size[son[p][0]] + size[son[p][1]] + cnt[p];}
void rotate(int &p, int d){
int k = son[p][d ^ 1];
son[p][d ^ 1] = son[k][d];
son[k][d] = p;
up(p), up(k);
p = k;
}
void insert(int &p, int x){
if(!p){
p = ++tot;
val[p] = x;
key[p] = rand();
size[p] = cnt[p] = 1;
return;
}
if(val[p] == x){
cnt[p]++;
size[p]++;
return;
}
int d = x > val[p];
insert(son[p][d], x);
if(key[p] < key[son[p][d]]) rotate(p, d ^ 1);
up(p);
}
void del(int &p, int x){
if(!p) return;
if(x < val[p]) del(son[p][0], x);
if(x > val[p]) del(son[p][1], x);
if(x == val[p]){
if(!son[p][0] && !son[p][1]){
cnt[p]--, size[p]--;
if(!cnt[p]) p = 0;
}
else if(!son[p][0]){
rotate(p, 0);
del(son[p][0], x);
}
else if(!son[p][1]){
rotate(p, 1);
del(son[p][1], x);
}
else{
int d = (key[son[p][0]] > key[son[p][1]]);
rotate(p, d);
del(son[p][d], x);
}
}
up(p);
}
int find_rank(int p, int x){
if(!p) return 0;
if(val[p] == x) return size[son[p][0]] + 1;
if(val[p] > x) return find_rank(son[p][0], x);
else return size[son[p][0]] + cnt[p] + find_rank(son[p][1], x);
}
int find_num(int p, int x){
if(!p) return 0;
if(size[son[p][0]] >= x) return find_num(son[p][0], x);
else if(size[son[p][0]] + cnt[p] < x){
return find_num(son[p][1], x - cnt[p] - size[son[p][0]]);
}
else return val[p];
}
int pre(int p, int x){
if(!p) return -2e9;
if(val[p] >= x) return pre(son[p][0], x);
else return max(val[p], pre(son[p][1], x));
}
int suc(int p, int x){
if(!p) return 2e9;
if(val[p] <= x) return suc(son[p][1], x);
else return min(val[p], suc(son[p][0], x));
}
int main(){
srand(19260817);
ios::sync_with_stdio(0);
for(cin >> n; n; n--){
int opt, x;
cin >> opt >> x;
if(opt == 1) insert(root, x);
if(opt == 2) del(root, x);
if(opt == 3) cout << find_rank(root, x) << endl;
if(opt == 4) cout << find_num(root, x) << endl;
if(opt == 5) cout << pre(root, x) << endl;
if(opt == 6) cout << suc(root, x) << endl;
}
return 0;
}
// FHQ Treap
#include<bits/stdc++.h>
#define nb 100010
using namespace std;
int n, tot, root, key[nb], val[nb], size[nb], son[nb][2];
void up(int p){size[p] = size[son[p][0]] + size[son[p][1]] + 1;}
int new_node(int v){
key[++tot] = rand();
val[tot] = v;
size[tot] = 1;
return tot;
}
void split(int p, int k, int &x, int &y){
if(!p) x = y = 0;
else{
if(val[p] <= k) x = p, split(son[p][1], k, son[p][1], y);
else y = p, split(son[p][0], k, x, son[p][0]);
up(p);
}
}
int merge(int x, int y){
if(!x || !y) return x + y;
if(key[x] < key[y]){
son[x][1] = merge(son[x][1], y);
up(x);
return x;
}
else{
son[y][0] = merge(x, son[y][0]);
up(y);
return y;
}
}
int find_num(int p, int k){
while(1){
if(k <= size[son[p][0]]) p = son[p][0];
else if(k > size[son[p][0]] + 1) k -= size[son[p][0]] + 1, p = son[p][1];
else return val[p];
}
}
int main(){
int x, y, z;
srand(19260817);
ios::sync_with_stdio(0);
for(cin >> n; n; n--){
int opt, v;
cin >> opt >> v;
if(opt == 1){
split(root, v, x, y);
root = merge(merge(x, new_node(v)), y);
}
if(opt == 2){
split(root, v, x, z);
split(x, v - 1, x, y);
y = merge(son[y][0], son[y][1]);
root = merge(merge(x, y), z);
}
if(opt == 3){
split(root, v - 1, x, y);
cout << size[x] + 1 << endl;
root = merge(x, y);
}
if(opt == 4){
cout << find_num(root, v) << endl;
}
if(opt == 5){
split(root, v - 1, x, y);
cout << find_num(x, size[x]) << endl;
root = merge(x, y);
}
if(opt == 6){
split(root, v, x, y);
cout << find_num(y, 1) << endl;
root = merge(x, y);
}
}
return 0;
}