RBT P3369普通平衡树
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
#define FL(a, b, c) for(int a = (b), a##end = (c); a <= a##end; a++)
#define FR(a, b, c) for(int a = (b), a##end = (c); a >= a##end; a--)
#define lowbit(x) ((x) & -(x))
constexpr int N = 1e5 + 10;
struct RBT{
struct node{
int val, size, fa, ch[2];
bool col;
}d[N];
#define ls(x) d[x].ch[0]
#define rs(x) d[x].ch[1]
#define fa(x) d[x].fa
int root, tot = 0, stk[N], top = 0;
void pushup(int x){d[x].size = 1 + d[ls(x)].size + d[rs(x)].size;}
#define delnode(x) stk[++top] = x //垃圾回收
#define get(x) (x == rs(fa(x)))
int newnode(int val){
int w = top ? stk[top--] : ++tot;
d[w].val = val, d[w].col = d[w].size = 1;
d[w].col = 1, fa(w) = ls(w) = rs(w) = 0;
return w;
}
void rotate(int x){
int y = fa(x), z = fa(y), c = get(x);
if(d[x].ch[!c])fa(d[x].ch[!c]) = y;
d[y].ch[c] = d[x].ch[!c], d[x].ch[!c] = y, fa(y) = x, fa(x) = z;
if(z) d[z].ch[y == d[z].ch[1]] = x;
else root = x;
pushup(y), pushup(x);
}
void insert(int val){
int now = root, f = 0, gf = 0;
while(now) d[f = now].size++, now = d[now].ch[val > d[now].val];
now = newnode(val), fa(now) = f, d[f].ch[val > d[f].val] = now;
while(d[gf = fa(f = fa(now)), f].col){
int w = !get(f), ul = d[gf].ch[w];
if(d[ul].col)d[f].col = d[ul].col = 0, d[gf].col = 1, now = gf;
else{
if(get(now) == w)rotate(now), swap(now, f);
d[gf].col = 1, d[f].col = 0, rotate(f);
break;
}
}
d[root].col = 0;
}
void del_fix(int now) {
while(now != root && !d[now].col) {
bool w = !get(now);
int f = fa(now), ul = d[f].ch[w];
if(d[ul].col)d[ul].col = 0, d[f].col = 1, rotate(ul);
else if(!d[ls(ul)].col && !d[rs(ul)].col)d[ul].col = 1, now = f;
else{
if(!d[d[ul].ch[w]].col){
d[d[ul].ch[!w]].col = 0, d[ul].col = 1;
rotate(d[ul].ch[!w]), ul = d[f].ch[w];
}
d[ul].col = d[f].col, d[d[ul].ch[w]].col = d[f].col = 0;
rotate(ul);
break;
}
}
d[now].col = 0;
}
void del(int v) {
int now = root, f = 0;
while(now && d[now].val != v)now = d[now].ch[v > d[now].val];
if(!now)return;
int w = now, g = 0;
if(ls(now) && rs(now)){
w = rs(now);
while(ls(w)) w = ls(w);
}
g = d[w].ch[!ls(w)];
fa(g) = fa(w);
if(!fa(w))root = g;
else d[fa(w)].ch[get(w)] = g;
d[now].val = d[w].val;
for(int i = fa(w); i; i = fa(i))d[i].size--;
if(!d[w].col) del_fix(g);
stk[++top] = w;
}
int kth(int x){
int now = root, siz = 0, z = x;
while(now){
if((siz = d[ls(now)].size + 1) == x)return d[now].val;
else now = ((siz > x) ? ls(now) : (x -= siz, rs(now)));
}
return -1;
}
int query_rank(int val){
int ans = 1, now = root;
while(now)
if(d[now].val < val)ans += d[ls(now)].size + 1, now = rs(now);
else now = ls(now);
return ans;
}
int ask_pre(int val){
int now = root, ans = -1e9;
while(now)
if(d[now].val >= val)now = ls(now);
else ans = d[now].val, now = rs(now);
return ans;
}
int ask_next(int val){
int now = root, ans = 1e9;
while(now){
if(d[now].val <= val)now = rs(now);
else ans = d[now].val, now = ls(now);
}
return ans;
}
RBT(){
root = newnode(INT_MAX);
d[root].col = 0;
}
}T;
int32_t main(){
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
while(n--){
int op, x;
cin >> op >> x;
switch(op){
case 1:T.insert(x);break;
case 2:T.del(x);break;
case 3:cout << T.query_rank(x) << endl;break;
case 4:cout << T.kth(x) << endl;break;
case 5:cout << T.ask_pre(x) << endl;break;
case 6:cout << T.ask_next(x) << endl;break;
}
}
return 0;
}