#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int root;
int cnt[N];
int fa[N];
int size[N];
int child[N][2];
int val[N];
void update(int x)
{
if(x)size[x] = cnt[x] + size[child[x][0]] + size[child[x][1]];
}
int son(int x)
{
if(x == child[fa[x]][0])return 0;
return 1;
}
int id = 0;
void rotate(int x)
{
int y = fa[x];
int z = fa[y];
int s = son(x);
child[y][s] = child[x][s ^ 1];
// if(child[x][s ^ 1])
// {
fa[child[x][s ^ 1]] = y;
// }
child[x][s ^ 1] = y;
fa[y] = x;
fa[x] = z;
if(z)
{
child[z][child[z][1] == y] = x;
}
update(y);
update(x);
}
void splay(int x,int y)
{
while(fa[x] != y)
{
if(fa[fa[x]] != y)
{
if(son(x) == son(fa[x]))
{
rotate(fa[x]);
}
else rotate(x);
}
rotate(x);
}
if(!y)root = x;
}
void add(int x)
{
if(!root)
{
root = ++id;
cnt[id] = size[id] = 1;
val[id] = x;
fa[id]=0;
return ;
}
int a = root,b = 0;
while(true)
{
if(val[a] == x)
{
cnt[x]++;
update(a);
update(b);
splay(a,0);
break;
}
b = a;
a = child[a][x > val[b]];
if(a == 0)
{
child[b][x > val[b]] = ++id;
fa[id] = b;
val[id] = x;
cnt[id]++;
update(id);
update(b);
splay(id,0);
break;
}
}
}
int getrank(int num)
{
int ans = 0;
int x = root;
while(x)
{
if(num < val[x])
{
x = child[x][0];
}
ans += size[child[x][0]];
if(num == val[x])
{
splay(x,0);
return ans + 1;
}
ans += cnt[x];
x = child[x][1];
}
return ans + 1;
}
int getnum(int rank)
{
int x = root;
while(true)
{
if(child[x][0] && rank <= size[child[x][0]])
{
x = child[x][0];
}
else if(rank <= size[child[x][0]] + cnt[x])
{
splay(x,0);
return val[x];
}
else
{
rank -= size[child[x][0]] + cnt[x];
x = child[x][1];
}
}
}
int pre(int num)
{
// getrank(num);
int x = child[root][0];
while(child[x][1])
{
x = child[x][1];
}
// splay(x,0);
return val[x];
}
int nxt(int num)
{
// getrank(num);
int x = child[root][1];
while(child[x][0])
{
x = child[x][0];
}
// splay(x,0);
return val[x];
}
void del(int num)
{
getrank(num);
if(cnt[root] > 1)
{
cnt[root]--;
update(root);
}
else if(!child[root][0] && !child[root][1])
{
root = 0;
}
else if(!child[root][1])
{
root = child[root][0];
fa[root]=0;
}
else if(!child[root][0])
{
root = child[root][1];
fa[root]=0;
}
else
{
int x= root;
pre(num);
splay(num,0);
fa[child[x][1]] = root;
child[root][1] = child[x][1];
update(root);
}
}
int main()
{
int t;
cin >> t;
while(t--)
{
int opt,x;
cin >> opt >> x;
if(opt == 1)
{
add(x);
}
else if(opt == 2)
{
del(x);
}
else if(opt == 3)
{
add(x);
cout << getrank(x) << endl;
del(x);
}
else if(opt == 4)
{
cout << getnum(x) << endl;
}
else if(opt == 5)
{
add(x);
cout << pre(x) << endl;
del(x);
}
else if(opt == 6)
{
add(x);
cout << nxt(x) << endl;
del(x);
}
// cout << "#: " << val[root] << " " << size[root] << endl;
}
return 0;
}
3个AC,1个WA,剩下的全都是RE