• fhq-Treap


    fhq-Treap 是一种非旋转平衡二叉树,代码很好写。

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <string>
    #include <cstdio>
    #include <vector>
    #include <ctime>
    using namespace std;
    
    #define RG register int
    #define LL long long
    
    template<typename elemType>
    inline void Read(elemType &T){
        elemType X=0,w=0; char ch=0;
        while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
        while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
        T=(w?-X:X);
    }
    
    template<typename elemType,size_t MAX_SIZE=100010>
    struct Treap{
        struct TreapNode{
            elemType val;
            int rnd,size,son[2];
            int &operator[](int x){return son[x];}
        }T[MAX_SIZE];
        
        int root,cnt;
        Treap():root(0),cnt(0){}
    
        void push_up(int u){T[u].size=T[T[u][0]].size+T[T[u][1]].size+1;}
    
        void split(int u,elemType key,int &x,int &y){
            //将以u为根的树分离成x,y两棵树
            //其中x树的权值均小于等于key,y树的权值均大于key
            if(!u){x=y=0;return;}
            if(T[u].val<=key){x=u;split(T[u][1],key,T[u][1],y);}
            else{y=u;split(T[u][0],key,x,T[u][0]);}
            push_up(u);
        }
    
        int merge(int x,int y){//合并树x,y
            if(!x) return y;
            if(!y) return x;
            if(T[x].rnd<T[y].rnd){
                T[x][1]=merge(T[x][1],y);
                push_up(x);return x;
            }else{
                T[y][0]=merge(x,T[y][0]);
                push_up(y);return y;
            }
        }
    
        int new_node(elemType val){//新建值为val的结点
            ++cnt;T[cnt].val=val;
            T[cnt].size=1;T[cnt].rnd=rand();
            return cnt;
        }
    
        void insert(elemType val){//插入值为val的结点
            if(root==0){root=new_node(val);return;}
            int x,y;split(root,val,x,y);
            root=merge(merge(x,new_node(val)),y);
        }
    
        void delete_node(elemType val){//删除值为val的结点
            int x,y,z;
            split(root,val,x,z);
            split(x,val-1,x,y);
            y=merge(T[y][0],T[y][1]);
            root=merge(merge(x,y),z);
        }
    
        int get_rank(elemType val){//查询值val的排名
            int x,y;
            split(root,val-1,x,y);
            int res=T[x].size+1;
            root=merge(x,y);
            return res;
        }
    
        int get_kth(int u,int kth){//返回第k大的结点
            if(u==0||kth>T[u].size) return 0;
            if(T[T[u][0]].size+1==kth) return u;
            else if(kth<=T[T[u][0]].size) return get_kth(T[u][0],kth);
            return get_kth(T[u][1],kth-T[T[u][0]].size-1);
        }
        elemType get_kth_val(int kth){return T[get_kth(root,kth)].val;}
        //返回第k大的结点的值
    
        int get_predecessor(elemType val){//返回val的(严格)前驱结点
            int x,y;
            split(root,val-1,x,y);
            int res=get_kth(x,T[x].size);
            root=merge(x,y);
            return res;
        }
        elemType get_predecessor_val(elemType val){return T[get_predecessor(val)].val;}
        //返回val的(严格)前驱结点的值
    
        int get_successor(elemType val){//返回val的(严格)后继结点
            int x,y;
            split(root,val,x,y);
            int res=get_kth(y,1);
            root=merge(x,y);
            return res;
        }
        elemType get_successor_val(elemType val){return T[get_successor(val)].val;}
        //返回val的(严格)后继结点的值
    
        void output_tree(int u){//中序遍历Treap
            if(!u) return;
            output_tree(T[u][0]);
            cout<<T[u].val<<" ";
            output_tree(T[u][1]);
        }
    };
    Treap<int> Tree;
    int n;
    
    int main(){
        Read(n);
        for(RG i=1;i<=n;++i){
            int opt,x;
            Read(opt);Read(x);
            if(opt==1) Tree.insert(x);
            else if(opt==2) Tree.delete_node(x);
            else if(opt==3) printf("%d
    ",Tree.get_rank(x));
            else if(opt==4) printf("%d
    ",Tree.get_kth_val(x));
            else if(opt==5) printf("%d
    ",Tree.get_predecessor_val(x));
            else if(opt==6) printf("%d
    ",Tree.get_successor_val(x));
            else if(opt==7) Tree.output_tree(Tree.root);
        }
    
        return 0;
    }
    
  • 相关阅读:
    OLEDB SqlHelper
    .net中数据库事务机制
    C#中的@符号
    C#实现WEB服务器
    Snake.Net
    C#在客戶端和服務端操作Excel文件
    数据库连接字符串大全
    获取cpu序列号,硬盘ID,网卡MAC地址
    Asp.net动态生成html页面(、
    document 文挡对象详解
  • 原文地址:https://www.cnblogs.com/AEMShana/p/13415081.html
Copyright © 2020-2023  润新知