各种平衡树板子

Splay

  1 #include <cstdio>
  2 #include <iostream>
  3 using namespace std;
  4 const int maxn=1e5+5,inf=0x3f3f3f3f;
  5 int siz[maxn],fa[maxn],ch[maxn][2],rep[maxn],vl[maxn];
  6 int n,tot;
  7 
  8 inline int ids(int x) {return ch[fa[x]][0]==x?0:1;}
  9 
 10 inline void update(int x) {siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+rep[x];}
 11 
 12 inline void connect(int x,int f,int fs) {fa[x]=f;ch[f][fs]=x;}
 13 
 14 inline void rotate(const int &x)
 15 {
 16     int f=fa[x],ff=fa[fa[x]];
 17     int s1=ids(x),s2=ids(f);
 18     connect(ch[x][s1^1],f,s1);connect(f,x,s1^1);connect(x,ff,s2);
 19     update(f);update(x);
 20 }
 21 
 22 inline void splay(int x,int to)
 23 {
 24     while(fa[x]!=to)
 25     {
 26         if(fa[fa[x]]==to) rotate(x);
 27         else if(ids(fa[x])==ids(x)) rotate(fa[x]),rotate(x);
 28         else rotate(x),rotate(x);
 29     }
 30 }
 31 
 32 inline int cre(const int &val,const int &f) {fa[++n]=f;vl[n]=val;siz[n]=rep[n]=1;return n;}
 33 
 34 int find(int val)
 35 {
 36     int now=ch[0][0],nxt;
 37     while(1)
 38     {
 39         if(vl[now]==val){
 40             splay(now,0);
 41             return now;
 42         }
 43         nxt=val<vl[now]?0:1;
 44         if(!ch[now][nxt]) return 0;
 45         now=ch[now][nxt];
 46     }
 47 }
 48 
 49 void insert(int val)
 50 {
 51     tot++;
 52     if(!ch[0][0]) {ch[0][0]=cre(val,0);return;}
 53     int now=ch[0][0],nxt;
 54     while(1)
 55     {
 56         siz[now]++;
 57         if(val==vl[now]){
 58             rep[now]++;splay(now,0);return;
 59         }
 60         nxt=val<vl[now]?0:1;
 61         if(!ch[now][nxt])
 62         {
 63             ch[now][nxt]=cre(val,now);
 64             splay(ch[now][nxt],0);
 65             return;
 66         }
 67         now=ch[now][nxt];
 68     }
 69 }
 70 
 71 inline int lowe(int x)
 72 {
 73     int now=ch[0][0],ans=-inf;
 74     while(now){
 75         if(vl[now]<x&&vl[now]>ans) ans=vl[now];
 76         now=ch[now][x>vl[now]?1:0];
 77     }
 78     return ans;
 79 }
 80 
 81 inline int uppe(int x)
 82 {
 83     int now=ch[0][0],ans=inf;
 84     while(now){
 85         if(vl[now]>x&&vl[now]<ans) ans=vl[now];
 86         now=ch[now][x<vl[now]?0:1];
 87     }
 88     return ans;
 89 }
 90 
 91 void delt(int x)
 92 {
 93     tot--;
 94     int dl=find(x);
 95     if(!dl) return;
 96     if(rep[dl]>1) {siz[dl]--;rep[dl]--;return;}
 97     if(!ch[dl][0]) {connect(ch[dl][1],0,0);return;}
 98     if(!ch[dl][1]) {connect(ch[dl][0],0,0);return;}
 99     int px=ch[dl][0];
100     while(ch[px][1]) px=ch[px][1];
101     splay(px,dl);
102     connect(ch[dl][1],px,1);connect(px,0,0);
103     update(px);
104 }
105 
106 int rk(int val)
107 {
108     int ans=0,now=ch[0][0];
109     while(now)
110     {
111         if(vl[now]==val){
112             ans+=siz[ch[now][0]]+1;break;
113         }
114         if(val<vl[now]) now=ch[now][0];
115         else ans+=siz[ch[now][0]]+rep[now],now=ch[now][1];
116     }
117     splay(now,0);
118     return ans;
119 }
120 
121 int rrk(int x)
122 {
123     if(x>tot) return -inf;
124     int now=ch[0][0],tmp;
125     while(now)
126     {
127         tmp=rep[now]+siz[ch[now][0]];
128         if(siz[ch[now][0]]<x&&x<=tmp) break;
129         if(x<tmp) now=ch[now][0];
130         else x-=tmp,now=ch[now][1];
131     }
132     splay(now,0);
133     return vl[now];
134 }
135 
136 int main()
137 {
138     int t,opt,x;
139     scanf("%d",&t);
140     for(int i=1;i<=t;i++)
141     {
142         scanf("%d%d",&opt,&x);
143         switch(opt)
144         {
145             case 1: insert(x);break;
146             case 2: delt(x);break;
147             case 3: printf("%d\n",rk(x));break;
148             case 4: printf("%d\n",rrk(x));break;
149             case 5: printf("%d\n",lowe(x));break;
150             case 6: printf("%d\n",uppe(x));break;
151             default: break;
152         }
153     }
154     return 0;
155 }

Fhq-treap

  1 #include <cstdio>
  2 #include <iostream>
  3 #include <ctime>
  4 #include <cstdlib>
  5 using namespace std;
  6 const int maxn=1e5+5;
  7 struct Fhq_treep_Node
  8 {
  9     int val,siz,ch[2],rnd;
 10 }tr[maxn];
 11 int rt,r1,r2,r3,utot;
 12 
 13 inline void update(const int &u) {tr[u].siz=tr[tr[u].ch[0]].siz+tr[tr[u].ch[1]].siz+1;}
 14 inline int newnode(const int &val) {tr[++utot].val=val;tr[utot].siz=1;tr[utot].rnd=rand();return utot;}
 15 
 16 void split(int u,int val,int &a,int &b)
 17 {
 18     if(!u) {a=b=0;return;}
 19     if(tr[u].val<=val) a=u,split(tr[a].ch[1],val,tr[a].ch[1],b);
 20     else b=u,split(tr[b].ch[0],val,a,tr[b].ch[0]);
 21     update(u);
 22 }
 23 
 24 int merge(int a,int b)
 25 {
 26     if(!a||!b) return a+b;
 27     if(tr[a].rnd<tr[b].rnd){
 28         tr[a].ch[1]=merge(tr[a].ch[1],b);
 29         update(a);return a;
 30     }
 31     else{
 32         tr[b].ch[0]=merge(a,tr[b].ch[0]);
 33         update(b);return b;
 34     }
 35 }
 36 
 37 void insert(int val)
 38 {
 39     split(rt,val,r1,r2);
 40     rt=merge(r1,merge(newnode(val),r2));
 41 }
 42 
 43 void delt(int val)
 44 {
 45     split(rt,val,r1,r3);
 46     split(r1,val-1,r1,r2);
 47     r2=merge(tr[r2].ch[0],tr[r2].ch[1]);
 48     rt=merge(merge(r1,r2),r3);
 49 }
 50 
 51 int rnk(int val)
 52 {
 53     split(rt,val-1,r1,r2);
 54     int ans=tr[r1].siz+1;
 55     rt=merge(r1,r2);
 56     return ans;
 57 }
 58 
 59 int kth(int u,int k)
 60 {
 61     while(1)
 62     {
 63         if(tr[tr[u].ch[0]].siz>=k) u=tr[u].ch[0];
 64         else if(k>tr[tr[u].ch[0]].siz+1) k=k-tr[tr[u].ch[0]].siz-1,u=tr[u].ch[1];
 65         else return u;
 66     }
 67 }
 68 
 69 void lower(int val)
 70 {
 71     split(rt,val-1,r1,r2);
 72     printf("%d\n",tr[kth(r1,tr[r1].siz)].val);
 73     rt=merge(r1,r2);
 74 }
 75 
 76 void upper(int val)
 77 {
 78     split(rt,val,r1,r2);
 79     printf("%d\n",tr[kth(r2,1)].val);
 80     rt=merge(r1,r2);
 81 }
 82 
 83 int main()
 84 {
 85     srand(time(0));
 86     int opt,x,t;
 87     scanf("%d",&t);
 88     while(t--)
 89     {
 90         scanf("%d%d",&opt,&x);
 91         switch(opt)
 92         {
 93             case 1:insert(x);break;
 94             case 2:delt(x);break;
 95             case 3:printf("%d\n",rnk(x));break;
 96             case 4:printf("%d\n",tr[kth(rt,x)].val);break;
 97             case 5:lower(x);break;
 98             case 6:upper(x);break;
 99         }
100     }
101     return 0;
102 }

WBLT

  1 #include <cstdio>
  2 #include <iostream>
  3 using namespace std;
  4 const int ratio=3,inf=0x3f3f3f3f;
  5 
  6 struct Node
  7 {
  8     int siz,val;
  9     Node *ch[2];
 10     Node(int siz=0,int val=0) : siz(siz),val(val)
 11     {
 12         this->ch[0]=NULL;this->ch[1]=NULL;
 13     }
 14 };
 15 Node *rt;
 16 
 17 template<typename T>
 18 inline void read(T &x)
 19 {
 20     char c;int f=1;
 21     while(!isdigit(c=getchar())) (c=='-')&&(f=-1);
 22     x=c^48;
 23     while(isdigit(c=getchar())) x=x*10+(c^48);
 24     x*=f;
 25 }
 26 
 27 inline int min(int x,int y)
 28 {
 29     return x<y?x:y;
 30 }
 31 
 32 inline int max(int x,int y)
 33 {
 34     return x>y?x:y;
 35 }
 36 
 37 void merge(Node *u,Node *l,Node *r)
 38 {
 39     u->siz=l->siz+r->siz;
 40     u->val=r->val;
 41     u->ch[0]=l;u->ch[1]=r;
 42 }
 43 
 44 void rotate(Node *u,int s)
 45 {
 46     if(u->ch[0]==NULL||u->ch[s^1]->ch[0]==NULL) return;
 47     Node *now=new Node;
 48     if(!s) merge(now,u->ch[s],u->ch[s^1]->ch[s]);
 49     else merge(now,u->ch[s^1]->ch[s],u->ch[s]);
 50     u->ch[s]=now;
 51     now=u->ch[s^1]->ch[s^1];
 52     delete []u->ch[s^1];
 53     u->ch[s^1]=now;
 54 }
 55 
 56 void maintain(Node *u)
 57 {
 58     if(u->ch[0]==NULL) return;
 59     if(u->ch[0]->siz > u->ch[1]->siz*ratio)
 60         rotate(u,1);
 61     else if(u->ch[1]->siz > u->ch[0]->siz*ratio)
 62         rotate(u,0);
 63     if(u->ch[0]->siz > u->ch[1]->siz*ratio)
 64         rotate(u->ch[0],0),rotate(u,1);
 65     else if(u->ch[1]->siz > u->ch[0]->siz*ratio)
 66         rotate(u->ch[1],1),rotate(u,0);
 67 }
 68 
 69 void pushup(Node *u)
 70 {
 71     if(u->ch[0]==NULL) return;
 72     u->siz=u->ch[0]->siz+u->ch[1]->siz;
 73     u->val=u->ch[1]->val;
 74 }
 75 
 76 void insert(Node *u,int val)
 77 {
 78     //if((u->ch[0]==NULL&&u->ch[1]!=NULL)||(u->ch[0]!=NULL&&u->ch[1]==NULL)) puts("FUCK");
 79     if(u->ch[0]==NULL)
 80     {
 81         u->ch[0]=new Node(1,min(val,u->val));
 82         u->ch[1]=new Node(1,max(val,u->val));
 83         pushup(u);return;
 84     }
 85     maintain(u);
 86     insert(u->ch[val > u->ch[0]->val],val);
 87     pushup(u);
 88 }
 89 
 90 void erase(Node *u,int val,Node *f)
 91 {
 92     if(u->ch[0]==NULL)
 93     {
 94         int s=f->ch[1]==u;
 95         Node *now=f->ch[s^1];delete []f->ch[s];
 96         f->val=now->val;f->siz=now->siz;
 97         f->ch[0]=now->ch[0];f->ch[1]=now->ch[1];
 98         delete []now;
 99         return;
100     }
101     maintain(u);
102     erase(u->ch[val > u->ch[0]->val],val,u);
103     pushup(u);
104 }
105 
106 int kth(int k)
107 {
108     Node *u=rt;
109     while(u->ch[0]!=NULL)
110     {
111         maintain(u);
112         if(u->ch[0]->siz < k) k-=u->ch[0]->siz,u=u->ch[1];
113         else u=u->ch[0];
114     }
115     return u->val;
116 }
117 
118 int rnk(Node *u,int x)
119 {
120     if(u->ch[0]==NULL) return 1;
121     maintain(u);
122     if(x > u->ch[0]->val) return rnk(u->ch[1],x)+u->ch[0]->siz;
123     else return rnk(u->ch[0],x);
124 }
125 
126 // void dfs(Node *u)
127 // {
128 //     if(u->siz==1) {printf("%d ",u->val);return;}
129 //     dfs(u->ch[0]);dfs(u->ch[1]);
130 // }
131 
132 int main()
133 {
134     int opt,x,t;
135     read(t);
136     rt=new Node(1,inf);
137     while(t--)
138     {
139         read(opt);read(x);
140         if(opt==1) insert(rt,x);
141         else if(opt==2) erase(rt,x,NULL);
142         else if(opt==3) printf("%d\n",rnk(rt,x));
143         else if(opt==4) printf("%d\n",kth(x));
144         else if(opt==5) printf("%d\n",kth(rnk(rt,x)-1));
145         else if(opt==6) printf("%d\n",kth(rnk(rt,x+1)));
146         //else dfs(rt),puts("");
147     }
148     return 0;
149 }

 

posted @ 2020-03-05 20:17  试试事实上吗  阅读(171)  评论(0)    收藏  举报
Live2D