题目描述
您需要写一种数据结构,来维护一些数( 都是 10^9
以内的数字)的集合,最开始时集合是空的。其中需要提供以下操作,操作次数 qq 不超过 10^4
1.查询 x数的排名(排名定义为比当前数小的数的个数 +1+1。若有多个相同的数,因输出最小的排名)。
2.查询排名为 x 的数。
3.求 x 的前驱(前驱定义为小于 x,且最大的数)。若未找到则输出 -2147483647
4.求 x 的后继(后继定义为大于 x,且最小的数)。若未找到则输出 2147483647
5.插入一个数 x。
输入格式
无
输出格式
无
输入输出样例
输入
7
5 1
5 3
5 5
1 3
2 2
3 3
4 3
输出
2
3
1
5
法一:
这玩意要首先是感觉能用STL呀,C++中multiset可以玩完美解决题;
各种方法的返回值自己C++书去吧,或者直接CSDN上看dalao总结的一大堆
AC代码:
#include<iostream>
#include<algorithm>
#include<set>
#include<iterator>
using namespace std;
multiset<int>q;
int main(){
int n,t,x;
q.insert(-0x7fffffff);
q.insert(0x7fffffff);
cin>>n;
while(n--){
cin>>t>>x;
int order;
if(t==1){
multiset<int>::iterator it=q.lower_bound(x);
order=0;
for(multiset<int>::iterator i=q.begin();i!=it;i++,order++);
cout<<order<<endl;
}
if(t==2){
order=-1;
for(multiset<int>::iterator i=q.begin();i!=q.end();i++){
order++;
if(order==x)
cout<<*i<<endl;
}
}
if(t