C++#include<bits/stdc++.h>
#define int long long
int n,m;
//FHQ_Treap
namespace FHQ_Treap{
const int N=1e5+10;
struct Tree_Node{
int value,left,right,priorit,size;
}t[N];
int seed=8954899789;
int root=0,tot=0;
int random(){
return seed = seed * 114514191981 % 6789589747565;
}
void pushup(int x){
t[x].size=t[t[x].left].size+t[t[x].right].size+1;
}
int create_node(int v){
t[++tot].size=1,
t[tot].value=v;
t[tot].left=t[tot].right=0;
t[tot].priorit=random();
return tot;
}
int merge(int x,int y){
if(x==0||y==0)
return x+y;
if(t[x].priorit >= t[y].priorit){
t[x].right = merge(t[x].right,y),
pushup(x);
return x;
}
else{
t[y].left=merge(x,t[y].left),
pushup(y);
return y;
}
}
void split(int root,int &x,int &y,int value){
if(root==0){
x=y=0;
return;
}
if(t[root].value<=value)
x=root,
split(t[root].right,t[x].right,y,value);
else
y=root,
split(t[root].left,x,t[y].left,value);
pushup(root);
}
void insert(int v){
int x=0,y=0;
split(root,x,y,v-1);
root=merge(merge(x,create_node(v)),y);
}
void del(int v){
int x=0,y=0,z;
split(root,y,z,v);
split(y,x,y,v-1);
y=merge(t[y].left,t[y].right);
root=merge(x,merge(y,z));
}
void find_rank(int v){
int x=0,y=0;
split(root,x,y,v-1);
std::cout<<t[x].size+1<<"\n";
root=merge(x,y);
}
void find_num(int k){
int o=root;
while(o){
int x=t[t[o].left].size+1;
if(k < x)
o=t[o].left;
else if(k == x){
std::cout<<t[o].value<<"\n";
return;
}
else
k-=x,
o=t[o].right;
}
}
void pre(int v){
int x=0,y=0;
split(root,x,y,v-1);
int o=x;
while(t[o].right) o=t[o].right;
std::cout<<t[o].value<<"\n";
root=merge(x,y);
}
void nxt(int v){
int x=0,y=0;
split(root,x,y,v);
int o=y;
while(t[o].left) o=t[o].left;
std::cout<<t[o].value<<"\n";
root=merge(x,y);
}
}
using namespace FHQ_Treap;
using namespace std;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
cin>>n;
while(n--){
int op,x;
cin>>op>>x;
switch(op){
case 1:
insert(x);
break;
case 2:
del(x);
break;
case 3:
find_rank(x);
break;
case 4:
find_num(x);
break;
case 5:
pre(x);
break;
case 6:
nxt(x);
break;
}
}
return 0;
}