bzoj 3224 splay模板题4

再刷水题我就废了。。。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<algorithm>
  4 #include<cstring>
  5 #define lc(x) ch[x][0]
  6 #define rc(x) ch[x][1]
  7 #define inf 0x3f3f3f3f
  8 #define N 200005
  9 using namespace std;
 10 int ch[N][2];
 11 int k[N];int cnt,root;
 12 int size[N],fa[N];
 13 void push_up(int x)
 14 {
 15     size[x]=size[ch[x][0]]+size[ch[x][1]]+1;
 16 }
 17 void rotate(int p)
 18 {
 19     int q=fa[p],y=fa[q],x=(p==ch[q][1]);
 20     ch[q][x]=ch[p][x^1];fa[ch[q][x]]=q;
 21     ch[p][x^1]=q;fa[q]=p;
 22     fa[p]=y;
 23     if(y)
 24     {
 25         if(q==ch[y][1])ch[y][1]=p;
 26         else ch[y][0]=p;
 27     }
 28     push_up(q);push_up(p);
 29     return ;
 30 }
 31 void splay(int x)
 32 {
 33     for(int y;y=fa[x];rotate(x))
 34     {
 35         if(fa[y])
 36         {
 37             if((ch[y][0]==x&&ch[fa[y]][0]==y)||(ch[y][1]==x&&ch[fa[y]][1]==y))rotate(y);
 38             else rotate(x);
 39         }
 40     }
 41     root=x;
 42 }
 43 int pre(int v)
 44 {
 45     int x=root;int tmp=-inf;
 46     while(ch[x][k[x]<v])
 47     {
 48         if(k[x]<v)tmp=k[x];
 49         x=ch[x][k[x]<v];
 50     }if(k[x]<v)tmp=k[x];
 51     return tmp;
 52 }
 53 int suc(int v)
 54 {
 55     int x=root;int tmp=inf;
 56     while(ch[x][k[x]<=v])
 57     {
 58         if(k[x]>v)tmp=k[x];
 59         x=ch[x][k[x]<=v];
 60     }if(k[x]>v)tmp=k[x];
 61     return tmp;
 62 }
 63 int find(int z)
 64 {
 65     int x=root;
 66     if(k[x]==z)return x;
 67     while(ch[x][k[x]<z])
 68     {
 69         x=ch[x][k[x]<z];
 70         if(k[x]==z)return x;
 71     }
 72     return 0;
 73 }
 74 void insert(int z)
 75 {
 76     int x=root;size[x]++;
 77     while(ch[x][k[x]<z])x=ch[x][k[x]<z],size[x]++;
 78     cnt++;ch[x][k[x]<z]=cnt;k[cnt]=z;size[cnt]=1;fa[cnt]=x;splay(cnt);
 79 }
 80 void del(int x)
 81 {
 82     splay(x);
 83     if(!ch[x][1])
 84     {
 85         root=ch[x][0];fa[ch[x][0]]=0;
 86     }
 87     else if(!ch[x][0])
 88     {
 89         root=ch[x][1];fa[ch[x][1]]=0;
 90     }
 91     else 
 92     {
 93         fa[ch[x][0]]=0;int tmp=ch[x][0];
 94         while(ch[tmp][1])tmp=ch[tmp][1];
 95         splay(tmp);ch[tmp][1]=ch[x][1];fa[ch[x][1]]=tmp;
 96         push_up(tmp);
 97     }
 98     return ;
 99 }
100 int fd(int kk,int x)
101 {
102     int l=ch[kk][0];int r=ch[kk][1];
103     if(size[l]+1==x)return k[kk];
104     if(size[l]>=x)return fd(l,x);
105     return fd(r,x-size[l]-1);
106 }
107 int pr(int z)
108 {
109     int ans=0;
110     int x=root;
111     while(x)
112     {
113         if(k[x]<=z)
114         {
115             ans+=size[ch[x][0]]+1;
116             x=ch[x][1];
117         }
118         else x=ch[x][0];
119     }
120     return ans;
121 }
122 void yu()
123 {
124     root=1;k[1]=inf;cnt=1;size[1]=1;
125     insert(-inf);
126 }
127 int main()
128 {
129    yu();
130    int n;
131    scanf("%d",&n);
132    for(int o=1;o<=n;o++)
133    {
134          int t1,t2;
135          scanf("%d%d",&t1,&t2);
136          if(t1==1)
137          {
138              insert(t2);
139       }
140       else if(t1==2)
141       {
142           int x1=find(t2);
143           if(x1!=0)del(x1);
144       }
145       else if(t1==3)
146       {
147           printf("%d\n",pr(t2-1));
148       }
149       else if(t1==4)
150       {
151            printf("%d\n",fd(root,t2+1));
152       }
153       else if(t1==5)
154       {
155            printf("%d\n",pre(t2));
156       }
157       else 
158       {
159           printf("%d\n",suc(t2));
160       }
161    }
162    return 0;
163 }

 

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