传送门:HTTP://www.lydsy.com/JudgeOnline/problem.php ID = 3224
注意老版本null_mapped_type新版本null_type
码:
/*
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)
*/
#include<ext/pb_ds/assoc_container.hpp>
#include<bits/stdc++.h>
#include<tr1/unordered_map>
using namespace std;
using namespace std::tr1;
using namespace __gnu_pbds;
typedef long long LL;
tree<LL, null_mapped_type, less<LL>, rb_tree_tag, tree_order_statistics_node_update>::iterator iter;
const int offset = 1e8;
int main()
{
tree<LL, null_mapped_type, less<LL>, rb_tree_tag, tree_order_statistics_node_update> t;
unordered_map<int, int> mp;
int n;
scanf("%d", &n);
while(n--){
int op, x;
scanf("%d %d", &op, &x);
if(op == 1){
x += offset;
t.insert((LL)x << 32 | (mp[x]++));
}
else if(op == 2){
x += offset;
t.erase((LL)x << 32 | (--mp[x]));
}
else if(op == 3){
x += offset;
printf("%d\n", (int)t.order_of_key((LL)x << 32) + 1);
}
else if(op == 4){
int ret = *t.find_by_order(x - 1) >> 32;
ret -= offset;
printf("%d\n", ret);
}
else if(op == 5){
x += offset;
iter = t.lower_bound((LL)x << 32);
iter--;
int ret = *iter >> 32;
ret -= offset;
printf("%d\n", ret);
}
else{
x += offset + 1;
iter = t.lower_bound((LL)x << 32);
int ret = *iter >> 32;
ret -= offset;
printf("%d\n", ret);
}
}
return 0;
}