WBLT P3835可持久化平衡树
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
constexpr int N = 5e6 + 10;
constexpr double alpha = 0.29;
int rt[N], crt;
struct WBLT{
#define ls ch[0]
#define rs ch[1]
#define ifleaf(x) (!d[x].ls)
struct node{int ch[2] = {0, 0}, size, val;}d[N];
int tot;
int newnode(int val){return d[++tot].size = 1, d[tot].val = val, tot;}
void pushup(int x){if(!ifleaf(x))d[x].size = d[d[x].ls].size + d[d[x].rs].size, d[x].val = d[d[x].rs].val;}
void copynode(int &i){if(i)d[++tot] = d[i], i = tot;}//
void rotate(int&x, int dir) {//
copynode(x), copynode(d[x].ch[!dir]);//
swap(d[x].ls, d[x].rs);
swap(d[d[x].ch[!dir]].ls, d[d[x].ch[!dir]].rs);
swap(d[d[x].ch[!dir]].ch[!dir], d[x].ch[dir]);
pushup(d[x].ch[!dir]), pushup(x);
}
void maintain(int&now){//
if(ifleaf(now))return;
int k;
if(d[d[now].ls].size < d[now].size * alpha)k = 1;
else if(d[d[now].rs].size < d[now].size * alpha)k = 0;
else return;
if(d[d[d[now].ch[k]].ch[!k]].size >= d[d[now].ch[k]].size * (1 - 2 * alpha) / (1 - alpha))rotate(d[now].ch[k], !k);
rotate(now, k);
}
void insert(int val, int&now){//
copynode(now);//
if(ifleaf(now)){
int s1 = newnode(d[now].val), s2 = newnode(val);
if(d[now].val > val)swap(s1, s2);
d[now].ls = s1, d[now].rs = s2;
return pushup(now);
}
else (d[d[now].ls].val >= val) ? (insert(val, d[now].ls)) : (insert(val, d[now].rs));
return pushup(now), maintain(now);
}
void del(int x, int &now) {//
copynode(now);//
if (d[d[now].ls].val >= x) {
if(ifleaf(d[now].ls)){
if(d[d[now].ls].val != x)return;
now = d[now].rs, copynode(now);//
}
else del(x, d[now].ls);
}
else {
if(ifleaf(d[now].rs)){
if(d[d[now].rs].val != x)return;
now = d[now].ls, copynode(now);//
}
else del(x, d[now].rs);
}
return pushup(now), maintain(now);
}
int queryrank(int x, int now){//
int ans = 0;
while(1){
if(ifleaf(now))return ans + 1;
else now = (d[d[now].ls].val >= x) ? (d[now].ls) : (ans += d[d[now].ls].size, d[now].rs);
}
}
int queryval(int k, int now){//
while(1){
if(ifleaf(now))return (k == 1) ? d[now].val : -1;
else now = (d[d[now].ls].size >= k) ? (d[now].ls) : (k -= d[d[now].ls].size, d[now].rs);
}
}
int ask_pre(int val, int now){//
int ans = -1e9;
while(now){
if(ifleaf(now))return (d[now].val >= val ? ans : d[now].val);
else if(d[d[now].ls].val >= val)now = d[now].ls;
else ans = d[d[now].ls].val, now = d[now].rs;
}
return ans;
}
int ask_next(int val, int now){//
int ans = 0;
while(now){
if(ifleaf(now))return (d[now].val <= val ? ans : d[now].val);
else if(d[d[now].ls].val <= val)now = d[now].rs;
else ans = d[d[now].rs].val, now = d[now].ls;
}
return ans;
}
WBLT(){
rt[0] = newnode(2147483647), d[rt[0]].ls = newnode(-2147483647);
d[rt[0]].rs = newnode(2147483647);
pushup(rt[0]);
}
}T;
int32_t main(){
cin.tie(0)->sync_with_stdio(0);
int q, op, x, root;
cin >> q;
while(q--){
cin >> root >> op >> x;
rt[++crt] = rt[root];
switch(op){
case 1:T.insert(x, rt[crt]);break;
case 2:T.del(x, rt[crt]);break;
case 3:cout << T.queryrank(x, rt[crt]) - 1 << endl;break;
case 4:cout << T.queryval(x + 1, rt[crt]) << endl;break;
case 5:cout << T.ask_pre(x, rt[crt]) << endl;break;
case 6:cout << T.ask_next(x, rt[crt]) << endl;
}
}
return 0;
}
/*
*/