#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct st{
ll d;int ran;
int r=0,l=0;
int rn=0,ln=0,n=0;
}t[100001];
int n,l,tl;
void ins(ll d,int p,int p1){
if(d==t[p].d){
t[p].n++;
}
if(d>t[p].d){
t[p].rn++;
if(t[p].r){
ins(d,t[p].r,p);
}
else{
t[p].r=++tl;t[tl].d=d;
t[tl].ran=rand();t[tl].n++;//随机数
if(t[tl].ran<t[p].ran){
if(t[p1].r==p) t[p1].r=tl;
else t[p1].l=tl;
t[tl].ln=t[p].ln+t[p].rn+t[p].n-1;
t[tl].l=p;
t[p].r=0;
t[p].rn=0;//旋转
}
}
}
else{
t[p].ln++;
if(t[p].l){
ins(d,t[p].l,p);
}
else{
t[p].l=++tl;t[tl].d=d;
t[tl].ran=rand();t[tl].n++;
if(t[tl].ran<t[p].ran){
if(t[p1].r==p) t[p1].r=tl;
else t[p1].l=tl;
t[tl].rn=t[p].ln+t[p].rn+t[p].n-1;
t[tl].r=p;
t[p].l=0;
t[p].ln=0;
}
}
}
return;
}
void del(ll d){
int p=0;
while(d!=t[p].d){
if(d<t[p].d){
t[p].ln--;
p=t[p].l;
}
else{
t[p].rn--;
p=t[p].r;
}
}
t[p].n--;
return;
}
int findn(ll d){
int p=1;int sum=0;
while(p){
if(d<t[p].d){
if(t[p].l) p=t[p].l;
else return sum+1;
}
else{
sum+=t[p].n+t[p].ln;
if(t[p].r) p=t[p].r;
else return sum+1;
}
}
}
int findp(int w){
int p=1;
while(w<=t[p].ln||w>t[p].ln+t[p].n){
if(w<=t[p].ln){
p=t[p].l;
}
else{w-=t[p].n;
w-=t[p].ln;
p=t[p].r;
}
}
return t[p].d;
}
int findfr(ll d){
int p=1;ll d1=0;
while(p){
if(d<t[p].d){
if(t[p].l) p=t[p].l;
else return d1;
}
else{if(d!=t[p].d) d1=max(t[p].d,d1);
if(t[p].r) p=t[p].r;
else return d1;
}
}
return d;
}
int findbl(ll d){
int p=1;ll d1=(ll)1e7+1;
while(p){
if(d<=t[p].d){
if(d!=t[p].d) d1=min(t[p].d,d1);
if(t[p].l) p=t[p].l;
else return d1;
}
else{
if(t[p].r) p=t[p].r;
else return d1;
}
}
return d;
}
int main(){
l=0;tl=0;
cin>>n;ll a,b;
t[0].ran=-(ll)1e7-1;
t[0].d=-(ll)1e7-1;
t[0].n=0;
for(int i=1;i<=n;i++){
cin>>a;
switch(a){
case 1:{
cin>>b;l++;
ins(b,0,0);
break;
}
case 2:{
cin>>b;l--;
del(b);
break;
}
case 3:{
cin>>b;
printf("%lld\n",findn(b));
break;
}
case 4:{
cin>>b;
printf("%lld\n",findp(b));
break;
}
case 5:{
cin>>b;
printf("%lld\n",findfr(b));
break;
}
case 6:{
cin>>b;
printf("%lld\n",findbl(b));
break;
}
}
}
return 0;
}
#1#3下数据了输出没问题 但是评测就WA了