Description
您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)
Input
第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)
Output
对于操作3,4,5,6每行输出一个数,表示对应答案
Sample Input
10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598
Sample Output
106465
84185
492737
84185
492737
HINT
1.n的数据范围:n<=100000
2.每个数的数据范围:[-1e7,1e7
可以用vector做
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
vector <int>LI;
int i,xx,x;
for(i=1;i<=n;i++)
{
scanf("%d",&xx);
scanf("%d",&x);
if(xx==1)
LI.insert(upper_bound(LI.begin(),LI.end(),x),x);
else if(xx==2)
LI.erase(lower_bound(LI.begin(),LI.end(),x));
else if(xx==3)
printf("%d\n",lower_bound(LI.begin(),LI.end(),x)-LI.begin()+1);
else if(xx==4)
printf("%d\n",LI[x-1]);
else if(xx==5)
printf("%d\n",*--lower_bound(LI.begin(),LI.end(),x));
else if(xx==6)
printf("%d\n",*upper_bound(LI.begin(),LI.end(),x));
}
return 0;
}