#define maxn 10010
using namespace std;
struct node{
long long left=0;
long long right=0;
long long s=1;
long long v;
long long q=0;
long long zy=-1;
}t[100000];
int ans;
int root1=0;
int bh1=0;
long long sans[100000];
void inserts(int root,long long v,int bh){
if(root==0){
root1=bh;
t[bh].v=v;
}
else{
if(v>t[root].v){
if(t[root].right==0){
t[root].s++;
t[root].right=bh;
t[bh].q=root;
t[bh].v=v;
t[bh].zy=1;//1是右子树
}
else {
t[root].s++;
inserts(t[root].right,v,bh);
}
}
else{
if(t[root].left==0){
t[root].s++;
t[root].left=bh;
t[bh].q=root;
t[bh].v=v;
t[bh].zy=0;//0是左子树
}
else {
t[root].s++;
inserts(t[root].left,v,bh);
}
}
}
}
void fh(int x,int root){
if(x<t[root].v){
if(t[root].left!=0){
int jl=ans;
fh(x,t[root].left);
if(ans>jl&&sans[ans]==2147483647){
sans[ans]=t[root].v;
}
}
else {
ans++;
sans[ans]=t[root].v;
return;
}
}
if(x>=t[root].v){
if(t[root].right!=0){
fh(x,t[root].right);
}
else {
if(t[root].zy==1){
ans++;
sans[ans]= 2147483647;
return ;
}
if(t[root].zy==0){
ans++;
sans[ans]=t[t[root].q].v;
return;
}
if(t[root].zy==-1){
ans++;
sans[ans]=2147483647;
return;
}
}
}
}
void fq(int x,int root){
if(x>t[root].v){
if(t[root].right!=0){
int jl=ans;
fq(x,t[root].right);
if(ans>jl&&sans[ans]==-2147483647){
sans[ans]=t[root].v;
}
}
else {
ans++;
sans[ans]=t[root].v;
return;
}
}
if(x<=t[root].v){
if(t[root].left!=0){
fq(x,t[root].left);
}
else {
if(t[root].zy==0){
ans++;
sans[ans]= -2147483647;
return ;
}
if(t[root].zy==1){
ans++;
sans[ans]=t[t[root].q].v;
return;
}
if(t[root].zy==-1){
ans++;
sans[ans]=-2147483647;
return;
}
}
}
}
int yzs(int x){
while(t[x].right!=0){
x=t[x].right;
}
return x;
}
void fpm(int x,int now,int root){
if(now+1==x&&!t[root].left){
ans++;
sans[ans]=t[root].v;
return;
}
else{
if(t[root].left){
if(t[t[root].left].s<x-now){
now+=t[t[root].left].s;
if(now+1==x){
ans++;
sans[ans]=t[root].v;
return;
}
fpm(x, now+1,t[root].right);
}
else if(t[t[root].left].s>x-now){
fpm(x,now,t[root].left);
}
else if(t[t[root].left].s==x-now){
ans++;
sans[ans]=t[yzs(t[root].left)].v;
}
}
else fpm(x, now+1,t[root].right);
}
}
void spm(int x,int root,int sum){
if(t[root].v<x){
if(t[root].left!=0){
sum+=t[t[root].left].s;
sum++;
if(t[root].right!=0){
spm(x,t[root].right,sum);
}
else {
ans++;
sans[ans]=sum+1;
return;
}
}
else {
if(t[root].right!=0){
spm(x,t[root].right,sum+1);
}
else {
ans++;
sans[ans]=sum+1;
return;
}
}
}
if(t[root].v>=x){
if(t[root].left!=0){
spm(x,t[root].left,sum);
}
else {
ans++;
sans[ans]=sum;
return;
}
}
}
int main(){
int q;
cin>>q;
for(int i=1;i<=q;i++){
int op,x;
cin>>op>>x;
switch(op){
case 1:spm(x,root1,1);break;
case 2:fpm(x,0,root1);break;
case 3:fq(x,root1);break;
case 4:fh(x,root1);break;
case 5:bh1++;inserts(root1,x,bh1);break;
};
}
// for(int i=1;i<=bh1;i++){
// cout<<t[i].v<<" "<<t[t[i].left].v<<" "<<t[t[i].right].v<<" "<<t[i].s<<" "<<t[t[i].q].v<<" "<<t[i].zy;
// cout<<endl;
// }
for(int i=1;i<=ans;i++){
cout<<sans[i]<<endl;
}
return 0;
} ```