Tyvj 1728 普通平衡树

来源:互联网 发布:趋势密码软件好吗 编辑:程序博客网 时间:2024/06/07 06:07
HYSBZ - 3224
Tyvj 1728 普通平衡树
Time Limit: 10000MS Memory Limit: 131072KB 64bit IO Format: %lld & %llu

Submit Status

 use MathJax to parse formulas

Description

您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:
1. 插入x数
2. 删除x数(若有多个相同的数,因只删除一个)
3. 查询x数的排名(若有多个相同的数,因输出最小的排名)
4. 查询排名为x的数
5. 求x的前驱(前驱定义为小于x,且最大的数)
6. 求x的后继(后继定义为大于x,且最小的数)

Input

第一行为n,表示操作的个数,下面n行每行有两个数opt和x,opt表示操作的序号(1<=opt<=6)

Output

对于操作3,4,5,6每行输出一个数,表示对应答案

Sample Input

101 1064654 11 3177211 4609291 6449851 841851 898516 819681 4927375 493598

Sample Output

10646584185492737

Hint

1.n的数据范围:n<=100000

2.每个数的数据范围:[-1e7,1e7]

数据如下http://pan.baidu.com/s/1jHMJwO2
代码:
#include<bits/stdc++.h>#include<ext/pb_ds/assoc_container.hpp>#include<tr1/unordered_map>using namespace __gnu_pbds;using namespace std;using namespace std::tr1;typedef long long LL;typedef tree<LL, null_mapped_type, less<LL>, rb_tree_tag, tree_order_statistics_node_update> Tree;int offset = 1e7 + 5;int main(){    int n;    Tree t;    unordered_map<int, int> mp;    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", t.order_of_key((LL)x << 32) + 1);        }        else if(op == 4)            printf("%d\n", (*t.find_by_order(x-1) >> 32)  - offset);        else if(op == 5)        {            x += offset;            Tree::iterator it = t.lower_bound((LL)x << 32);            it--;            printf("%d\n", (*it >> 32) - offset);        }        else        {            x += offset;            Tree::iterator it = t.lower_bound((LL)x + 1 << 32);            printf("%d\n", (*it >> 32) - offset);        }    }    return 0;}


0 0
原创粉丝点击