【BZOJ1500】【NOI2005】维修数列(Splay)
2017-12-29 16:24
417 查看
题面
不想再看见这种毒瘤题,自己去BZOJ看题解
Splay良心模板题真的很简单
我一言不发
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> #include<set> #include<map> #include<vector> #include<queue> using namespace std; #define ll long long #define MAX 555555 #define INF 1000000000 #define lson (t[x].ch[0]) #define rson (t[x].ch[1]) inline int read() { int x=0,t=1;char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-')ch=getchar(); if(ch=='-')t=-1,ch=getchar(); while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar(); return x*t; } int root,n,m; int A[MAX]; queue<int> Q; int Qsize; struct Node { int ch[2],ff; ll sum,v,size; int rev,tag; ll lm,rm,mm; void init(int w) { sum=v=w;size=1; rev=tag=0; lm=rm=max(0,w); mm=w; } }t[MAX]; void pushup(int x) { t[x].sum=t[lson].sum+t[rson].sum+t[x].v; t[x].size=t[lson].size+t[rson].size+1; t[x].lm=max(t[lson].lm,t[lson].sum+t[x].v+t[rson].lm); t[x].rm=max(t[rson].rm,t[rson].sum+t[x].v+t[lson].rm); t[x].mm=max(max(t[lson].mm,t[rson].mm),t[lson].rm+t[x].v+t[rson].lm); } void rotate(int x) { int y=t[x].ff,z=t[y].ff; int k=t[y].ch[1]==x; t[z].ch[t[z].ch[1]==y]=x;t[x].ff=z; t[y].ch[k]=t[x].ch[k^1];t[t[x].ch[k^1]].ff=y; t[x].ch[k^1]=y;t[y].ff=x; pushup(y);pushup(x); } void pushdown(int x) { if(t[x].tag)//区间覆盖 { t[x].tag=t[x].rev=0; if(lson)t[lson].tag=1,t[lson].v=t[x].v,t[lson].sum=t[lson].size*t[x].v; if(rson)t[rson].tag=1,t[rson].v=t[x].v,t[rson].sum=t[rson].size*t[x].v; if(t[x].v>=0) { if(lson)t[lson].lm=t[lson].rm=t[lson].mm=t[lson].sum; if(rson)t[rson].lm=t[rson].rm=t[rson].mm=t[rson].sum; } else { if(lson)t[lson].lm=t[lson].rm=0,t[lson].mm=t[x].v; if(rson)t[rson].lm=t[rson].rm=0,t[rson].mm=t[x].v; } } if(t[x].rev) { t[x].rev^=1; if(lson)t[lson].rev^=1; if(rson)t[rson].rev^=1; swap(t[lson].lm,t[lson].rm); swap(t[rson].rm,t[rson].lm); swap(t[lson].ch[0],t[lson].ch[1]); swap(t[rson].ch[0],t[rson].ch[1]); } } void Splay(int x,int goal) { while(t[x].ff!=goal) { int y=t[x].ff,z=t[y].ff; if(z!=goal) (t[y].ch[1]==x)^(t[z].ch[1]==y)?rotate(x):rotate(y); rotate(x); } if(!goal)root=x; } int Rank(int k) { int x=root; while(x) { pushdown(x); if(t[lson].size+1==k)return x; if(t[lson].size+1<k)k-=t[lson].size+1,x=rson; else x=lson; } } int Build(int l,int r,int ff) { if(l>r)return 0; int mid=(l+r)>>1; int x=Q.front();Q.pop();Qsize--; //printf("Qsize=====%d\n",Qsize); if(l==r) { t[x].ff=ff; t[x].init(A[mid]); return x; } t[x].v=t[x].mm=A[mid];t[x].ff=ff; lson=Build(l,mid-1,x); rson=Build(mid+1,r,x); pushup(x); return x; } void Insert() { int pos=read(),tt=read(); for(int i=1;i<=tt;++i)A[i]=read(); if(!tt)return; int L=Rank(pos+1),R=Rank(pos+2); Splay(L,0);Splay(R,L); t[R].ch[0]=Build(1,tt,R); pushup(R);pushup(L); } void Clear(int x) { t[x].ch[0]=t[x].ch[1]=t[x].ff=t[x].size=t[x].sum=t[x].lm=t[x].rm=0; t[x].mm=-INF; t[x].rev=t[x].tag=0; } void Reuse(int x) { if(!x)return; Q.push(x);Qsize++; if(lson)Reuse(lson); if(rson)Reuse(rson); Clear(x); } void Delete() { int pos=read(),tt=read(); if(!tt)return; int L=Rank(pos),R=Rank(pos+tt+1); Splay(L,0);Splay(R,L); Reuse(t[R].ch[0]); t[R].ch[0]=0; pushup(R);pushup(L); } void SetVal() { int pos=read(),tt=read(); int vv=read(); int L=Rank(pos),R=Rank(pos+tt+1); Splay(L,0);Splay(R,L); int x=t[R].ch[0]; t[x].v=vv; t[x].tag=1; t[x].sum=t[x].size*vv; if(vv>=0)t[x].lm=t[x].rm=t[x].mm=t[x].sum; else t[x].lm=t[x].rm=0,t[x].mm=vv; pushup(R);pushup(L); } void Reverse() { int pos=read(),tt=read(); int L=Rank(pos),R=Rank(pos+tt+1); Splay(L,0);Splay(R,L); int x=t[R].ch[0]; if(!t[x].tag) { t[x].rev^=1; swap(lson,rson); swap(t[x].lm,t[x].rm); pushup(R);pushup(L); } } void Query_Sum() { int pos=read(),tt=read(); int L=Rank(pos),R=Rank(pos+tt+1); Splay(L,0);Splay(R,L); printf("%lld\n",t[t[R].ch[0]].sum); } void Query_Max_Sum() { printf("%lld\n",t[root].mm); } int main() { n=read();m=read(); A[1]=A[n+2]=-INF; Clear(0); for(int i=1;i<=550000;++i)Q.push(i);Qsize=550000; for(int i=1;i<=n;++i)A[i+1]=read(); root=Build(1,n+2,0); char ch[50]; while(m--) { scanf("%s",ch); if(ch[0]=='G')Query_Sum(); if(ch[0]=='D')Delete(); if(ch[0]=='R')Reverse(); if(ch[0]=='M'&&ch[2]=='K')SetVal(); if(ch[0]=='M'&&ch[2]=='X')Query_Max_Sum(); if(ch[0]=='I')Insert(); } }
相关文章推荐
- BZOJ 1500: [NOI2005]维修数列 Splay
- BZoj 1500 [NOI2005]维修数列 (Splay 模板)
- 【bzoj1500】[NOI2005]维修数列 Splay
- BZOJ 1500 NOI 2005 维修数列 Splay
- [BZOJ 1500][NOI2005]维修数列(Splay)
- [BZOJ1500][NOI2005]维修数列(Splay)
- BZOJ_1500_[NOI2005]维修数列_splay
- [bzoj1500][NOI2005 维修数列] (splay区间操作)
- 【Splay】BZOJ1500 [NOI2005]维修数列
- 【splay】BZOJ 1500: [NOI2005]维修数列
- BZOJ1500(NOI2005)[维修数列]--splay
- BZOJ 1500|NOI 2005|维修数列|Splay
- BZOJ 1500 [NOI2005]维修数列 Splay
- BZOJ1500: [NOI2005]维修数列 Splay
- [Splay] BZOJ1500: [NOI2005]维修数列
- [BZOJ1500][NOI2005]维修数列(splay)
- 【bzoj1500】[NOI2005]维修数列 Splay
- bzoj 1500: [NOI2005]维修数列 -- splay
- 【BZOJ1500】[NOI2005]维修数列 Splay
- 【bzoj1500】[NOI2005]维修数列 Splay