bzoj3224: Tyvj 1728 普通平衡树 treap

Posted lhclqslove

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bzoj3224: Tyvj 1728 普通平衡树 treap相关的知识,希望对你有一定的参考价值。

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

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

  1 #include<bits/stdc++.h>
  2 #include<set>
  3 #include<cstdio>
  4 #include<iomanip>
  5 #include<iostream>
  6 #include<string>
  7 #include<cstring>
  8 #include<algorithm>
  9 #define pb push_back
 10 #define ll long long
 11 #define fi first
 12 #define se second
 13 #define PI 3.14159265
 14 //#define ls l,m,rt<<1
 15 //#define rs m+1,r,rt<<1|1
 16 #define eps 1e-7
 17 #define pii pair<int,int>
 18 typedef unsigned long long ull;
 19 const int inf=1e9+1;
 20 const int mx=~0u>>1;
 21 const int mod=1e9+7;
 22 const int maxn=2e5+10;
 23 using namespace std;
 24 struct treap
 25 {
 26     struct node
 27     {
 28         node *ch[2];
 29         int key,siz,wei,cnt;
 30         node(int _key,node * f)
 31         {
 32             ch[0]=ch[1]=f;
 33             key=_key;
 34             siz=cnt=1;
 35             wei=rand();
 36         }
 37         void push(){siz=ch[0]->siz+ch[1]->siz+cnt;}
 38     }*null,*root;
 39     treap()
 40     {
 41         null=new node(0,0);
 42         null->siz=null->cnt=0;null->wei=mx;
 43         root=null;
 44     }
 45     void rot(node *&rt,bool d)//把!d抬上来
 46     {
 47         node *c=rt->ch[!d];
 48         rt->ch[!d]=c->ch[d];
 49         c->ch[d]=rt;
 50         rt->push();c->push();
 51         rt=c;
 52     }
 53     void insert(const int &key,node *&rt)
 54     {
 55         if(rt==null){rt=new node(key,null);return;}
 56         if(key==rt->key)
 57         {
 58             rt->cnt++;
 59             rt->siz++;return ;
 60         }
 61         bool d=key>rt->key;
 62         insert(key,rt->ch[d]);
 63         if(rt->wei>rt->ch[d]->wei)rot(rt,!d);
 64         rt->push();
 65     }
 66     void remove(const int &key,node *&rt)
 67     {
 68         if(rt==null)return;
 69         bool d=key>rt->key;
 70         if(key==rt->key)
 71         {
 72             if(rt->cnt>1){rt->cnt--;rt->siz--;return ;}
 73              d=rt->ch[0]->wei>rt->ch[1]->wei;
 74              if(rt->ch[d]==null)
 75              {
 76                 delete rt;
 77                 rt=null;
 78                 return ;
 79              }
 80              rot(rt,!d);
 81              remove(key,rt);
 82         }
 83         else remove(key,rt->ch[d]);
 84         rt->push();
 85     }
 86     void view(node *rt)
 87     {
 88         if(rt==null)
 89         {
 90             return ;
 91         }
 92         cout<<rt->key<<endl;
 93         view(rt->ch[0]);
 94         view(rt->ch[1]);
 95     }
 96     int rank(const int &key,node *rt)
 97     {
 98         if(rt==null)return 0;
 99         if(key==rt->key)return rt->ch[0]->siz+1;
100         if(key<rt->key)return rank(key,rt->ch[0]);
101         else return rt->ch[0]->siz+rt->cnt+rank(key,rt->ch[1]);
102     }
103     node *select(int k,node *rt)
104     {
105            // cout<<k<<endl;
106             int s=rt->ch[0]->siz+rt->cnt;
107             if(k>rt->ch[0]->siz&&k<=s)return rt;
108              if(k>s)return select(k-s,rt->ch[1]);
109             else return select(k,rt->ch[0]);
110           //  if(k<s)return select(k-s,rt->ch[1]);
111            // else return select(k,rt->ch[0]);
112     }
113     int pre(const int &k)
114     {
115        node *t=root;
116        int ret=0;
117         while(t!=null)
118         {
119             if(t->key<k)ret=t->key,t=t->ch[1];
120             else t=t->ch[0];
121         }
122         return ret;
123     }
124     int suc(const int &k)
125     {
126          node *t=root;
127        int ret=0;
128         while(t!=null)
129         {
130             if(t->key>k)ret=t->key,t=t->ch[0];
131             else t=t->ch[1];
132         }
133         return ret;
134     }
135 };
136 int main()
137 {
138     int n;
139     treap ac=treap();
140     scanf("%d",&n);
141     while(n--)
142     {
143         int op,x;
144         scanf("%d %d",&op,&x);
145         if(op==1)ac.insert(x,ac.root);
146         else if(op==2)ac.remove(x,ac.root);
147         else if(op==3)printf("%d
",ac.rank(x,ac.root));
148         else if(op==4)printf("%d
",ac.select(x,ac.root)->key);
149         else if(op==5)printf("%d
",ac.pre(x));
150         else printf("%d
",ac.suc(x));
151       //  ac.view(ac.root);
152     }
153     return 0;
154 }

 

以上是关于bzoj3224: Tyvj 1728 普通平衡树 treap的主要内容,如果未能解决你的问题,请参考以下文章

bzoj 3224 Tyvj 1728 普通平衡树

bzoj3224: Tyvj 1728 普通平衡树 treap

BZOJ3224: Tyvj 1728 普通平衡树[treap]

bzoj 3224: Tyvj 1728 普通平衡树

bzoj3224: Tyvj 1728 普通平衡树(spaly)

bzoj3224Tyvj 1728 普通平衡树 treap