HNOI2004 宠物收养所 (平衡二叉树)

题目链接

平衡树基础题,用于测试各种平衡树的性能(雾)

treap:

  1 #include<bits/stdc++.h>
  2 typedef long long ll;
  3 using namespace std;
  4 struct Treap {
  5     static const int N=1e5+10;
  6     int rnd() {static int seed=time(0)%0x7fffffff; return seed=seed*48271ll%0x7fffffff;}
  7     int ch[N][2],siz[N],val[N],fa[N],tot,rd[N];
  8     int rel(int u) {return ch[fa[u]][1]==u;}
  9     void init() {tot=fa[0]=ch[0][0]=ch[0][1]=siz[0]=val[0]=rd[0]=0;}
 10     int newnode(int x) {
 11         int u=++tot;
 12         fa[u]=ch[u][0]=ch[u][1]=0;
 13         siz[u]=1,val[u]=x,rd[u]=rnd();
 14         return u;
 15     }
 16     void pu(int u) {siz[u]=siz[ch[u][0]]+siz[ch[u][1]]+1;}
 17     void rot(int u) {
 18         int v=fa[u],f=rel(u),ff=rel(v);
 19         ch[v][f]=ch[u][f^1],fa[ch[v][f]]=v;
 20         ch[u][f^1]=v,fa[u]=fa[v],fa[v]=u;
 21         if(fa[u])ch[fa[u]][ff]=u;
 22         pu(v),pu(u);
 23     }
 24     void ins(int& rt,int u) {
 25         if(!rt) {rt=u; return;}
 26         int v;
 27         for(v=rt; ch[v][val[u]>=val[v]]; v=ch[v][val[u]>=val[v]]);
 28         ch[v][val[u]>=val[v]]=u,fa[u]=v;
 29         for(; fa[u]&&rd[u]<rd[fa[u]]; rot(u));
 30         if(!fa[u])rt=u;
 31         for(u=fa[u]; u; u=fa[u])pu(u);
 32     }
 33     void del(int& rt,int u) {
 34         if(u==rt) {
 35             if(!ch[u][1])rt=ch[u][0];
 36             else if(!ch[u][0])rt=ch[u][1];
 37             else rt=ch[u][rd[ch[u][0]]>=rd[ch[u][1]]];
 38         }
 39         for(; ch[u][0]&&ch[u][1]; rot(ch[u][rd[ch[u][0]]>=rd[ch[u][1]]]));
 40         int f=ch[u][0]?0:1;
 41         if(fa[u])ch[fa[u]][rel(u)]=ch[u][f];
 42         if(ch[u][f])fa[ch[u][f]]=fa[u];
 43         for(u=fa[u]; u; u=fa[u])pu(u);
 44     }
 45     int kth(int u,int k) {
 46         while(k!=siz[ch[u][0]]+1) {
 47             if(k<siz[ch[u][0]]+1)u=ch[u][0];
 48             else k-=siz[ch[u][0]]+1,u=ch[u][1];
 49         }
 50         return u;
 51     }
 52     int next(int u) {
 53         if(ch[u][1]) {
 54             for(u=ch[u][1]; ch[u][0]; u=ch[u][0]);
 55             return u;
 56         } else {
 57             for(; fa[u]&&rel(u); u=fa[u]);
 58             return fa[u];
 59         }
 60     }
 61     int prev(int u) {
 62         if(ch[u][0]) {
 63             for(u=ch[u][0]; ch[u][1]; u=ch[u][1]);
 64             return u;
 65         } else {
 66             for(; fa[u]&&!rel(u); u=fa[u]);
 67             return fa[u];
 68         }
 69     }
 70     int lb(int rt,int x) {
 71         int v=0;
 72         for(int u=rt; u; u=ch[u][x>val[u]])if(val[u]>=x)v=u;
 73         return v;
 74     }
 75     int ub(int rt,int x) {
 76         int v=0;
 77         for(int u=rt; u; u=ch[u][x>=val[u]])if(val[u]>x)v=u;
 78         return v;
 79     }
 80     void go() {
 81         init();
 82         const int mod=1000000;
 83         const int inf=0x3f3f3f3f;
 84         int rt=0,cnt=0,m,ans=0;
 85         ins(rt,newnode(inf));
 86         ins(rt,newnode(~inf));
 87         scanf("%d",&m);
 88         while(m--) {
 89             int f,x;
 90             scanf("%d%d",&f,&x);
 91             if(f==0) {
 92                 if(cnt<0) {
 93                     int r=lb(rt,x),l=prev(r);
 94                     if(abs(val[l]-x)<=abs(val[r]-x)) {
 95                         ans=(ans+abs(val[l]-x))%mod;
 96                         del(rt,l);
 97                     } else {
 98                         ans=(ans+abs(val[r]-x))%mod;
 99                         del(rt,r);
100                     }
101                 } else ins(rt,newnode(x));
102                 cnt++;
103             } else {
104                 if(cnt>0) {
105                     int r=lb(rt,x),l=prev(r);
106                     if(abs(val[l]-x)<=abs(val[r]-x)) {
107                         ans=(ans+abs(val[l]-x))%mod;
108                         del(rt,l);
109                     } else {
110                         ans=(ans+abs(val[r]-x))%mod;
111                         del(rt,r);
112                     }
113                 } else ins(rt,newnode(x));
114                 cnt--;
115             }
116         }
117         printf("%d\n",ans);
118     }
119 } treap;
120 
121 int main() {
122     treap.go();
123     return 0;
124 }
View Code

splay:

  1 #include<bits/stdc++.h>
  2 typedef long long ll;
  3 using namespace std;
  4 struct Splay {
  5     static const int N=1e5+10;
  6     int ch[N][2],siz[N],val[N],fa[N],tot;
  7     int rel(int u) {return ch[fa[u]][1]==u;}
  8     void init() {tot=fa[0]=ch[0][0]=ch[0][1]=siz[0]=val[0]=0;}
  9     int newnode(int x) {
 10         int u=++tot;
 11         fa[u]=ch[u][0]=ch[u][1]=0;
 12         siz[u]=1,val[u]=x;
 13         return u;
 14     }
 15     void pu(int u) {siz[u]=siz[ch[u][0]]+siz[ch[u][1]]+1;}
 16     void rot(int u) {
 17         int v=fa[u],f=rel(u),ff=rel(v);
 18         ch[v][f]=ch[u][f^1],fa[ch[v][f]]=v;
 19         ch[u][f^1]=v,fa[u]=fa[v],fa[v]=u;
 20         if(fa[u])ch[fa[u]][ff]=u;
 21         pu(v),pu(u);
 22     }
 23     void splay(int& rt,int u) {
 24         for(int v=fa[rt]; fa[u]!=v; rot(u))
 25             if(fa[fa[u]]!=v&&rel(fa[u])==rel(u))rot(fa[u]);
 26         rt=u;
 27     }
 28     void ins(int& rt,int u) {
 29         if(!rt) {rt=u; return;}
 30         int v;
 31         for(v=rt; ch[v][val[u]>=val[v]]; v=ch[v][val[u]>=val[v]]);
 32         ch[v][val[u]>=val[v]]=u,fa[u]=v;
 33         splay(rt,u);
 34     }
 35     void del(int& rt,int u) {
 36         splay(rt,u);
 37         if(!ch[u][1])rt=ch[u][0];
 38         else if(!ch[u][0])rt=ch[u][1];
 39         else {
 40             splay(ch[rt][1],kth(ch[rt][1],1));
 41             ch[ch[rt][1]][0]=ch[rt][0];
 42             rt=fa[ch[rt][0]]=ch[rt][1];
 43             pu(rt);
 44         }
 45         fa[rt]=0;
 46     }
 47     int kth(int u,int k) {
 48         while(k!=siz[ch[u][0]]+1) {
 49             if(k<siz[ch[u][0]]+1)u=ch[u][0];
 50             else k-=siz[ch[u][0]]+1,u=ch[u][1];
 51         }
 52         return u;
 53     }
 54     int next(int u) {
 55         if(ch[u][1]) {
 56             for(u=ch[u][1]; ch[u][0]; u=ch[u][0]);
 57             return u;
 58         } else {
 59             for(; fa[u]&&rel(u); u=fa[u]);
 60             return fa[u];
 61         }
 62     }
 63     int prev(int u) {
 64         if(ch[u][0]) {
 65             for(u=ch[u][0]; ch[u][1]; u=ch[u][1]);
 66             return u;
 67         } else {
 68             for(; fa[u]&&!rel(u); u=fa[u]);
 69             return fa[u];
 70         }
 71     }
 72     int lb(int rt,int x) {
 73         int v=0;
 74         for(int u=rt; u; u=ch[u][x>val[u]])if(val[u]>=x)v=u;
 75         return v;
 76     }
 77     int ub(int rt,int x) {
 78         int v=0;
 79         for(int u=rt; u; u=ch[u][x>=val[u]])if(val[u]>x)v=u;
 80         return v;
 81     }
 82     void go() {
 83         init();
 84         const int mod=1000000;
 85         const int inf=0x3f3f3f3f;
 86         int rt=0,cnt=0,m,ans=0;
 87         ins(rt,newnode(inf));
 88         ins(rt,newnode(~inf));
 89         scanf("%d",&m);
 90         while(m--) {
 91             int f,x;
 92             scanf("%d%d",&f,&x);
 93             if(f==0) {
 94                 if(cnt<0) {
 95                     int r=lb(rt,x),l=prev(r);
 96                     if(abs(val[l]-x)<=abs(val[r]-x)) {
 97                         ans=(ans+abs(val[l]-x))%mod;
 98                         del(rt,l);
 99                     } else {
100                         ans=(ans+abs(val[r]-x))%mod;
101                         del(rt,r);
102                     }
103                 } else ins(rt,newnode(x));
104                 cnt++;
105             } else {
106                 if(cnt>0) {
107                     int r=lb(rt,x),l=prev(r);
108                     if(abs(val[l]-x)<=abs(val[r]-x)) {
109                         ans=(ans+abs(val[l]-x))%mod;
110                         del(rt,l);
111                     } else {
112                         ans=(ans+abs(val[r]-x))%mod;
113                         del(rt,r);
114                     }
115                 } else ins(rt,newnode(x));
116                 cnt--;
117             }
118         }
119         printf("%d\n",ans);
120     }
121 } splay;
122 
123 int main() {
124     splay.go();
125     return 0;
126 }
View Code

 

posted @ 2019-01-16 19:44  jrltx  阅读(203)  评论(0编辑  收藏  举报