zoukankan      html  css  js  c++  java
  • [bzoj3224][tyvj1728][普通平衡树] (pb_ds库自带红黑树)

    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

    10
    1 106465
    4 1
    1 317721
    1 460929
    1 644985
    1 84185
    1 89851
    6 81968
    1 492737
    5 493598

    Sample Output

    106465
    84185
    492737

    HINT

    1.n的数据范围:n<=100000
    2.每个数的数据范围:[-1e7,1e7]
    数据如下http://pan.baidu.com/s/1jHMJwO2
     
    pb_ds中红黑树500ms水过
    #include <bits/stdc++.h>
    #include <ext/pb_ds/tree_policy.hpp>
    #include <ext/pb_ds/assoc_container.hpp>
    using namespace std;
    using namespace __gnu_pbds;
    struct pt{
        int first,second;
        pt(int x,int y) :first(x),second(y) {}
        bool operator<(const pt h)const{
            return first<h.first || (first==h.first&&second<h.second);
        }
        bool operator==(const pt h)const{
            return first==h.first&&second==h.second;
        }
    };
    typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
    inline char Blue(){
        static char B[1<<9],*S=B,*T=B;
        if(S==T)T=(S=B)+fread(B,1,1<<9,stdin);
        return *S++;
    }
    inline int Rin(){
        int x=0,c=Blue(),f=1;
        for(;c<48||c>57;c=Blue())
            if(!(c^45))f=-1;
        for(;c>47&&c<58;c=Blue())
            x=(x<<1)+(x<<3)+c-48;
        return x*f;
    }
    int T,opt,x;
    map<int,int>s;
    rbtree t;
    int main(){
        T=Rin();
        while(T--){
            opt=Rin(),x=Rin();
            switch(opt){
                case 1:t.insert(pt(x,s[x]++));break;
                case 2:t.erase(pt(x,--s[x]));break;
                case 3:
                    printf("%d
    ",t.order_of_key(pt(x,0))+1);
                    break;
                case 4:
                    printf("%d
    ",t.find_by_order(x-1)->first);
                    break;
                case 5:
                    printf("%d
    ",t.find_by_order(t.order_of_key(pt(x,0))-1)->first);
                    break;
                case 6:
                    printf("%d
    ",t.find_by_order(t.order_of_key(pt(x,s[x]-1))+(t.find(pt(x,0))==t.end()?0:1))->first);
                    break;
                default:break;
            }
        }
        return 0;
    }
  • 相关阅读:
    神经网络 初步
    SVM整理
    碎碎念
    random note
    2015阿里实习内推一轮被拒
    django开发框架-view & template
    一点思考
    dive into python 读笔(3)
    dive into python 读笔(2)
    dive into python 读笔(1)
  • 原文地址:https://www.cnblogs.com/keshuqi/p/6257904.html
Copyright © 2011-2022 走看看