
1 Description 2 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作: 3 1.插入 x 数 4 2.删除 x 数(若有多个相同的数,应只删除一个) 5 3.查询 x 数的排名(排名定义为比当前数小的数的个数 。若有多个相同的数,因输出最小的排名) 6 4.查询排名为 x 的数 7 5.求 x 的前驱(前驱定义为小于 x ,且最大的数) 8 6.求 x 的后继(后继定义为大于 x ,且最小的数) 9 Input 10 第一行为 n ,表示操作的个数,下面 n 行每行有两个数 opt 和 x , opt 表示操作的序号( 1≤opt≤6 ) 11 Output 12 对于操作 3,4,5,6 每行输出一个数,表示对应答案 13 Sample Input 14 10 15 1 106465 16 4 1 17 1 317721 18 1 460929 19 1 644985 20 1 84185 21 1 89851 22 6 81968 23 1 492737 24 5 493598 25 Sample Output 26 106465 27 84185 28 492737 29 Hint 30 时空限制:1000ms,128M 31 1.n的数据范围: n≤100000 32 2.每个数的数据范围: [−10^7,10^7]

1 #include<bits/stdc++.h>
2 using namespace std;
3 int n,op,x;
4 vector<int>a;
5 int main()
6 {
7 scanf("%d",&n);
8 while(n--)
9 {
10 scanf("%d%d",&op,&x);
11 if(op==1) a.insert(lower_bound(a.begin(),a.end(),x),x);
12 if(op==2) a.erase(lower_bound(a.begin(),a.end(),x));
13 if(op==3) cout<<lower_bound(a.begin(),a.end(),x)-a.begin()+1<<endl;
14 if(op==4) cout<<a[x-1]<<endl;
15 if(op==5) cout<<a[lower_bound(a.begin(),a.end(),x)-a.begin()-1]<<endl;
16 if(op==6) cout<<a[upper_bound(a.begin(),a.end(),x)-a.begin()]<<endl;
17 }
18 return 0;
19 }
