https://www.luogu.com.cn/record/156725415
#include<iostream>
#include<cstdlib>
#include<ctime>
#define N 100005
#define inf 0x3fffffff
#define lson(x) (t[x].ls)
#define rson(x) (t[x].rs)
#define key(x) (t[x].key)
#define siz(x) (t[x].siz)
#define val(x) (t[x].val)
using namespace std;
// key rand()!!
struct FHQ_Treap{
int ls, rs, siz, key, val;
} t[N];
int tot, root;
void push_up(int x){
siz(x) = siz(lson(x)) + siz(rson(x)) + 1;
}
void split(int u, int val, int &x, int &y){
if(!u) {x = y = 0; return ;}
if(val(u) <= val) {x = u; split(rson(u), val, rson(u), y);}
else {y = u; split(lson(u), val, x, lson(u));}
push_up(u);
}
// ^
int merge(int x, int y){
if(x == 0 || y == 0) return x+y;
if(key(x) > key(y)) {
rson(x) = merge(rson(x), y);
push_up(x);
return x;
}
else {
lson(y) = merge(x, lson(y));
push_up(y);
return y;
}
}
int newnode(int val){
key(++tot) = rand();
lson(tot) = rson(tot) = 0;
siz(tot) = 1;
val(tot) = val;
return tot;
}
int insert(int val){
int x, y;
split(root, val, x, y);
int u = newnode(val);
root = merge(merge(x, u), y);
return tot;
}
int del(int val){
int x, mid, y;
split(root, val, x, y);
split(x, val-1, x, mid);
mid = merge(lson(mid), rson(mid));
root = merge(merge(x, mid), y);
return root;
}
//11, 10, 9, 10, 643621
int kth(int k){
// ++k;
int now = root;
while(1){
if(k <= siz(lson(now)) && lson(now)) now = lson(now);
else if(k > siz(lson(now))+1) {
now = rson(now);
k -= siz(lson(now))+1;
}
else return (now);
}
}
int rnk(int val){
int x, y, ans;
split(root, val-1, x, y);
ans = siz(x)+1;
root = merge(x, y);
return ans;
}
int pre(int val){
int _x, y;
split(root, val-1, _x, y);
int x = _x;
while(rson(x)) x = rson(x);
root = merge(_x, y);
return val(x);
}
int last(int val)
{
int x, _y;
split(root, val, x, _y);
int y = _y;
while(lson(y)) y = lson(y);
root = merge(x, _y);
return val(y);
}
int main(){
// freopen("mine.out", "w", stdout);
srand(time(0));
// insert(inf);
// insert(-inf);
int n;
scanf("%d", &n);
while(n--){
int op, x;
scanf("%d%d", &op, &x);
if(op == 1) insert(x);
else if(op == 2) del(x);
else if(op == 3) printf("%d\n", rnk(x));
else if(op == 4) printf("%d\n", val(kth(x)));
else if(op == 5) printf("%d\n", pre(x));
else printf("%d\n", last(x));
}
return 0;
}