BZOJ 3224: Tyvj 1728 普通平衡树 pb_ds

传送门: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;
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值