平衡树

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long ll;
tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>bbt;
//tree<ll,null_type,less<ll>,splay_tree_tag,tree_order_statistics_node_update>bbt;
//tree<ll,null_type,less<ll>,ov_tree_tag,tree_order_statistics_node_update>bbt;
/*
    int类型
    null_type为映射类型, 低版本g++为 null_mapped_type
    less<int>, greater<int> 比较器
    rb_tree_tag 和 splay_tree_tag 选择树的类型
    tree_order_statistics_node_update 结点更新

    insert, erase
    order_of_key rank
    find_by_order() kth
    lower_bound() 前继, >=x 最小的迭代器
    upper_bound() 后继   >x  最小的迭代器
    a.join(b) b并入a,前提是两颗树的取值范围不相交
    a.split(v, b) key <= v的属于a,其他属于
    注意,插入的元素会去重,如set
*/
int main(){
    ll T,op,n;
    cin>>T;
    for(int i=1;i<=T;i++){
        cin>>op>>n;
        if(op==1){
            bbt.insert((n<<20)+i);
        }
        else if(op==2){
            bbt.erase(bbt.lower_bound(n<<20));
        }
        else if(op==3){
            cout<<bbt.order_of_key(n<<20)+1<<endl;;
        }
        else if(op==4){
            cout<<((*bbt.find_by_order(n-1))>>20)<<endl;
        }
        else if(op==5){
            cout<<((*--bbt.lower_bound(n<<20))>>20)<<endl;
        }
        else{
            cout<<((*bbt.lower_bound((n+1)<<20))>>20)<<endl;
        }
    }
    return 0;
}