P3369 【模板】普通平衡树
题目描述
您需要动态地维护一个可重集合
- 向
中插入一个数 。 - 从
中删除一个数 (若有多个相同的数,应只删除一个)。 - 查询
中有多少个数比 小,并且将得到的答案加一。 - 查询如果将
从小到大排列后,排名位于第 位的数。 - 查询
中 的前驱(前驱定义为小于 ,且最大的数)。 - 查询
中 的后继(后继定义为大于 ,且最小的数)。
对于操作 3,5,6,不保证当前可重集中存在数
对于操作
输入格式
第一行为
输出格式
对于操作
输入输出样例 #1
输入 #1
10
1 106465
4 1
1 317721
1 460929
1 644985
1 84185
1 89851
6 81968
1 492737
5 493598输出 #1
106465
84185
492737说明/提示
【数据范围】
对于
来源:Tyvj1728 原名:普通平衡树
在此鸣谢
solve
pb_ds版
排名是从
c++
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using i64 = long long;
using namespace __gnu_pbds;
typedef tree<std::pair<int, int>, null_type, std::less<std::pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> Tree;
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
Tree tr;
std::map<int, int> dic;
while (n--) {
int opt, x;
std::cin >> opt >> x;
if (opt == 1) {
tr.insert({x, ++dic[x]});
} else if (opt == 2) {
tr.erase({x, dic[x]--});
} else if (opt == 3) { // 查询元素 x 的排名 (比当前数小的个数 + 1)
std::cout << tr.order_of_key({x, 1}) + 1 << "\n";
} else if (opt == 4) { // 查询排名为 x 的元素
std::cout << tr.find_by_order(x - 1)->first << "\n";
} else if (opt == 5) { // 查找 x 的前驱
int idx = tr.order_of_key({x, 1}) - 1; // 无论x存不存在都要 -1 排名从0开始所以要 -1
std::cout << tr.find_by_order(idx)->first << "\n";
} else { // 查找 x 的后继
int idx = tr.order_of_key({x, dic[x]}); // 如果 x 不存在 那么idx就是x的后继
if (tr.find({x, 1}) != tr.end()) { // 如果 x 存在 那么idx是x的位置要 +1
idx++;
}
// 求排名要 +1
std::cout << tr.find_by_order(idx)->first << "\n";
}
}
return 0;
}