#include<bits/stdc++.h>
using namespace std;
const int N=100010;
struct Bst{
int lc,rc,cnt,siz,val;
}bst[N];
int tot;
void insertNode(int u, int w) {
// 根节点单独处理
if (tot == 0) {
bst[++tot].val = w;
bst[tot].siz = bst[tot].cnt = 1;
return ;
}
bst[u].siz++;
if (w == bst[u].val) {
bst[u].cnt++;
return ;
}
if (w < bst[u].val) { // 应该在左子树插入
if (bst[u].lc) insertNode(bst[u].lc, w);
else { // 左子树为空,新建结点
bst[++tot].val = w;
bst[tot].cnt = bst[tot].siz = 1;
bst[u].lc = tot;
}
}
else { // 应该在右子树插入
if (bst[u].rc) insertNode(bst[u].rc, w);
else { // 右子树为空,新建结点
bst[++tot].val = w;
bst[tot].cnt = bst[tot].siz = 1;
bst[u].rc = tot;
}
}
}
bool eraseVal(int u, int w) {
if (u == 0) return false;
bool ok = false; // 标记是否成功删除
if (w == bst[u].val) {
if (bst[u].cnt) {
bst[u].cnt--;
ok = true;
}
}
else if (w < bst[u].val) {
if (eraseVal(bst[u].lc, w)) ok = true;
}
else {
if (eraseVal(bst[u].rc, w)) ok = true;
}
if (ok) bst[u].siz--;
return ok;
}
int queryRank(int u, int w) {
if (u == 0) return 2147483647; // 在空树中,w是最小值,即第1小
int lsz = bst[bst[u].lc].siz;
// 类似QueryKth分3种情况讨论
if (bst[u].val == w) return lsz + 1;
if (bst[u].val > w) return queryRank(bst[u].lc, w);
return queryRank(bst[u].rc, w) + lsz + bst[u].cnt;
}
int queryKth(int u, int k) {
if (u == 0) return -2147483647; // 空子树中找排名第k小,返回-1表示无解
int lsz = bst[bst[u].lc].siz; // lsz表示u的左子树数字数量
// 1.第k小在左子树中,往左子树找
if (lsz >= k) return queryKth(bst[u].lc, k);
// 2.第k小在右子树中,右子树的第k-lsz-bst[u].cnt小也就是以u为根的子树的第k小
if (lsz + bst[u].cnt < k) return queryKth(bst[u].rc, k - lsz - bst[u].cnt);
// 3.当前结点权值就是第k小
return bst[u].val;
}
int main(){
int q;
cin>>q;
while(q--){
int op;
cin>>op;
int x;
cin>>x;
if(op==1) cout<<queryRank(1,x)<<endl;
else if(op==2) cout<<queryKth(1,x)<<endl;
else if(op==3) cout<<queryKth(1,queryRank(1,x)-1)<<endl;
else if(op==4) cout<<queryKth(1,queryRank(1,x+1))<<endl;
else if(op==5) insertNode(1,x);
}
return 0;
}
操作四出错了不知道错哪了TwT