bzoj 1208 splay模板题2

            自己yy了找前驱和后继,学了学怎么删除。。。(反正就是练模板)

        

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #define N 80005
  6 #define lc(x) ch[x][0]
  7 #define rc(x) ch[x][1]
  8 #define inf 0x3f3f3f3f
  9 using namespace std;
 10 int n,k[N*4],size,ch[N*4][2],root,cnt,fa[N];
 11 void rotate(int p)
 12 {
 13     int q=fa[p],y=fa[q],x=(ch[q][1]==p);
 14     ch[q][x]=ch[p][x^1];fa[ch[q][x]]=q;
 15     ch[p][x^1]=q;fa[q]=p;
 16     fa[p]=y;
 17     if(y)
 18     {
 19         if(ch[y][0]==q)ch[y][0]=p;
 20         else ch[y][1]=p;
 21     }
 22 }
 23 void splay(int x)
 24 {
 25     for(int y;y=fa[x];rotate(x))
 26     {
 27         if(fa[y])
 28         {
 29             if((y==lc(fa[y])&&x==lc(y))||(y==rc(fa[y])&&x==rc(y)))rotate(y);
 30             else rotate(x);
 31         }
 32     }
 33     root=x;
 34 }
 35 void insert(int x,int v)
 36 {
 37      while(ch[x][k[x]<v])x=ch[x][k[x]<v];
 38      ch[x][k[x]<v]=++cnt;
 39      fa[cnt]=x;k[cnt]=v;splay(cnt);
 40 }
 41 int now1,now2;
 42 int pre(int x,int v)
 43 {
 44     int tmp=-inf;
 45     while(ch[x][k[x]<v])
 46     {
 47         if(k[x]<=v)if(k[x]>tmp)tmp=k[x],now1=x;
 48         x=ch[x][k[x]<v];
 49     }if(k[x]<=v)if(k[x]>tmp)tmp=k[x],now1=x;
 50     return tmp;
 51 }
 52 int suc(int x,int v)
 53 {
 54     int tmp=inf;
 55     while(ch[x][k[x]<v])
 56     {
 57         if(k[x]>=v)if(k[x]<tmp)tmp=k[x],now2=x;
 58         x=ch[x][k[x]<v];
 59     }if(k[x]>=v)if(k[x]<tmp)tmp=k[x],now2=x;
 60     return tmp;
 61 }
 62 void del(int x)
 63 {
 64     splay(x);
 65     if(!ch[x][0])fa[ch[x][1]]=0,root=ch[x][1];
 66     else if(!ch[x][1])fa[ch[x][0]]=0,root=ch[x][0];
 67     else 
 68     {
 69         fa[ch[x][0]]=0;
 70         int tmp=ch[x][0];while(ch[tmp][1])tmp=ch[tmp][1];
 71         splay(tmp);
 72         ch[tmp][1]=ch[x][1];fa[ch[x][1]]=tmp;
 73     }
 74     return ;
 75 }
 76 int main()
 77 {
 78    scanf("%d",&n);
 79    cnt=1;root=1;
 80    k[1]=inf;int ans=0;
 81    insert(root,-inf);
 82    size=0;int shu=0;
 83    for(int i=1;i<=n;i++)
 84    {
 85       int t1,t2;scanf("%d%d",&t1,&t2);
 86       if(!t1)
 87       {
 88          if(!shu||size==0)
 89          {
 90              insert(root,t2);
 91             shu=0;size++;
 92          }
 93          else 
 94          {
 95              size--;
 96              int qq=pre(root,t2),ww=suc(root,t2);
 97              if(abs(qq-t2)<=abs(ww-t2))
 98              {
 99                  ans+=abs(qq-t2);del(now1);
100             }
101             else 
102             {
103                 ans+=ww-t2;del(now2);
104             }
105          }
106       }
107       else
108       {
109           if(shu||size==0)
110          {
111              insert(root,t2);
112             shu=1;size++;
113          }
114          else 
115          {
116              size--;
117              int qq=pre(root,t2),ww=suc(root,t2); 
118             if(abs(qq-t2)<=abs(ww-t2))
119              {
120                  ans+=abs(qq-t2);del(now1);
121             }
122             else 
123             {
124                 ans+=ww-t2;del(now2);
125             }
126          }
127       }
128       ans%=1000000;
129    }
130    printf("%d\n",ans);
131    return 0;
132 }

 

posted @ 2016-12-06 15:40  SD_le  阅读(183)  评论(0编辑  收藏  举报
重置按钮