[Splay] BZOJ1500: [NOI2005]维修数列
2017-02-28 19:52
405 查看
题意
题解
纯数据结构题,用来练代码能力很不错。就是用splay来维护这个序列,各种标记放上去,维护各种信息即可。
我们需要维护这些:
val:节点的数字
size:子树大小
sum:子树对应的区间的数字和
ml:子树对应区间的最大前缀和(长度>0)
mr:子树对应区间的最大后缀和(长度>0)
allm:子树对应区间的最大连续子段和(长度>0)
rev:翻转标记
same:修改标记
对于ml, mr, allm的维护,只需要讨论一下是否跨过中间节点即可。需要注意的一点是长度要大于0,也就是说当所有元素都为负数时,最大子段和应该是最大的某个数的值。
还有一个问题,因为可能会频繁的插入删除,所有这题需要回收内存才能不MLE。可以用new/delete动态访问。如果是事先开好数组的话,需要自己写个栈模拟内存池,把已经删除的位置放到栈里下次新建的时候就在这里用。
这种题目思路上完全没问题,主要在于调试时间。尽量要把代码写的清晰会有助于debug。
#include<cstdio> #include<algorithm> using namespace std; const int INF=1e+9; int max(int x,int y){ return x>y?x:y; } struct node{ int val,size,sum,ml,mr,allm; bool rev; int same; node* ch[2]; node(int v=0,node* son=NULL){ val=sum=v; size=1; rev=0; same=INF; ch[0]=ch[1]=son; ml=mr=allm=v; } int cmp(int &k){ if(k<=ch[0]->size) return 0; if(k>ch[0]->size+1){ k-=ch[0]->size+1; return 1; } return -1; } void maintain(){ sum=ch[0]->sum+ch[1]->sum+val; size=ch[0]->size+ch[1]->size+1; ml=max(ch[0]->ml,max(ch[0]->sum+val,ch[0]->sum+val+ch[1]->ml)); mr=max(ch[1]->mr,max(ch[1]->sum+val,ch[1]->sum+val+ch[0]->mr)); allm=max(max(val,ch[0]->mr+val+ch[1]->ml),max(max(ch[0]->mr+val,ch[1]->ml+val),max(ch[0]->allm,ch[1]->allm))); } void add_same(int x){ same=val=x; sum=val*size; ml=mr=allm=(val>0?sum:val); } void add_rev(){ swap(ml,mr); swap(ch[0],ch[1]); rev^=1; } void pushdown(){ if(same!=INF) ch[0]->add_same(same), ch[1]->add_same(same), same=INF; if(rev) ch[0]->add_rev(), ch[1]->add_rev(), rev=0; } } nil, *null=&nil, *root; typedef node* P_node; void NULL_init(){ null->val=null->sum=null->size=0; null->ml=null->mr=null->allm=-INF; null->ch[0]=null->ch[1]=null; } void rot(P_node &p,int d){ P_node k=p->ch[d^1]; p->ch[d^1]=k->ch[d]; k->ch[d]=p; p->maintain(); k->maintain(); p=k; } void Splay(P_node &p,int k){ p->pushdown(); int d1=p->cmp(k); if(d1!=-1){ P_node p2=p->ch[d1]; p2->pushdown(); int d2=p2->cmp(k); if(d2!=-1){ Splay(p2->ch[d2],k); if(d1==d2) rot(p,d1^1), rot(p,d1^1); else rot(p->ch[d1],d2^1), rot(p,d1^1); } else rot(p,d1^1); } } P_node build(int L,int R,int *Arr){ if(L>R) return null; int mid=(L+R)>>1; P_node p=new node(*(Arr+mid),null); p->ch[0]=build(L,mid-1,Arr); p->ch[1]=build(mid+1,R,Arr); p->maintain(); return p; } int getint(){ char ch=getchar(); int res=0,ff=1; while(!('0'<=ch&&ch<='9')){ if(ch=='-') ff=-1; ch=getchar(); } while('0'<=ch&&ch<='9') res=(res<<1)+(res<<3)+ch-'0', ch=getchar(); return res*ff; } int n,Q,b[4000005]; P_node Interval(int L,int R){ Splay(root,L-1); Splay(root->ch[1],R-L+2); return root->ch[1]->ch[0]; } void dfs_delete(P_node p){ if(p==null) return; dfs_delete(p->ch[0]); dfs_delete(p->ch[1]); delete p; } int main(){ freopen("bzoj1500.in","r",stdin); freopen("bzoj1500.out","w",stdout); scanf("%d%d",&n,&Q); for(int i=1;i<=n;i++) b[i]=getint(); NULL_init(); root=build(0,n+1,b); while(Q--){ char s[15]; scanf("%s",s); if(s[0]=='I'){ int pos=getint(),t=getint(); pos++; for(int i=1;i<=t;i++) b[i]=getint(); Splay(root,pos); Splay(root->ch[1],1); root->ch[1]->ch[0]=build(1,t,b); root->ch[1]->maintain(); root->maintain(); } else if(s[0]=='D'){ int pos=getint(),t=getint(); pos++; Interval(pos,pos+t-1); dfs_delete(root->ch[1]->ch[0]); root->ch[1]->ch[0]=null; root->ch[1]->maintain(); root->maintain(); } else if(s[0]=='R'){ int pos=getint(),t=getint(); pos++; Interval(pos,pos+t-1)->add_rev(); } else if(s[0]=='G'){ int pos=getint(),t=getint(); pos++; printf("%d\n",Interval(pos,pos+t-1)->sum); } else if(s[2]=='K'){ int pos=getint(),t=getint(),c=getint(); pos++; Interval(pos,pos+t-1)->add_same(c); } else{ printf("%d\n",Interval(2,root->size-1)->allm); } } }
相关文章推荐
- bzoj1500 [NOI2005]维修数列 splay
- bzoj 1500: [NOI2005]维修数列 -- splay
- 【bzoj1500】[NOI2005]维修数列 Splay
- BZOJ_1500_[NOI2005]维修数列_splay
- 【BZOJ1500】【NOI2005】维修数列(Splay)
- BZoj 1500 [NOI2005]维修数列 (Splay 模板)
- bzoj1500 [NOI2005]维修数列(splay)
- [NOI2005] [BZOJ1500] 维修数列 - splay
- 【Splay】BZOJ1500 [NOI2005]维修数列
- BZOJ 1500: [NOI2005]维修数列( splay )
- 【BZOJ1500】[NOI2005]维修数列【Splay】
- 【BZOJ1500】[NOI2005]维修数列 Splay
- 【splay】[noi2005] bzoj1500 维修数列
- BZOJ 1500 [NOI2005]维修数列 (splay)
- 【bzoj1500】[NOI2005]维修数列 Splay
- BZOJ 1500 [NOI2005]维修数列(splay)
- 【BZOJ】1500 [NOI2005]维修数列 【splay】
- 【BZOJ1500】[NOI2005]维修数列 Splay
- BZOJ 1500 [NOI2005]维修数列 Splay
- BZOJ 1500 [NOI 2005] 维修数列 (splay 模板)