BZOJ1208 [HNOI2004]宠物收养所

普通的splay,熟悉板

  1 /**************************************************************
  2     Problem: 1208
  3     User: round_0
  4     Language: C++
  5     Result: Accepted
  6     Time:232 ms
  7     Memory:2372 kb
  8 ****************************************************************/
  9  
 10 #include <cstdio>
 11 #include <cstring>
 12 #include <cmath>
 13 #include <algorithm>
 14 using namespace std;
 15 const int maxn = 100005;
 16 const int mod = 1000000;
 17 const int INF = 0x7fffffff;
 18 typedef long long LL;
 19 int fa[maxn],son[2][maxn],val[maxn];
 20 int root,tot,cnt;
 21 void init(){
 22     memset(fa,0,sizeof(fa));
 23     memset(son,0,sizeof(son));
 24     root = tot = cnt = 0;
 25 }
 26 void rota(int w,int x){
 27     int y = fa[x];
 28     son[!w][y] = son[w][x];
 29     if(son[w][x])fa[son[w][x]] = y;
 30     fa[x] = fa[y];
 31     if(fa[y])son[y==son[1][fa[y]]][fa[y]] = x;
 32     fa[y] = x;
 33     son[w][x] = y;
 34 }
 35 void splay(int x,int y){
 36     while(fa[x]!=y){
 37         if(fa[fa[x]]==y)rota(x==son[0][fa[x]],x);
 38         else {
 39             int w = fa[x]==son[0][fa[fa[x]]];
 40             if(x==son[w][fa[x]]){
 41                 rota(!w,x);
 42                 rota(w,x);
 43             }
 44             else {
 45                 rota(w,fa[x]);
 46                 rota(w,x);
 47             }
 48         }
 49     }
 50     if(y==0)root = x;
 51 }
 52 int Ins(int v){
 53     int x = root;
 54     while(1){
 55         if(v==val[x]){splay(x,0);return 0;}
 56         else {
 57             if(v<val[x]){
 58                 if(son[0][x])x = son[0][x];
 59                 else break;
 60             }
 61             else {
 62                 if(son[1][x])x = son[1][x];
 63                 else break;
 64             }
 65         }
 66     }
 67     fa[++tot] = x;
 68     val[tot] = v;
 69     if(v<val[x])son[0][x] = tot;
 70     else son[1][x] = tot;
 71     splay(tot,0);
 72     return 1;
 73 }
 74 int Find(int v){
 75     int x = root;
 76     while(1){
 77         if(v==val[x])break;
 78         if(v<val[x])x = son[0][x];
 79         else x = son[1][x];
 80     }
 81     splay(x,0);
 82     return x;
 83 }
 84 void Del(int v){
 85     int x = Find(v),y = son[0][x],z = son[1][x];
 86     if(y==0&&z==0){root = 0;return;}
 87     while(son[1][y])y = son[1][y];
 88     while(son[0][z])z = son[0][z];
 89     if(y)splay(y,0);
 90     if(z)splay(z,y);
 91     if(z==0){son[1][y] = 0;return;}
 92     son[0][z] = 0;
 93 }
 94 int getPre(int x){
 95     x = son[0][x];
 96     if(!x)return INF;
 97     while(son[1][x])x = son[1][x];
 98     return val[x];
 99 }
100 int getNext(int x){
101     x = son[1][x];
102     if(!x)return INF;
103     while(son[0][x])x = son[0][x];
104     return val[x];
105 }
106 int main()
107 {
108   //  freopen("in.txt","r",stdin);
109     int n;
110     while(~scanf("%d",&n)){
111         init();
112         int ans = 0,ok;
113         for(int i = 1;i<=n;++i){
114             int op,x;scanf("%d%d",&op,&x);
115             if(cnt==0){
116                 root = ++tot;
117                 val[tot] = x;
118                 cnt++;
119                 ok = op;
120                 continue;
121             }
122             if(ok==op){
123                 Ins(x);
124                 cnt++;
125             }
126             else {
127                 if(Ins(x)==0){
128                     Del(x);
129                     cnt--;
130                 }
131                 else {
132                     int a = getPre(root),b = getNext(root);
133                     if(abs(a-x)<=abs(b-x))Del(a);
134                     else Del(b);
135                     Del(x);cnt--;
136                     ans = ((LL)ans+(LL)min(abs(a-x),abs(b-x)))%(LL)mod;
137                 }
138             }
139         }
140         printf("%d\n",ans);
141     }
142     return 0;
143 }

 

posted on 2015-08-06 00:00  round_0  阅读(156)  评论(0编辑  收藏  举报

导航