Splay 专辑

Splay写的真是抑郁,各种被坑。。。

被题坑,被自己坑。

一直在调程序。。。

Splay模板:

View Code
  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cstring>
  4 #include <cstdlib>
  5 #include <cstdio>
  6 
  7 #define N 170000
  8 #define INF 2e9
  9 
 10 using namespace std;
 11 
 12 int son[N][2],fa[N],val[N],sum[N];
 13 int n,m,ans,a,w,root,cnt;
 14 
 15 inline void prt(int x,int dp)//调试专用,用不上最好 
 16 {
 17     if(!x) return;
 18     prt(son[x][0],dp+1); printf("%d    %d\n",val[x],dp); prt(son[x][1],dp+1);
 19 }
 20 
 21 inline void pushup(int x)
 22 {
 23     sum[x]=sum[son[x][0]]+sum[son[x][1]]+1;
 24 }
 25 
 26 inline void link(int x,int y,int c)//c=0 left   c=1 right
 27 {
 28     fa[x]=y;
 29     son[y][c]=x;
 30 }
 31 
 32 void rotate(int x,int c)//c=0 left   c=1 right
 33 {
 34     int y=fa[x];
 35     link(x,fa[y],son[fa[y]][1]==y);
 36     link(son[x][!c],y,c);
 37     link(y,x,!c);
 38     pushup(y);
 39 }
 40 
 41 void splay(int x,int g)
 42 {
 43     while(fa[x]!=g)
 44     {
 45         int y=fa[x];
 46         int cy=(son[fa[y]][1]==y),cx=(son[y][1]==x);
 47         if(fa[y]==g) rotate(x,cx);
 48         else
 49         {
 50             if(cx==cy) rotate(y,cy);
 51             else rotate(x,cx);
 52             rotate(x,cy);
 53         }    
 54     }
 55     pushup(x);
 56     if(!g) root=x;
 57 }
 58 
 59 void newnode(int y,int &x,int sp)
 60 {
 61     x=++cnt;
 62     fa[x]=y; val[x]=sp; sum[x]=1;
 63     son[x][0]=son[x][1]=0;
 64 } 
 65 
 66 void init()
 67 {
 68     newnode(cnt=0,root,-INF);
 69     newnode(root,son[root][1],INF);
 70     sum[root]=2;
 71 }
 72 
 73 void insert(int sp)
 74 {
 75     int x=root;
 76     while(son[x][val[x]<sp]) x=son[x][val[x]<sp];
 77     newnode(x,son[x][val[x]<sp],sp);
 78     splay(cnt,0);
 79 }
 80 
 81 int pred(int sp)
 82 {
 83     int x=root,rt=-INF;
 84     while(x)
 85     {
 86         if(val[x]==sp) return sp;
 87         if(val[x]<sp) rt=max(rt,val[x]);
 88         x=son[x][val[x]<sp];
 89     }
 90     return rt;
 91 }
 92 
 93 int succ(int sp)
 94 {
 95     int x=root,rt=INF;
 96     while(x)
 97     {
 98         if(val[x]==sp) return sp;
 99         if(val[x]>sp) rt=min(rt,val[x]);
100         x=son[x][val[x]<sp];
101     }
102     return rt;
103 }
104 
105 int find(int sp)
106 {
107     int x=root,rt=0;
108     while(x)
109     {
110         if(val[x]==sp) rt=x;
111         x=son[x][val[x]<sp];
112     }
113     return rt;
114 }
115 
116 int del(int sp)//return the total number of the deleted numbers  区间删除 
117 {
118     int x=find(sp),rt=0;
119     if(x)
120     {
121         splay(1,0);
122         splay(x,1);
123         rt=sum[son[x][0]];
124         son[x][0]=0;//delete (0,val[x])
125         splay(x,0); 
126     }
127     return rt;
128 }
129 
130 inline void DEL(int sp)//单点删除 一个sp值的点 
131 {
132     int x=find(sp);
133     splay(x,0);
134     if(son[x][0]&&son[x][1])
135     {
136         int y=getmax(son[x][0]);
137         splay(y,x);
138         fa[y]=0; root=y; fa[son[x][1]]=root; son[y][1]=son[x][1];
139         pushup(y);
140     }
141     else if(son[x][0]) fa[son[x][0]]=0,root=son[x][0];
142     else if(son[x][1]) fa[son[x][1]]=0,root=son[x][1];
143 }
144 
145 int select(int k,int g)//return the value of the kth largest number
146 {
147     int x=root;
148     while(sum[son[x][1]]!=k)
149     {
150         if(sum[son[x][1]]>k) x=son[x][1];
151         else k=k-sum[son[x][1]]-1,x=son[x][0];
152     }
153     splay(x,g);
154     return val[x];
155 }
156 
157 void go()
158 {
159     char str[10];
160     init();
161     ans=w=0;
162     while(n--)
163     {
164         scanf("%s%d",str,&a);
165         if(str[0]=='I')
166         {
167             a-=w;
168             if(a>=m-w) insert(a);
169         }
170         else if(str[0]=='A') w+=a;
171         else if(str[0]=='S')
172         {
173             w-=a;
174             ans+=del(succ(m-w));
175         }
176         else if(str[0]=='F')
177         {
178             if(sum[root]<a+2) puts("-1");
179             else printf("%d\n",select(a,0)+w);
180         }
181     }
182     printf("%d\n",ans);
183 }
184 
185 int main()
186 {
187     while(~scanf("%d%d",&n,&m)) go();
188     return 0;
189 }

 

BZOJ 1503:

View Code
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cstdlib>
  5 #include <algorithm>
  6 
  7 #define INF 1e9
  8 #define N 1000000
  9 
 10 using namespace std;
 11 
 12 int fa[N],son[N][2],sz[N],val[N];
 13 int n,cnt,root,ans,dt,mn;
 14 
 15 inline void pushup(int x)
 16 {
 17     sz[x]=sz[son[x][0]]+sz[son[x][1]]+1;
 18 }
 19 
 20 inline void link(int x,int y,int c)
 21 {
 22     fa[x]=y; son[y][c]=x;
 23 }
 24 
 25 inline void rotate(int x,int c)
 26 {
 27     int y=fa[x];
 28     link(x,fa[y],son[fa[y]][1]==y);
 29     link(son[x][!c],y,c);
 30     link(y,x,!c);
 31     pushup(y);
 32 }
 33 
 34 inline void splay(int x,int g)
 35 {
 36     while(fa[x]!=g)
 37     {
 38         int y=fa[x];
 39         int cy=(son[fa[y]][1]==y),cx=(son[y][1]==x);
 40         if(fa[y]==g) rotate(x,cx);
 41         else
 42         {
 43             if(cx==cy) rotate(y,cy);
 44             else rotate(x,cx);
 45             rotate(x,cy);
 46         }
 47     }
 48     pushup(x);
 49     if(!g) root=x; 
 50 }
 51 
 52 inline void newnode(int y,int &x,int sp)
 53 {
 54     x=++cnt;
 55     fa[x]=y; val[x]=sp; sz[x]=1;
 56     son[x][0]=son[x][1]=0;
 57 }
 58 
 59 inline void init()
 60 {
 61     cnt=0;
 62     newnode(0,root,-INF);
 63     newnode(root,son[root][1],INF);
 64     sz[root]=2;
 65 }
 66 
 67 inline void insert(int sp)
 68 {
 69     int x=root;
 70     while(son[x][sp>val[x]]) x=son[x][sp>val[x]];
 71     newnode(x,son[x][sp>val[x]],sp);
 72     splay(cnt,0);
 73 }
 74 
 75 inline int find(int sp)
 76 {
 77     int x=root,res=0;
 78     while(x)
 79     {
 80         if(val[x]==sp) res=x;
 81         x=son[x][sp>val[x]];
 82     }
 83     return res;
 84 }
 85 
 86 inline int del(int sp)
 87 {
 88     int x=find(sp),res=0;
 89     if(x)
 90     {
 91         splay(1,0);
 92         splay(x,1);
 93         res=sz[son[x][0]];
 94         son[x][0]=0;
 95         splay(x,0);
 96     }
 97     return res;
 98 }
 99 
100 inline int succ(int sp)
101 {
102     int x=root,res=INF;
103     while(x)
104     {
105         if(val[x]==sp) return sp;
106         if(sp<val[x]) res=min(res,val[x]);
107         x=son[x][sp>val[x]];
108     }
109     return res;
110 }
111 
112 inline int select(int k,int g)
113 {
114     int x=root;
115     while(sz[son[x][1]]!=k)
116     {
117         if(k<sz[son[x][1]]) x=son[x][1];
118         else k-=sz[son[x][1]]+1,x=son[x][0];
119     }
120     splay(x,g);
121     return val[x];
122 }
123 
124 inline void go()
125 {
126     init();
127     scanf("%d%d",&n,&mn);
128     ans=dt=0;
129     char str[10];int a;
130     while(n--)
131     {
132         scanf("%s%d",str,&a);
133         if(str[0]=='I')
134         {
135             a-=dt;
136             if(a>=mn-dt) insert(a);
137         }
138         else if(str[0]=='A') dt+=a;
139         else if(str[0]=='S')
140         {
141             dt-=a;
142             ans+=del(succ(mn-dt));
143         }
144         else
145         {
146             if(sz[root]-2<a) puts("-1");
147             else printf("%d\n",select(a,0)+dt);
148         }
149     }
150     printf("%d\n",ans);
151 }
152 
153 int main()
154 {
155     go();
156     return 0;
157 } 

 

BZOJ 1588:

View Code
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cstdlib>
  5 #include <algorithm>
  6 
  7 #define INF 1e9
  8 #define N 1000000
  9 
 10 using namespace std;
 11 
 12 int son[N][2],fa[N],sz[N],val[N];
 13 int n,cnt,root,ans;
 14 
 15 
 16 inline void pushup(int x)
 17 {
 18     sz[x]=sz[son[x][0]]+sz[son[x][1]]+1;
 19 }
 20 
 21 inline void link(int x,int y,int c)
 22 {
 23     fa[x]=y; son[y][c]=x;
 24 }
 25 
 26 inline void rotate(int x,int c)
 27 {
 28     int y=fa[x];
 29     link(x,fa[y],son[fa[y]][1]==y);
 30     link(son[x][!c],y,c);
 31     link(y,x,!c);
 32     pushup(y);
 33 }
 34 
 35 inline void splay(int x,int g)
 36 {
 37     while(fa[x]!=g)
 38     {
 39         int y=fa[x];
 40         int cy=(son[fa[y]][1]==y),cx=(son[y][1]==x);
 41         if(fa[y]==g) rotate(x,cx);
 42         else
 43         {
 44             if(cx==cy) rotate(y,cy);
 45             else rotate(x,cx);
 46             rotate(x,cy);
 47         }
 48     }
 49     pushup(x);
 50     if(!g) root=x;
 51 }
 52 
 53 inline void newnode(int y,int &x,int sp)
 54 {
 55     x=++cnt;
 56     fa[x]=y; val[x]=sp; sz[x]=1;
 57     son[x][0]=son[x][1]=0;
 58 }
 59 
 60 inline void init()
 61 {
 62     cnt=0;
 63     newnode(0,root,-INF);
 64     newnode(root,son[root][1],INF);
 65     sz[root]=2;
 66 }
 67 
 68 inline void insert(int sp)
 69 {
 70     int x=root;
 71     while(son[x][sp>val[x]]) x=son[x][sp>val[x]];
 72     newnode(x,son[x][sp>val[x]],sp);
 73     splay(cnt,0);
 74 }
 75 
 76 inline int pred(int sp)
 77 {
 78     int x=root,res=-INF;
 79     while(x)
 80     {
 81         if(val[x]==sp) return sp;
 82         if(val[x]<sp) res=max(res,val[x]);
 83         x=son[x][sp>val[x]];
 84     }
 85     return res;
 86 }
 87 
 88 inline int succ(int sp)
 89 {
 90     int x=root,res=INF;
 91     while(x)
 92     {
 93         if(val[x]==sp) return sp;
 94         if(val[x]>sp) res=min(res,val[x]);
 95         x=son[x][sp>val[x]];
 96     }
 97     return res;
 98 }
 99 
100 inline void go()
101 {
102     init();
103     scanf("%d%d",&n,&ans);insert(ans);
104     for(int i=2,a;i<=n;i++)
105     {
106         if(scanf("%d",&a)==EOF) a=0;
107         int f1=pred(a);
108         int f2=succ(a);
109         ans+=min(f2-a,a-f1);
110         insert(a);
111     }
112     printf("%d\n",ans);
113 }
114 
115 int main()
116 {
117     go();
118     return 0;
119 } 

 

HDU 1754:

View Code
  1 #include <iostream>
  2 #include <algorithm>
  3 #include <cstring>
  4 #include <cstdio>
  5 #include <cstdlib>
  6 
  7 #define N 1000000
  8 #define INF 1e9
  9 
 10 using namespace std;
 11 
 12 int son[N][2],val[N],bh[N],fa[N],mx[N];
 13 int n,cnt,root,m;
 14 
 15 inline void prt(int x,int dp)
 16 {
 17     if(!x) return;
 18     prt(son[x][0],dp+1); printf("%d    %d\n",val[x],dp); prt(son[x][1],dp+1);
 19 }
 20 
 21 inline void pushup(int x)
 22 {
 23     mx[x]=max(val[x],max(mx[son[x][0]],mx[son[x][1]]));
 24 }
 25 
 26 inline void link(int x,int y,int c)
 27 {
 28     fa[x]=y; son[y][c]=x;
 29 }
 30 
 31 inline void rotate(int x,int c)
 32 {
 33     int y=fa[x];
 34     link(x,fa[y],son[fa[y]][1]==y);
 35     link(son[x][!c],y,c);
 36     link(y,x,!c);
 37     pushup(y);
 38 }
 39 
 40 inline void splay(int x,int g)
 41 {
 42     while(fa[x]!=g)
 43     {
 44         int y=fa[x];
 45         int cy=(son[fa[y]][1]==y),cx=(son[y][1]==x);
 46         if(fa[y]==g) rotate(x,cx);
 47         else
 48         {
 49             if(cx==cy) rotate(y,cy);
 50             else rotate(x,cx);
 51             rotate(x,cy);
 52         }
 53     }
 54     pushup(x);
 55     if(!g) root=x;
 56 }
 57 
 58 inline void newnode(int y,int &x,int sp,int b)//细节 
 59 {
 60     x=++cnt;
 61     fa[x]=y; val[x]=sp; bh[x]=b;
 62     son[x][0]=son[x][1]=0;
 63 }
 64 
 65 inline void init()
 66 {
 67     cnt=0;
 68     newnode(0,root,-INF,0);
 69     newnode(root,son[root][1],INF,n+1);
 70 }
 71 
 72 inline void insert(int sp,int b)
 73 {
 74     int x=root;
 75     while(son[x][b>bh[x]]) x=son[x][b>bh[x]];
 76     newnode(x,son[x][b>bh[x]],sp,b);
 77     splay(cnt,0);//细节 
 78 }
 79 
 80 inline int getpos(int b)
 81 {
 82     int x=root;
 83     while(x)
 84     {
 85         if(bh[x]==b) return x;
 86         x=son[x][b>bh[x]];
 87     }
 88 }
 89 
 90 inline void querymax(int l,int r)
 91 {
 92     int f1=getpos(l-1);
 93     int f2=getpos(r+1);
 94     splay(f1,0);splay(f2,f1);
 95     printf("%d\n",mx[son[f2][0]]);
 96 }
 97 
 98 inline void change(int b,int sp)
 99 {
100     int f1=getpos(b);
101     splay(f1,0); val[f1]=sp;
102     pushup(f1);
103 }
104 
105 inline void go()
106 {
107     init();mx[0]=-INF;
108     for(int i=1,a;i<=n;i++)
109     {
110         scanf("%d",&a);
111         insert(a,i);
112     }
113     //prt(root,0);
114     char str[10];
115     for(int i=1,a,b;i<=m;i++)
116     {
117         scanf("%s%d%d",str,&a,&b);
118         if(str[0]=='Q')
119         {
120             if(a>b) swap(a,b);
121             querymax(a,b);
122         }
123         else change(a,b);
124     }
125 }
126 
127 int main()
128 {
129     while(scanf("%d%d",&n,&m)!=EOF) go();
130     return 0;
131 } 

 

HDU 1890:

View Code
  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 #include <cstdlib>
  5 #include <algorithm>
  6 
  7 #define N 1000000
  8 #define BUG system("pause")
  9 
 10 using namespace std;
 11 
 12 struct PX
 13 {
 14     int nu,id;
 15 }px[N];
 16 
 17 int id[N],pos[N],son[N][2],fa[N],rev[N],sz[N];
 18 int cnt,root,n;
 19 
 20 inline bool cmp(const PX &a,const PX &b)
 21 {
 22     if(a.nu!=b.nu) return a.nu<b.nu;
 23     return a.id<b.id;
 24 }
 25 
 26 inline bool cmp1(const PX &a,const PX &b)
 27 {
 28     return a.id<b.id;
 29 }
 30 
 31 inline void pushup(int x)
 32 {
 33     sz[x]=sz[son[x][0]]+sz[son[x][1]]+1;
 34 }
 35 
 36 inline void reverse(int x)
 37 {
 38     if(!x) return;
 39     rev[x]^=1;
 40 }
 41 
 42 inline void pushdown(int x)
 43 {
 44     if(!x||!rev[x]) return;
 45     rev[x]=0;
 46     reverse(son[x][0]);
 47     reverse(son[x][1]);
 48     swap(son[x][0],son[x][1]);
 49 }
 50 
 51 inline void link(int x,int y,int c)
 52 {
 53     fa[x]=y; son[y][c]=x;
 54 }
 55 
 56 inline void rotate(int x,int c)
 57 {
 58     int y=fa[x];
 59     pushdown(y);
 60     pushdown(x); 
 61     link(x,fa[y],son[fa[y]][1]==y);
 62     link(son[x][!c],y,c);
 63     link(y,x,!c);
 64     pushup(y);
 65 }
 66 
 67 inline void splay(int x,int g)
 68 {
 69     pushdown(x);
 70     while(fa[x]!=g)
 71     {
 72         int y=fa[x];
 73         pushdown(fa[y]); pushdown(y); pushdown(x);
 74         int cy=(son[fa[y]][1]==y),cx=(son[y][1]==x);
 75         if(fa[y]==g) rotate(x,cx);
 76         else
 77         {
 78             if(cx==cy) rotate(y,cy);
 79             else rotate(x,cx);
 80             rotate(x,cy);
 81         }
 82     }
 83     pushup(x);
 84     if(!g) root=x;
 85 }
 86 
 87 inline void newnode(int y,int &x)
 88 {
 89     x=++cnt;
 90     fa[x]=y; sz[x]=1; rev[x]=0;
 91     son[x][0]=son[x][1]=0;
 92 }
 93 
 94 inline void init()
 95 {
 96     cnt=0;
 97     newnode(0,root);
 98     newnode(root,son[root][1]);
 99     sz[root]=2;
100 }
101 
102 inline int getmax(int x)//在任何有pushdown的操作中,从上向下时必须提前pushdown! 
103 {
104     pushdown(x);
105     while(son[x][1])
106     {
107         x=son[x][1];
108         pushdown(x);
109     }
110     return x;
111 }
112 
113 inline void del()
114 {
115     int x=root;
116     if(son[x][0]&&son[x][1])
117     {
118         int y=getmax(son[x][0]);
119         splay(y,x);
120         fa[y]=0; root=y; fa[son[x][1]]=root; son[y][1]=son[x][1];
121         pushup(y);
122     }
123     else if(son[x][0]) fa[son[x][0]]=0,root=son[x][0];
124     else if(son[x][1]) fa[son[x][1]]=0,root=son[x][1];
125 }
126 
127 inline void build(int &x,int l,int r,int g)
128 {
129     if(l>r) return;
130     int mid=(l+r)>>1;
131     newnode(g,x);
132     pos[id[mid]]=x;
133     build(son[x][0],l,mid-1,x);
134     build(son[x][1],mid+1,r,x);
135     pushup(x);
136 }
137 
138 inline void go()
139 {
140     init();
141     for(int i=1;i<=n;i++)
142     {
143         scanf("%d",&px[i].nu);
144         px[i].id=i;
145     }
146     sort(px+1,px+1+n,cmp);
147     for(int i=1;i<=n;i++) id[px[i].id]=i;
148     sort(px+1,px+1+n,cmp1);
149     build(son[son[root][1]][0],1,n,son[root][1]);
150     pushup(son[root][1]);
151     pushup(root);
152     for(int i=1;i<=n;i++)
153     {
154         splay(pos[i],0); splay(1,pos[i]);
155         printf("%d",i+sz[son[1][1]]);
156         if(i<n) printf(" ");
157         rev[son[1][1]]^=1;
158         del();
159     }
160     puts("");
161 }
162 
163 int main()
164 {
165     while(scanf("%d",&n),n) go();
166     return 0;
167 }

 

BZOJ 1208:

View Code
  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 #include <cstdlib>
  5 #include <algorithm>
  6 
  7 #define mod 1000000
  8 #define N 1000000
  9 #define INF 1e9
 10 
 11 using namespace std;
 12 
 13 int son[N][2],sz[N],val[N],fa[N];
 14 int n,root,cnt,people,ans;
 15 
 16 
 17 inline void prt(int x,int dp)
 18 {
 19     if(!x) return;
 20     prt(son[x][0],dp+1); printf("%d    %d\n",val[x],dp); prt(son[x][1],dp+1);
 21 }
 22 
 23 inline void pushup(int x)
 24 {
 25     sz[x]=sz[son[x][0]]+sz[son[x][1]]+1;
 26 }
 27 
 28 inline void link(int x,int y,int c)
 29 {
 30     fa[x]=y; son[y][c]=x;
 31 }
 32 
 33 inline void rotate(int x,int c)
 34 {
 35     int y=fa[x];
 36     link(x,fa[y],son[fa[y]][1]==y);
 37     link(son[x][!c],y,c);
 38     link(y,x,!c);
 39     pushup(y);
 40 }
 41 
 42 inline void splay(int x,int g)
 43 {
 44     while(fa[x]!=g)
 45     {
 46         int y=fa[x];
 47         int cy=(son[fa[y]][1]==y),cx=(son[y][1]==x);
 48         if(fa[y]==g) rotate(x,cx);
 49         else 
 50         {
 51             if(cx==cy) rotate(y,cy);
 52             else rotate(x,cx);
 53             rotate(x,cy);
 54         }
 55     }
 56     pushup(x);
 57     if(!g) root=x;
 58 }
 59 
 60 inline void newnode(int y,int &x,int sp)
 61 {
 62     x=++cnt;
 63     fa[x]=y; val[x]=sp; sz[x]=1;
 64     son[x][0]=son[x][1]=0;
 65 }
 66 
 67 inline void init()
 68 {
 69     cnt=0;
 70     newnode(0,root,-INF);
 71     newnode(root,son[root][1],INF);
 72     sz[root]=2;
 73 }
 74 
 75 inline void insert(int sp)
 76 {
 77     int x=root;
 78     while(son[x][sp>val[x]]) x=son[x][sp>val[x]];
 79     newnode(x,son[x][sp>val[x]],sp);
 80     splay(cnt,0);
 81 }
 82 
 83 inline int pred(int sp)
 84 {
 85     int x=root,res=-INF;
 86     while(x)
 87     {
 88         if(sp==val[x]) res=val[x];
 89         else if(sp>val[x]) res=max(res,val[x]);
 90         x=son[x][sp>val[x]];
 91     }
 92     return res;
 93 }
 94 
 95 inline int succ(int sp)
 96 {
 97     int x=root,res=INF;
 98     while(x)
 99     {
100         if(sp==val[x]) res=val[x];
101         else if(sp<val[x]) res=min(res,val[x]);
102         x=son[x][sp>val[x]];
103     }
104     return res;
105 }
106 
107 inline int getmax(int x)
108 {
109     while(son[x][1]) x=son[x][1];
110     return x;
111 }
112 
113 inline int find(int sp)
114 {
115     int x=root,res=0;
116     while(x)
117     {
118         if(val[x]==sp) res=x;
119         x=son[x][sp>val[x]];
120     }
121     return res;
122 }
123 
124 inline void del(int sp)//单点删除 
125 {
126     int x=find(sp);
127     splay(x,0);
128     if(son[x][0]&&son[x][1])
129     {
130         int y=getmax(son[x][0]);
131         splay(y,x);
132         fa[y]=0; root=y; fa[son[x][1]]=root; son[y][1]=son[x][1];
133         pushup(y);
134     }
135     else if(son[x][0]) fa[son[x][0]]=0,root=son[x][0];
136     else if(son[x][1]) fa[son[x][1]]=0,root=son[x][1];
137 }
138 
139 inline void go()
140 {
141     init();
142     scanf("%d",&n);
143     int pd,da;
144     while(n--)
145     {
146         scanf("%d%d",&pd,&da);
147         if(sz[root]==2||pd==people)
148         {
149             people=pd;
150             insert(da);
151             continue;
152         }
153         int f1=pred(da);
154         int f2=succ(da);
155         if(da-f1<=f2-da) ans+=(da-f1)%mod,del(f1);
156         else ans+=(f2-da)%mod,del(f2);
157         ans%=mod;
158     }
159     printf("%d\n",ans);
160 }
161 
162 int main()
163 {
164     go();
165     return 0;
166 }

 

BZOJ 1269:

View Code
  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdlib>
  4 #include <cstdio>
  5 #include <algorithm>
  6 
  7 #define N 5000000
  8 
  9 using namespace std;
 10 
 11 int sz[N],son[N][2],fa[N];
 12 char val[N];
 13 bool rev[N];
 14 int m,root,cnt,gb;
 15 char str[N];
 16 
 17 inline void pushup(int x)
 18 {
 19     sz[x]=sz[son[x][0]]+sz[son[x][1]]+1;
 20 }
 21 
 22 inline void reverse(int x)
 23 {
 24     if(!x) return;
 25     rev[x]^=1;
 26 }
 27 
 28 inline void pushdown(int x)
 29 {
 30     if(!x||!rev[x]) return;
 31     rev[x]=0;
 32     reverse(son[x][0]);
 33     reverse(son[x][1]);
 34     swap(son[x][0],son[x][1]);
 35 }
 36 
 37 inline void link(int x,int y,int c)
 38 {
 39     fa[x]=y; son[y][c]=x;
 40 }
 41 
 42 inline void rotate(int x,int c)
 43 {
 44     int y=fa[x];
 45     pushdown(y);
 46     pushdown(x);
 47     link(x,fa[y],son[fa[y]][1]==y);
 48     link(son[x][!c],y,c);
 49     link(y,x,!c);
 50     pushup(y);
 51 }
 52 
 53 inline void splay(int x,int g)
 54 {
 55     while(fa[x]!=g)
 56     {
 57         int y=fa[x];
 58         pushdown(fa[y]); pushdown(y); pushdown(x);
 59         int cy=(son[fa[y]][1]==y),cx=(son[y][1]==x);
 60         if(fa[y]==g) rotate(x,cx);
 61         else
 62         {
 63             if(cx==cy) rotate(y,cy);
 64             else rotate(x,cx);
 65             rotate(x,cy);
 66         }
 67     }
 68     pushup(x);
 69     if(!g) root=x;
 70 }
 71 
 72 inline void newnode(int y,int &x,char b)
 73 {
 74     x=++cnt;
 75     fa[x]=y; sz[x]=1; val[x]=b;
 76     son[x][0]=son[x][1]=0;
 77 }
 78 
 79 inline void init()
 80 {
 81     cnt=0;
 82     newnode(0,root,'~');
 83     newnode(root,son[root][1],'!');
 84     sz[root]=2;
 85 }
 86 
 87 inline void build(int &x,int l,int r,int g)
 88 {
 89     if(l>r) return;
 90     int mid=(l+r)>>1;
 91     newnode(g,x,str[mid]);
 92     build(son[x][0],l,mid-1,x);
 93     build(son[x][1],mid+1,r,x);
 94     pushup(x);
 95 }
 96 
 97 inline int find(int b)
 98 {
 99     int x=root;
100     pushdown(x);
101     while(x)
102     {
103         if(sz[son[x][0]]+1==b) return x;
104         if(sz[son[x][0]]+1>b) x=son[x][0];
105         else b-=sz[son[x][0]]+1,x=son[x][1];
106         pushdown(x);
107     }
108     while(1);return -1e9;
109 }
110 
111 inline void INSERT(int sl)
112 {
113     int x=find(gb),y=find(gb-1);
114     splay(y,0); splay(x,y);
115     build(son[x][0],1,sl,x);
116     pushup(x); pushup(y);
117 }
118 
119 inline void DELETE(int k)
120 {
121     int x=find(gb+k),y=find(gb-1);
122     splay(y,0); splay(x,y);
123     fa[son[x][0]]=0; son[x][0]=0;
124     pushup(x); pushup(y);
125 }
126 
127 inline void REVERSE(int k)
128 {
129     int x=find(gb+k),y=find(gb-1);
130     splay(y,0); splay(x,y);
131     rev[son[x][0]]^=1;
132 }
133 
134 inline void go()
135 {
136     init(); gb=2;
137     scanf("%d",&m);
138     for(int i=1,a;i<=m;i++)
139     {
140         scanf("%s",str);
141         if(str[0]=='M')
142         {
143             scanf("%d",&a);
144             gb=a+2;
145         }
146         else if(str[0]=='I')
147         {
148             scanf("%d",&a);getchar();
149             gets(str+1);
150             INSERT(a);
151         }
152         else if(str[0]=='D')
153         {
154             scanf("%d",&a);
155             DELETE(a);
156         }
157         else if(str[0]=='R')
158         {
159             scanf("%d",&a);
160             REVERSE(a);
161         }
162         else if(str[0]=='G')
163         {
164             printf("%c\n",val[find(gb)]);
165         }
166         else if(str[0]=='P')
167         {
168             gb--;
169         }
170         else if(str[0]=='N')
171         {
172             gb++;
173         }
174     }
175 }
176 
177 int main()
178 {
179     go();
180     return 0;
181 } 

 

POJ 3468:

View Code
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #include <algorithm>
  5 #include <cstring>
  6 
  7 #define N 200000
  8 #define INF 1LL<<50
  9 
 10 using namespace std;
 11 //带区间更新的splay模板 
 12 __int64 sum[N],val[N],dt[N];
 13 int bh[N],son[N][2],fa[N],sz[N];
 14 int n,m,root,cnt;
 15 
 16 inline void pushup(__int64 x)
 17 {
 18     sum[x]=sum[son[x][0]]+sum[son[x][1]]+val[x];
 19     sz[x]=sz[son[x][0]]+sz[son[x][1]]+1;
 20 }
 21 
 22 inline void renew(int x,__int64 d)
 23 {
 24     if(!x) return;
 25     sum[x]+=sz[x]*d;
 26     val[x]+=d;
 27     dt[x]+=d;
 28 }
 29 
 30 inline void pushdown(int x)
 31 {
 32     if(!x||dt[x]==0) return;
 33     renew(son[x][0],dt[x]); renew(son[x][1],dt[x]);
 34     dt[x]=0;
 35 }
 36 
 37 inline void link(int x,int y,int c)
 38 {
 39     fa[x]=y; son[y][c]=x;
 40 }
 41 
 42 inline void rotate(int x,int c)
 43 {
 44     int y=fa[x];
 45     pushdown(y);
 46     pushdown(x);
 47     link(x,fa[y],son[fa[y]][1]==y);
 48     link(son[x][!c],y,c);
 49     link(y,x,!c);
 50     pushup(y);
 51 }
 52 
 53 inline void splay(int x,int g)
 54 {
 55     while(fa[x]!=g)
 56     {
 57         int y=fa[x];
 58         int cy=(son[fa[y]][1]==y),cx=(son[y][1]==x);
 59         if(fa[y]==g) rotate(x,cx);
 60         else
 61         {
 62             if(cx==cy) rotate(y,cy);
 63             else rotate(x,cx);
 64             rotate(x,cy);
 65         }
 66     }
 67     pushup(x);
 68     if(!g) root=x;
 69 }
 70 
 71 inline void newnode(int y,int &x,__int64 sp,int b)
 72 {
 73     x=++cnt;
 74     fa[x]=y; val[x]=sp; bh[x]=b; sum[x]=val[x]; dt[x]=0; sz[x]=1;
 75     son[x][0]=son[x][1]=0;
 76 }
 77 
 78 inline void init()
 79 {
 80     cnt=0;
 81     newnode(0,root,-INF,0);
 82     newnode(root,son[root][1],INF,n+1);
 83     sz[root]=2;
 84 }
 85 
 86 inline void insert(__int64 sp,int b)
 87 {
 88     int x=root;
 89     while(son[x][b>bh[x]]) x=son[x][b>bh[x]];
 90     newnode(x,son[x][b>bh[x]],sp,b);
 91     splay(cnt,0);
 92 }
 93 
 94 inline int getpos(int b)
 95 {
 96     int x=root;
 97     while(x)
 98     {
 99         if(bh[x]==b) return x;
100         x=son[x][b>bh[x]];
101     }
102 }
103 
104 inline void increase(int l,int r,__int64 sp)
105 {
106     int f1=getpos(l-1);
107     int f2=getpos(r+1);
108     splay(f1,0); splay(f2,f1);
109     renew(son[f2][0],sp);
110 }
111 
112 inline void getsum(int l,int r)
113 {
114     int f1=getpos(l-1);
115     int f2=getpos(r+1);
116     splay(f1,0); splay(f2,f1);
117     printf("%I64d\n",sum[son[f2][0]]);
118 }
119 
120 inline void go()
121 {
122     init();
123     for(int i=1,a;i<=n;i++)
124     {
125         scanf("%I64d",&a);
126         insert(a,i);
127     }
128     char str[10];__int64 c;
129     for(int i=1,a,b,c;i<=m;i++)
130     {
131         scanf("%s",str);
132         if(str[0]=='C')
133         {
134             scanf("%d%d%I64d",&a,&b,&c);
135             increase(a,b,c);
136         }
137         else
138         {
139             scanf("%d%d",&a,&b);
140             getsum(a,b);
141         }
142     }
143 }
144 
145 int main()
146 {
147     while(scanf("%d%d",&n,&m)!=EOF) go();
148     return 0;
149 }

 

BZOJ 1500:集Splay的大多数功能于一身~

View Code
  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdlib>
  4 #include <cstdio>
  5 #include <algorithm>
  6 
  7 #define N 500000
  8 #define INF 1e8
  9 #define BUG system("pause")
 10 
 11 using namespace std;
 12 
 13 int sz[N],sum[N],val[N],mx[N],lmx[N],rmx[N],fa[N],son[N][2];
 14 bool rev[N],lz[N];
 15 int q[N],h,ss[N];
 16 int n,m,posi,tot,num,root,cnt;
 17 
 18 inline void prt()
 19 {
 20     for(int i=1;i<=cnt;i++)    printf("%d    %d     %d     %d      %d     %d\n",i,son[i][0],son[i][1],sz[i],val[i],rev[i]);
 21 }
 22 
 23 inline void pushup(int x)
 24 {
 25     sz[x]=sz[son[x][0]]+sz[son[x][1]]+1;
 26     sum[x]=sum[son[x][0]]+sum[son[x][1]]+val[x];
 27     lmx[x]=max(lmx[son[x][0]],max(sum[son[x][0]]+val[x],sum[son[x][0]]+val[x]+lmx[son[x][1]]));
 28     rmx[x]=max(rmx[son[x][1]],max(sum[son[x][1]]+val[x],sum[son[x][1]]+val[x]+rmx[son[x][0]]));
 29     mx[x]=max(max(mx[son[x][0]],mx[son[x][1]]),val[x]+max(0,lmx[son[x][1]])+max(0,rmx[son[x][0]]));
 30 }
 31 
 32 inline void reverse(int x)
 33 {
 34     if(!x) return;
 35     rev[x]^=1;
 36     swap(son[x][0],son[x][1]);
 37     swap(lmx[x],rmx[x]);
 38 }
 39 
 40 inline void renew(int x,int sp)
 41 {
 42     if(!x) return;
 43     val[x]=sp; lz[x]=true;
 44     sum[x]=sz[x]*sp;
 45     lmx[x]=rmx[x]=mx[x]=max(sp,sum[x]);
 46 }
 47 
 48 inline void pushdown(int x)
 49 {
 50     if(!x) return;
 51     if(rev[x])
 52     {
 53         rev[x]=false;
 54         reverse(son[x][0]);
 55         reverse(son[x][1]);
 56     }
 57     if(lz[x])
 58     {
 59         lz[x]=false;
 60         renew(son[x][0],val[x]);
 61         renew(son[x][1],val[x]);
 62     }
 63 } 
 64 
 65 inline void link(int x,int y,int c)
 66 {
 67     fa[x]=y; son[y][c]=x;
 68 }
 69 
 70 inline void rotate(int x,int c)
 71 {
 72     int y=fa[x];
 73     link(x,fa[y],son[fa[y]][1]==y);
 74     link(son[x][!c],y,c);
 75     link(y,x,!c);
 76     pushup(y);
 77 }
 78 
 79 inline void splay(int x,int g)
 80 {
 81     while(fa[x]!=g)
 82     {
 83         int y=fa[x];
 84         pushdown(fa[y]); pushdown(y); pushdown(x);
 85         int cy=(son[fa[y]][1]==y),cx=(son[y][1]==x);
 86         if(fa[y]==g) rotate(x,cx);
 87         else 
 88         {
 89             if(cx==cy) rotate(y,cy);
 90             else rotate(x,cx);
 91             rotate(x,cy);
 92         }
 93     }
 94     pushup(x);
 95     if(!g) root=x;
 96 }
 97 
 98 inline int getnum()
 99 {
100     if(h==0) return ++cnt;
101     return q[h--];
102 }
103 
104 inline void newnode(int y,int &x,int sp)
105 {
106     x=getnum();
107     fa[x]=y; sz[x]=1; val[x]=sum[x]=mx[x]=lmx[x]=rmx[x]=sp; rev[x]=lz[x]=0;
108     son[x][0]=son[x][1]=0;
109 }
110 
111 inline void init()
112 {
113     cnt=0;
114     newnode(0,root,-INF);
115     newnode(root,son[root][1],INF);
116     sz[root]=2;
117 }
118 
119 inline void build(int &x,int l,int r,int g)
120 {
121     if(l>r) return;
122     int mid=(l+r)>>1;
123     newnode(g,x,ss[mid]);
124     build(son[x][0],l,mid-1,x);
125     build(son[x][1],mid+1,r,x);
126     pushup(x);
127 }
128 
129 inline int find(int b)
130 {
131     int x=root;
132     pushdown(x);
133     while(x)
134     {
135         if(sz[son[x][0]]==b) return x;
136         else if(sz[son[x][0]]>b) x=son[x][0];
137         else b-=sz[son[x][0]]+1,x=son[x][1];
138         pushdown(x);
139     }
140 }
141 
142 inline void recycle(int x)
143 {
144     if(!x) return;
145     q[++h]=x;
146     recycle(son[x][0]);
147     recycle(son[x][1]);
148 }
149 
150 inline void INSERT()
151 {
152     int x=find(posi),y=find(posi+1);
153     splay(x,0); splay(y,x);
154     build(son[y][0],1,tot,y);
155     pushup(y); pushup(x);
156 }
157 
158 inline void DELETE()
159 {
160     int x=find(posi-1),y=find(posi+tot);
161     splay(x,0); splay(y,x);
162     recycle(son[y][0]); 
163     fa[son[y][0]]=0;
164     son[y][0]=0;
165     pushup(y); pushup(x);
166 }
167 
168 inline void SAME()
169 {
170     int x=find(posi-1),y=find(posi+tot);
171     splay(x,0); splay(y,x);
172     renew(son[y][0],num);
173     pushup(y); pushup(x);
174 }
175 
176 inline void REVERSE()
177 {
178     int x=find(posi-1),y=find(posi+tot);
179     splay(x,0); splay(y,x);
180     reverse(son[y][0]);
181     pushup(y); pushup(x);
182 }
183 
184 inline void MAXSUM()
185 {
186     splay(1,0); splay(2,1);
187     printf("%d\n",mx[son[2][0]]);
188 }
189 
190 inline void GETSUM()
191 {
192     int x=find(posi-1),y=find(posi+tot);
193     splay(x,0); splay(y,x);
194     printf("%d\n",sum[son[y][0]]);
195 }
196 
197 inline void go()
198 {
199     init();
200     mx[0]=lmx[0]=rmx[0]=val[0]=-INF;
201     scanf("%d%d",&n,&m);
202     for(int i=1;i<=n;i++) scanf("%d",&ss[i]);
203     build(son[son[root][1]][0],1,n,son[root][1]);
204     pushup(son[root][1]);
205     pushup(root);
206     char str[20];
207     while(m--)
208     {
209         scanf("%s",str);
210         if(str[0]=='I')
211         {
212             scanf("%d%d",&posi,&tot);
213             for(int i=1;i<=tot;i++) scanf("%d",&ss[i]);
214             INSERT();
215         }
216         else if(str[0]=='D')
217         {
218             scanf("%d%d",&posi,&tot);
219             DELETE();
220         }
221         else if(str[0]=='M')
222         {
223             if(str[6]=='A')
224             {
225                 scanf("%d%d%d",&posi,&tot,&num);
226                 SAME();
227             }
228             else
229             {
230                 MAXSUM();
231             }
232         }
233         else if(str[0]=='R')
234         {
235             scanf("%d%d",&posi,&tot);
236             REVERSE();
237         }
238         else
239         {
240             scanf("%d%d",&posi,&tot);
241             GETSUM();
242         } 
243     }
244 }
245 
246 int main()
247 {
248     go();
249     return 0;
250 } 

 

复习专用。

数据结构裸题,没有什么思维含量,主要是模拟不要写错!!!!

 

posted @ 2013-01-15 10:29  proverbs  阅读(2830)  评论(0编辑  收藏  举报