洛谷-普通平衡树 【pbds】

题目链接:https://www.luogu.org/problemnew/show/P3369

使用教程:https://baijiahao.baidu.com/s?id=1610302746201562113&wfr=spider&for=pc

tree差不多可以当作强化版set来用吧。

/// 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

///显然迭代器可以++,--运算

#include<bits/stdc++.h>
//#include<bits/extc++.h> //include全部文件,但是好像不能用
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp> //用tree
#include<ext/pb_ds/hash_policy.hpp> //用hash
#include<ext/pb_ds/priority_queue.hpp> //用priority_queue
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define ll long long
using namespace std;
using namespace __gnu_pbds;
tree<ll,null_type, less<ll> ,rb_tree_tag,tree_order_statistics_node_update> T;
int main() {
    freopen("a.txt","r",stdin);
    ios::sync_with_stdio(0);
    int n;
    cin>>n;
    rep(i, 1, n) {
        int oper;
        ll x;
        cin>>oper>>x;
        if(oper==1) T.insert((x<<20)+i);
        if(oper==2) T.erase(T.lower_bound(x<<20));
        if(oper==3) cout<<T.order_of_key(x<<20)+1<<endl;
        ll ans = -1e9;
        if(oper==4) ans = *T.find_by_order(x-1);
        if(oper==5) ans = *--T.lower_bound(x<<20);
        if(oper==6) ans = *T.lower_bound((x+1)<<20);
        if(ans!=-1e9) cout<<(ans>>20)<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值