頭文件
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//或者直接用
#include <bits/extc++.h>
命名空間
using namespace __gnu_pbds;
定義
tree<double, null_type, greater<double>, rb_tree_tag, tree_order_statistics_node_update> T;
//這個東西有一點點長
//第一個參數是數據類型
//第二個要填null_type,低版本編譯器填null_mapped_type
//第三個填比較函數 std::greater<> or std::less<> or cmp
//第四個填樹的類型,有rb_tree_tag紅黑樹和splay_tree_tag
//第五個是為了支持查詢第k大和排名的一個參數
//tree_order_statistics_node_update
使用
這個東西和\(set\)一樣不支持重復元素,所以一般用\(double\),或者自定義結構體變量或者用\(pair\)都是可以的,只要記住千萬不要插入重復元素就好了。
#include <cstdio>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
tree<double, null_mapped_type, greater<double>, rb_tree_tag, tree_order_statistics_node_update> T;
int main()
{
//freopen("3369.in", "r", stdin);
//freopen("3369.out", "w", stdout);
int q, opt, x;
scanf("%d", &q);
for (int i = 1; i <= q; ++ i)
{
scanf("%d%d", &opt, &x);
if(opt == 1)
T.insert(x + i * 1e-6);
//插入一個數
if(opt == 2)
T.erase(T.lower_bound(x));
//刪除一個數
if(opt == 3)
printf("%d\n", (int)T.order_of_key(x) + 1);
//查詢一個數的排名
if(opt == 4)
printf("%d\n", (int)*T.find_by_order(x - 1));
//查詢第k小的數 返回的是一個迭代器 這里k是從0開始算的,意思是最小的數是第0小的
if(opt == 5)
printf("%d\n", (int)round(*(-- T.lower_bound(x))));
//查詢一個數的前驅
if(opt == 6)
printf("%d\n", (int)round(*T.lower_bound(x + 1)));
//查詢一個數的后繼
}
return 0;
}
這個東西在比賽中是可以用的,所以如果嫌打平衡樹太麻煩就可以直接用啦。