cogs339 维修数列 ……

链接:http://cogs.pro/cogs/problem/problem.php?pid=339

题意……我还用说么……OI有史以来最毒瘤数据结构题……

只能说精神低落的时候来上一发,真是神清气爽,完全忘了自己的抑郁症……说实在的,做这道题之前我抑郁症又犯了,但做完这道题,抑郁症是什么?

这个效果大概就是我说的这个东西:

抑郁症患者表示忘记了自杀的念头,忘记了自己没有妹子的事实,忘记了各种催泪番的悲惨结局,忘记了四月是你的谎言,忘记了白色相簿2,忘记了秒速5厘米,忘记了未闻花名,忘记了CLANNAD,忘记了日在校园(滑稽),忘记了零使小说原结局,忘记了FSN天天讲的“不可能每个人都幸福”,忘记了各种电视剧的悲剧结局,忘记了各种村上式结局,忘记了社会的阴暗面,只看见了人生的美好……

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 using namespace std;
  6 const int inf=0x3f3f3f3f;
  7 int n,m,cnt,c[500010];
  8 struct node
  9 {
 10     int lsum,rsum,totsum,maxsum,size,dk,dv,v,k;
 11     bool rev;
 12     node *ch[2],*pa;
 13     void init(int kk, int vv)
 14     {
 15         k=kk;v=vv;size=1;
 16         dv=inf;
 17         totsum=lsum=rsum=maxsum=v;
 18         ch[0]=ch[1]=pa=0;dk=rev=0;
 19     }
 20 }*root,*MIN,*MAX,*p;
 21 void pushdown(node *x)
 22 {
 23     if(x->dv<inf)
 24     {
 25         x->v=x->dv; x->totsum=x->v*x->size;
 26         x->lsum=x->rsum=x->maxsum=max(x->v,x->totsum);
 27         if(x->ch[0])x->ch[0]->dv=x->dv; 
 28         if(x->ch[1])x->ch[1]->dv=x->dv;
 29         x->dv=inf;
 30     }
 31     if(x->dk)
 32     {
 33         x->k+=x->dk;
 34         if(x->ch[0])x->ch[0]->dk+=x->dk; 
 35         if(x->ch[1])x->ch[1]->dk+=x->dk;
 36         x->dk=0;
 37     }
 38     if(x->rev)
 39     {
 40         int sl=0, sr=0;
 41         if(x->ch[0])sl=x->ch[0]->size,x->ch[0]->rev=!(x->ch[0]->rev);
 42         if(x->ch[1])sr=x->ch[1]->size,x->ch[1]->rev=!(x->ch[1]->rev);
 43         x->k=x->k-sl+sr;
 44         if(x->ch[0])x->ch[0]->dk+=sr+1;
 45         if(x->ch[1])x->ch[1]->dk+=-sl-1;
 46         swap(x->ch[0],x->ch[1]);
 47         swap(x->lsum,x->rsum);
 48         x->rev=false;
 49     }
 50 }
 51 void update(node *x)
 52 {
 53     pushdown(x);
 54     if(x->ch[0])pushdown(x->ch[0]);
 55     if(x->ch[1])pushdown(x->ch[1]);
 56     int t;
 57     x->size=1;
 58     if(x->ch[0])x->size+=x->ch[0]->size;
 59     if(x->ch[1])x->size+=x->ch[1]->size;
 60     x->totsum=x->v;
 61     if(x->ch[0])x->totsum+=x->ch[0]->totsum;
 62     if(x->ch[1])x->totsum+=x->ch[1]->totsum;
 63     t=x->v;
 64     if(x->ch[0])t+=max(0,x->ch[0]->rsum);
 65     if(x->ch[1])t+=max(0,x->ch[1]->lsum);
 66     if(x->ch[0])t=max(t,x->ch[0]->maxsum);
 67     if(x->ch[1])t=max(t,x->ch[1]->maxsum);
 68     x->maxsum=t;
 69     if(x->ch[0])
 70     {
 71         t=max(x->ch[0]->lsum,x->ch[0]->totsum+x->v);
 72         if(x->ch[1])t=max(t,x->ch[0]->totsum + x->v + max(0,x->ch[1]->lsum) );
 73     }
 74     else if(x->ch[1])t=x->v+max(0,x->ch[1]->lsum);
 75     x->lsum=t;
 76     if(x->ch[1])
 77     {
 78         t=max(x->ch[1]->rsum,x->v+x->ch[1]->totsum);
 79         if(x->ch[0])t=max(t,x->ch[1]->totsum + x->v + max(0,x->ch[0]->rsum));
 80     }
 81     else if(x->ch[0])t=x->v+max(0,x->ch[0]->rsum);
 82     x->rsum=t;
 83 }
 84 void zig(node *x)
 85 {
 86     node *y=x->pa;
 87     pushdown(y),pushdown(x);
 88     if(y->pa->ch[0]==y)y->pa->ch[0]=x; 
 89     else y->pa->ch[1]=x;
 90     x->pa=y->pa;
 91     y->ch[0]=x->ch[1]; if(x->ch[1])x->ch[1]->pa=y;
 92     x->ch[1]=y;y->pa=x;
 93     update(y);update(x);
 94 }
 95 void zag(node *x)
 96 {
 97     node *y=x->pa;
 98     pushdown(y),pushdown(x);
 99     if(y->pa->ch[0]==y)y->pa->ch[0]=x; else y->pa->ch[1]=x;
100     x->pa=y->pa;
101     y->ch[1]=x->ch[0]; if(x->ch[0])x->ch[0]->pa=y;
102     x->ch[0]=y;y->pa=x;
103     update(y);update(x);
104 }
105 void splay(node *x, node *F)
106 {
107     while(x->pa!=F)
108     {
109         node *y=x->pa;
110         if(y->pa==F)
111             if(y->ch[0]==x)zig(x);
112             else zag(x);
113         else
114             if(y->pa->ch[0]==y)
115                 if(y->ch[0]==x)zig(y),zig(x);
116                 else zag(x),zig(x);
117             else
118                 if(y->ch[1]==x)zag(y),zag(x);
119                 else zig(x),zag(x);
120     }
121 }
122 node* find(node *x, int k)
123 {
124     node *t;
125     pushdown(x);
126     if(x->k==k)t=x;
127     if(x->k>k)t=find(x->ch[0],k);
128     if(x->k<k)t=find(x->ch[1],k);
129     update(x);
130     return t;
131 }
132 node* get(int k, int v)
133 {
134     node *t;
135     t=p;p=p->pa;
136     if(p->ch[0]==t)p->ch[0]=0;else p->ch[1]=0;
137     while(p->ch[0]||p->ch[1])
138         if(p->ch[0])p=p->ch[0];else p=p->ch[1];
139     t->init(k,v);
140     return t;
141 }
142 void ins(node *t)
143 {
144     p->ch[0]=t;t->pa=p;
145     while(p->ch[0]||p->ch[1])
146         if(p->ch[0])p=p->ch[0];
147         else p=p->ch[1];
148 }
149 node* build(int l, int r)
150 {
151     int mid=(l+r)>>1;
152     node *TMP=get(mid,c[mid]);
153     if(l==r)return TMP;
154     if(l!=mid)
155     {
156         TMP->ch[0]=build(l,mid-1);
157         TMP->ch[0]->pa=TMP;
158     }
159     TMP->ch[1]=build(mid+1,r),TMP->ch[1]->pa=TMP;
160     update(TMP);
161     return TMP;
162 }
163 int haha()
164 {
165     freopen("seq2005.in","r",stdin);
166     freopen("seq2005.out","w",stdout);
167     char s[25],ch;int x,pos,tot;
168     node *t,*tmp;
169     p=new node;
170     for(int i=2;i<=500005;i++)
171     {
172         t=new node;
173         t->init(0,0);
174         t->pa=p,p->ch[1]=t;
175         p=t;
176     }
177     scanf("%d%d",&n,&m);
178     {
179         cnt=0;
180         MIN=get(0,0);
181         MAX=get(1,0);
182         root=get(123,0);
183         root->ch[0]=MIN;MIN->pa=root;
184         MIN->ch[1]=MAX;MAX->pa=MIN;
185         for(int i=1;i<=n;i++)scanf("%d",&c[i]);
186         MAX->k+=n;
187         MAX->ch[0]=build(1,n);MAX->ch[0]->pa=MAX;
188         update(MAX);update(MIN);
189         update(MAX);update(MIN);
190         for(int i=1;i<=m;i++)
191         {
192             scanf("%s",s);
193             switch(s[0])
194             {
195                 case 'I':scanf("%d%d",&pos,&tot);
196                             for(int j=pos+1;j<=pos+tot;j++)scanf("%d",&c[j]);
197                             tmp=find(root->ch[0],pos);
198                             t=find(root->ch[0],pos+1);
199                             splay(tmp,root);splay(t,tmp);
200                             t->dk+=tot;pushdown(t);
201                             t->ch[0]=build(pos+1,pos+tot);
202                             t->ch[0]->pa=t;
203                             update(t);update(tmp);
204                             break;
205                 case 'D':scanf("%d%d",&pos,&tot);
206                             if(!tot)break;
207                             tmp=find(root->ch[0],pos-1);t=find(root->ch[0],pos+tot);
208                             splay(tmp,root);splay(t,tmp);
209                             ins(t->ch[0]);t->ch[0]=0;
210                             t->dk-=tot;pushdown(t);
211                             update(t);update(tmp);
212                             break;
213                 case 'R':scanf("%d%d",&pos,&tot);
214                             tmp=find(root->ch[0],pos-1);
215                             t=find(root->ch[0],pos+tot);
216                             splay(tmp,root);splay(t,tmp);
217                             update(tmp);
218                             t->ch[0]->rev=!(t->ch[0]->rev);
219                             break;
220                 case 'G':scanf("%d%d",&pos,&tot);
221                             if(!tot)
222                             {
223                                 puts("0");
224                                 break;
225                             }
226                            tmp=find(root->ch[0],pos-1);t=find(root->ch[0],pos+tot);
227                            splay(tmp,root);splay(t,tmp);
228                            printf("%d\n",t->ch[0]->totsum);
229                            break;
230                 case 'M':
231                         if(s[2]=='K')
232                         {
233                             scanf("%d%d%d",&pos,&tot,&c[0]);
234                             if(!tot)break;
235                             tmp=find(root->ch[0],pos-1);t=find(root->ch[0],pos+tot);
236                             splay(tmp,root);splay(t,tmp);
237                             t->ch[0]->dv=c[0];pushdown(t->ch[0]);
238                             update(t);update(tmp);
239                             break;
240                         }
241                         splay(MIN,root);splay(MAX,MIN);
242                         pushdown(MAX->ch[0]);
243                         printf("%d\n",MAX->ch[0]->maxsum);
244             }
245         }
246     }
247 }
248 int sb=haha();
249 int main(){;}
cogs339

 

posted @ 2017-07-31 21:02  ccc000111  阅读(156)  评论(0编辑  收藏  举报