//P5076
#include<bits/stdc++.h>
using namespace std;
int n, root, cnt, opt, x;
#define MAXN 100010
struct Node //一个树中的节点的结构体
{
int left, size, right, num, value;
/*left表示左孩子,right表示右孩子,
size表示以该节点为根节点的子树的节点个数,
num表示该点权值出现的次数,value表示该点的权值*/
Node(int l, int r, int s, int v)
:left(l), size(s), right(r), value(v), num(1){ }
//等效于:left = l, size = s, right = r, value = v, num = 1
Node(){}
};
//构造节点结构体,以及节点变量构造函数
Node t[MAXN];
inline void update(int root)
{
t[root].size = t[t[root].left].size + t[t[root].right].size + t[root].num;
//更新节点信息
}
int frank(int x, int root)//查找数x的排名
{
if (root)
{
if (x < t[root].value)//进入左子树
{
return frank(x, t[root].left);
}
if (x > t[root].value)//进入右子树
{
return frank(x, t[root].right) + t[t[root].left].size + t[root].num;
//左子树所有数均比x小,故应加上子树的根节点与左子树节点数量
}
return t[t[root].left].size + t[root].num;
}
return 1;
}
int search(int x, int root)//查询排名为x的数
{
if (x <= t[t[root].left].size)
{
return search(x, t[root].left);
//排名在x的数在左子树
}
if (x <= t[t[root].left].size + t[root].num)
{
return t[root].value;//正好为根节点
}
return search(x - t[t[root].left].size - t[root].num, t[root].right);//进入右子树,更新为在右子树中的排名
}
void insert(int x, int& root)
{
if (x < t[root].value)//进入左子树
{
if (!t[root].left)//左孩子为空,新建一个左孩子存入
{
t[t[root].left = ++cnt] = Node(0, 0, 1, x);
}
else//递归向下
{
insert(x, t[root].left);
}
}
else if (x > t[root].value)//进入右子树
{
if (!t[root].right)//右孩子为空,新建一个右孩子存入
{
t[t[root].right = ++cnt] = Node(0, 0, 1, x);
}
else//递归向下
{
insert(x, t[root].right);
}
}
else//x的节点已存在,节点size+1
{
t[root].num++;
}
update(root);//更新当前的树的节点的size数据
}
int main()
{
cin >> n;
int num = 0;
t[root = ++cnt] = Node(0, 0, 1, 2147483647);
while (n--)
{
cin >> opt >> x;
num++;
if (opt == 1)cout << frank(x, root) << endl;
else if (opt == 2)cout << search(x, root) << endl;
else if (opt == 3)cout << search(frank(x, root) - 1, root) << endl;
else if (opt == 4)cout << search(frank(x + 1, root), root) << endl;
else num--, insert(x, root);
}
return 0;
}
Subtask 1 ,TLE 了,求条,玄关。