• bzoj3224: Tyvj 1728 普通平衡树 treap


    题目链接“:3224: Tyvj 1728 普通平衡树

    题解:treap平衡树模板 ,参考资料还有

    #include<bits/stdc++.h>
    #include<set>
    #include<cstdio>
    #include<iomanip>
    #include<iostream>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #define pb push_back
    #define ll long long
    #define fi first
    #define se second
    #define PI 3.14159265
    //#define ls l,m,rt<<1
    //#define rs m+1,r,rt<<1|1
    #define eps 1e-7
    #define pii pair<int,int>
    typedef unsigned long long ull;
    const int inf=1e9+1;
    const int mx=~0u>>1;
    const int mod=1e9+7;
    const int maxn=2e5+10;
    using namespace std;
    struct treap
    {
        struct node
        {
            node *ch[2];
            int key,siz,wei,cnt;
            node(int _key,node * f)
            {
                ch[0]=ch[1]=f;
                key=_key;
                siz=cnt=1;
                wei=rand();
            }
            void push(){siz=ch[0]->siz+ch[1]->siz+cnt;}
        }*null,*root;
        treap()
        {
            null=new node(0,0);
            null->siz=null->cnt=0;null->wei=mx;
            root=null;
        }
        void rot(node *&rt,bool d)//把!d抬上来
        {
            node *c=rt->ch[!d];
            rt->ch[!d]=c->ch[d];
            c->ch[d]=rt;
            rt->push();c->push();
            rt=c;
        }
        void insert(const int &key,node *&rt)//插入
        {
            if(rt==null){rt=new node(key,null);return;}
            if(key==rt->key)
            {
                rt->cnt++;
                rt->siz++;return ;
            }
            bool d=key>rt->key;
            insert(key,rt->ch[d]);
            if(rt->wei>rt->ch[d]->wei)rot(rt,!d);
            rt->push();
        }
        void remove(const int &key,node *&rt)//删除
        {
            if(rt==null)return;
            bool d=key>rt->key;
            if(key==rt->key)
            {
                if(rt->cnt>1){rt->cnt--;rt->siz--;return ;}
                 d=rt->ch[0]->wei>rt->ch[1]->wei;
                 if(rt->ch[d]==null)
                 {
                    delete rt;
                    rt=null;
                    return ;
                 }
                 rot(rt,!d);
                 remove(key,rt);
            }
            else remove(key,rt->ch[d]);
            rt->push();
        }
        void view(node *rt)
        {
            if(rt==null)
            {
                return ;
            }
            cout<<rt->key<<endl;
            view(rt->ch[0]);
            view(rt->ch[1]);
        }
        int rank(const int &key,node *rt)//查询key是第几大
        {
            if(rt==null)return 0;
            if(key==rt->key)return rt->ch[0]->siz+1;
            if(key<rt->key)return rank(key,rt->ch[0]);
            else return rt->ch[0]->siz+rt->cnt+rank(key,rt->ch[1]);
        }
        node *select(int k,node *rt)//查询第k大
        {
               // cout<<k<<endl;
                int s=rt->ch[0]->siz+rt->cnt;
                if(k>rt->ch[0]->siz&&k<=s)return rt;
                 if(k>s)return select(k-s,rt->ch[1]);
                else return select(k,rt->ch[0]);
              //  if(k<s)return select(k-s,rt->ch[1]);
               // else return select(k,rt->ch[0]);
        }
        int pre(const int &k)//返回第一个比k小的树
        {
           node *t=root;
           int ret=0;
            while(t!=null)
            {
                if(t->key<k)ret=t->key,t=t->ch[1];
                else t=t->ch[0];
            }
            return ret;
        }
        int suc(const int &k)//返回第一个比k大的数
        {
             node *t=root;
           int ret=0;
            while(t!=null)
            {
                if(t->key>k)ret=t->key,t=t->ch[0];
                else t=t->ch[1];
            }
            return ret;
        }
    };
    int main()
    {
        int n;
        treap ac=treap();
        scanf("%d",&n);
        while(n--)
        {
            int op,x;
            scanf("%d %d",&op,&x);
            if(op==1)ac.insert(x,ac.root);
            else if(op==2)ac.remove(x,ac.root);
            else if(op==3)printf("%d
    ",ac.rank(x,ac.root));
            else if(op==4)printf("%d
    ",ac.select(x,ac.root)->key);
            else if(op==5)printf("%d
    ",ac.pre(x));
            else printf("%d
    ",ac.suc(x));
          //  ac.view(ac.root);
        }
        return 0;
    }
  • 相关阅读:
    【C#语言规范版本5.0学习】2 词法结构(一、简述)
    【C#语言规范版本5.0学习】1.11 特性
    TP5.1 实现超时未支付订单自动关闭
    tp5.1使用队列
    开启队列时,命令行输入php think queue:listen出现乱码
    mysql 查询分组后的总条数
    处理mysql先排序在分组
    mysql 5.7 sql_mode设置 坑
    Windows 版 SourceTree 免登录跳过初始设置的方法和下载地址
    thinkphp5.1-jwt的安装与使用
  • 原文地址:https://www.cnblogs.com/lhclqslove/p/9415820.html
Copyright © 2020-2023  润新知