您需要写一种数据结构,来维护一些数( 都是 1 0 9 10^9 109 以内的数字)的集合,最开始时集合是空的。其中需要提供以下操作,操作次数 q q q 不超过 1 0 4 10^4 104:
第一行是一个整数 q q q,表示操作次数。
接下来 q q q 行,每行两个整数 o p , x op,x op,x,分别表示操作序号以及操作的参数 x x x。
输出有若干行。对于操作 1 , 2 , 3 , 4 1,2,3,4 1,2,3,4,输出一个整数,表示该操作的结果。
7
5 1
5 3
5 5
1 3
2 2
3 3
4 3
2
3
1
5
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define endl "\n"
#define int long long
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define gcd __gcd
#define repn(i,a,n) for(int i = a; i <= n; i++)
#define rep(i,a,n) for(int i = a; i < n; i++)
typedef pair<int,int> PII;
const int N = 1000010;
multiset<int> s;
const int INF = 2147483647;
void solve(){
int op,x;
cin>>op>>x;
if(op==1){//查询x数的排名
int num=0;
for(auto i:s)
if(i<x) num++;//注意是<
else break;
cout<<num<<endl;
}else if(op==2){//查询排名为x的数
int num=-1;
for(auto i:s){
num++;
if(num==x){
cout<<i<<endl;
break;
}
}
}else if(op==3){//x的前驱
cout<<*(--s.lb(x))<<endl;
}else if(op==4){//x的后继
cout<<*(s.ub(x))<<endl;
}else{//将x插入集合
s.insert(x);
}
}
signed main(){
IOS;
int T=1;
cin>>T;
s.insert(INF),s.insert(-INF);
while(T--){
solve();
}
return 0;
}