BZOJ 1500 [NOI2005]维修数列 (splay)

题目大意:略

调了好久终于过了!

我犯了一个错误,虽然我记得在翻转pushdown的时候交换lx和rx

但我应该翻转的是左儿子和右儿子的lx和rx!而不是当前节点的lx和rx

因为pushup的时候是根据左右儿子的lx和rx更新的!

还有就是在find的时候下传标记,在转到根的时候pushup

很丧病的题

然后我的代码在洛谷上过了,可bzoj上迷之CE了,害得我的提交记录里多了13%的CE

  1 #include <cstdio>
  2 #include <algorithm>
  3 #include <cstring>
  4 #define root d[0].ch[1]
  5 #define il inline
  6 #define nu 7777
  7 #define inf 500000
  8 #define N 601000
  9 using namespace std;
 10 
 11 int n,m,hd,tl,tot;
 12 char qq[20];
 13 int ww[N],que[N*10];
 14 struct SPLAY{
 15     int fa,ch[2],sz,val,sum,lx,rx,tx,tag,rev;
 16 }d[N];
 17 il int idf(int x){return d[d[x].fa].ch[0]==x?0:1;}
 18 il void con(int x,int ff,int p){d[x].fa=ff,d[ff].ch[p]=x;}
 19 il int cre(int val)
 20 {
 21     int x=(hd>=tl)?que[tl++]:++tot;
 22     d[x].lx=d[x].rx=d[x].tx=d[x].val=d[x].sum=val;
 23     d[x].tag=nu,d[x].sz=1;return x;
 24 }
 25 il void des(int x)
 26 {
 27     memset(&d[x],0,sizeof(d[x]));
 28     que[++hd]=x;
 29 }
 30 il void pushup(int x)
 31 {
 32     #define ls d[x].ch[0]
 33     #define rs d[x].ch[1]
 34     d[x].sz=d[ls].sz+d[rs].sz+1;
 35     d[x].sum=d[ls].sum+d[rs].sum+d[x].val;
 36     if(ls&&rs)
 37     {
 38         d[x].lx=max(d[ls].lx,d[ls].sum+max(0,d[x].val+max(0,d[rs].lx)));
 39         d[x].rx=max(d[rs].rx,d[rs].sum+max(0,d[x].val+max(0,d[ls].rx)));
 40         d[x].tx=max(d[ls].tx,d[rs].tx);
 41         d[x].tx=max(d[x].tx,max(0,d[ls].rx)+d[x].val+max(0,d[rs].lx));
 42         d[x].tx=max(d[x].tx,max(d[x].lx,d[x].rx));
 43     }
 44     if(ls&&!rs)
 45     {
 46         d[x].lx=max(d[ls].lx,d[ls].sum+d[x].val);
 47         d[x].rx=max(d[ls].rx,0)+d[x].val;
 48         d[x].tx=max(d[ls].tx,max(d[x].lx,d[x].rx));
 49     }
 50     if(!ls&&rs)
 51     {
 52         d[x].lx=d[x].val+max(d[rs].lx,0);
 53         d[x].rx=max(d[rs].rx,d[rs].sum+d[x].val);
 54         d[x].tx=max(d[rs].tx,max(d[x].lx,d[x].rx));
 55     }
 56     if(!ls&&!rs)
 57     {
 58         d[x].lx=d[x].rx=d[x].tx=d[x].val;
 59     }
 60     #undef ls
 61     #undef rs
 62 }
 63 il void pushdown(int x)
 64 {
 65     #define ls d[x].ch[0]
 66     #define rs d[x].ch[1]
 67     if(d[x].tag!=nu)
 68     {
 69         d[ls].tag=d[rs].tag=d[ls].val=d[rs].val=d[x].tag;
 70         d[ls].lx=d[ls].rx=d[ls].tx=max(d[x].tag,d[x].tag*d[ls].sz);
 71         d[rs].lx=d[rs].rx=d[rs].tx=max(d[x].tag,d[x].tag*d[rs].sz);
 72         d[ls].sum=d[ls].val*d[ls].sz;
 73         d[rs].sum=d[rs].val*d[rs].sz;
 74         d[x].tag=nu,d[x].rev=0;
 75     }
 76     if(d[x].rev)
 77     {
 78         swap(d[x].ch[0],d[x].ch[1]);
 79         d[ls].rev^=1,d[rs].rev^=1,d[x].rev=0;
 80         swap(d[ls].lx,d[ls].rx);
 81         swap(d[rs].lx,d[rs].rx);
 82     }
 83     #undef ls
 84     #undef rs
 85 }
 86 il void rot(int x)
 87 {
 88     int y=d[x].fa;int ff=d[y].fa;int px=idf(x);int py=idf(y);
 89     con(d[x].ch[px^1],y,px),con(y,x,px^1),con(x,ff,py);
 90     pushup(y),pushup(x);
 91 }
 92 il void splay(int x,int to)
 93 {
 94     to=d[to].fa;
 95     while(d[x].fa!=to){
 96         int y=d[x].fa;
 97         if(d[y].fa==to) rot(x);
 98         else if(idf(y)==idf(x)) rot(y),rot(x);
 99         else rot(x),rot(x);
100     }
101 }
102 int find_pos(int p)
103 {
104     int x=root;
105     while(1)
106     {
107         pushdown(x);
108         if(d[d[x].ch[0]].sz>=p){
109             x=d[x].ch[0];
110             continue;
111         }p-=d[d[x].ch[0]].sz;
112         if(p==1) return x;
113         p--,x=d[x].ch[1];
114     }
115 }
116 int build(int l,int r,int ff)
117 {
118     if(l>r) return 0;
119     int mid=(l+r)>>1;
120     int x=cre(ww[mid]);
121     d[x].fa=ff;
122     d[x].ch[0]=build(l,mid-1,x);
123     d[x].ch[1]=build(mid+1,r,x);
124     pushup(x);
125     return x;
126 }
127 int gc()
128 {
129     int rett=0,fh=1;char p=getchar();
130     while(p<'0'||p>'9') {if(p=='-')fh=-1;p=getchar();}
131     while(p>='0'&&p<='9'){rett=(rett<<3)+(rett<<1)+p-'0';p=getchar();}
132     return rett*fh;
133 }
134 void Ins(int pos,int num)
135 {
136     for(int i=1;i<=num;i++) ww[i]=gc();
137     int x=find_pos(pos+1);splay(x,root);
138     int y=find_pos(pos+2);splay(y,d[root].ch[1]);
139     int rt=build(1,num,y);con(rt,y,0);
140     pushup(y),pushup(x);
141 }
142 void clr(int x)
143 {
144     if(d[x].ch[0]) clr(d[x].ch[0]);
145     if(d[x].ch[1]) clr(d[x].ch[1]);
146     des(x);
147 }
148 void Del(int pos,int num)
149 {
150     int x=find_pos(pos);splay(x,root);
151     int y=find_pos(pos+num+1);splay(y,d[x].ch[1]);
152     clr(d[y].ch[0]);d[y].ch[0]=0;
153     pushup(y),pushup(x);
154 }
155 void Same(int pos,int num,int val)
156 {
157     int x=find_pos(pos);splay(x,root);
158     int y=find_pos(pos+num+1);splay(y,d[x].ch[1]);
159     int rt=d[y].ch[0];
160     d[rt].val=d[rt].tag=val;
161     d[rt].sum=val*d[rt].sz;
162     d[rt].tx=d[rt].lx=d[rt].rx=max(d[rt].val,d[rt].sum);
163     pushup(y),pushup(x);
164 }
165 void Rev(int pos,int num)
166 {
167     int x=find_pos(pos);splay(x,root);
168     int y=find_pos(pos+num+1);splay(y,d[x].ch[1]);
169     int rt=d[y].ch[0];
170     d[rt].rev^=1;
171     swap(d[rt].lx,d[rt].rx);
172 }
173 int Getsum(int pos,int num)
174 {
175     int x=find_pos(pos);splay(x,root);
176     int y=find_pos(pos+num+1);splay(y,d[x].ch[1]);
177     int rt=d[y].ch[0];
178     return d[rt].sum;
179 }
180 /*
181 void pro_dfs(int x)
182 {
183     pushdown(x);
184     //printf("%d %d %d %d\n",x,d[x].ch[0],d[x].ch[1],d[x].val);
185     if(d[x].ch[0]) pro_dfs(d[x].ch[0]);
186     if(d[x].ch[1]) pro_dfs(d[x].ch[1]);
187     pushup(x);
188 }
189 void mid_dfs(int x)
190 {
191     pushdown(x);
192     if(d[x].ch[0]) mid_dfs(d[x].ch[0]);
193     printf("%d ",d[x].val);
194     if(d[x].ch[1]) mid_dfs(d[x].ch[1]);
195 }*/
196 int Maxsum()
197 {
198     int x=find_pos(1);splay(x,root);
199     int y=find_pos(n+2);splay(y,d[root].ch[1]);
200     //pro_dfs(root);
201     int rt=d[y].ch[0];
202     return d[rt].tx;
203 }
204 
205 
206 int main()
207 {
208     n=gc(),m=gc();
209     for(int i=1;i<=n;i++) ww[i]=gc();
210     ww[0]=ww[n+1]=-inf,hd=0,tl=1;
211     root=build(0,n+1,0);
212     int x,y,z;
213     for(int i=1;i<=m;i++)
214     {
215         scanf("%s",qq);
216         if(qq[2]=='S'){
217             x=gc(),y=gc();
218             Ins(x,y);n+=y;
219         }else if(qq[2]=='L'){
220             x=gc(),y=gc();
221             Del(x,y);n-=y;
222         }else if(qq[2]=='K'){
223             x=gc(),y=gc(),z=gc();
224             Same(x,y,z);
225         }else if(qq[2]=='V'){
226             x=gc(),y=gc();
227             Rev(x,y);
228         }else if(qq[2]=='T'){
229             x=gc(),y=gc();
230             printf("%d\n",Getsum(x,y));
231         }else{
232             printf("%d\n",Maxsum());
233         }
234     }
235     return 0;
236 }

 

posted @ 2018-09-24 22:35  guapisolo  阅读(141)  评论(0编辑  收藏  举报