BZOJ 3224: Tyvj 1728 普通平衡树 pb_ds

来源:互联网 发布:midi伴奏制作软件 编辑:程序博客网 时间:2024/06/10 21:27

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3224

注意老版本 null_mapped_type 新版本null_type

code:

/*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;}


原创粉丝点击