记录编号 |
433091 |
评测结果 |
AAAAAAAAAA |
题目名称 |
[NOI 2005]维护数列 |
最终得分 |
100 |
用户昵称 |
Anonymity |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
2.728 s |
提交时间 |
2017-08-04 19:32:06 |
内存使用 |
0.28 MiB |
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#define key(x)(x?x->v:0)
#define sz(x)(x?x->s:0)
#define sl(x)(x?x->lmax:0)
#define sr(x)(x?x->rmax:0)
#define sm(x)(x?x->ma:-0x3fffffff)
#define ss(x)(x?x->sum:0)
#define ssr(x)(x?x->rmax:-0x3fffffff)
#define ssl(x)(x?x->lmax:-0x3fffffff)
#define sss(x)(x?x->sum:-0x3fffffff)
#define skey(x)(x?x->v:-0x3fffffff)
using namespace std;
inline int read()
{ char c=getchar();int x=0,y=1;
while(c<'0'||c>'9'){if(c=='-') y=-1;c=getchar();}
while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
return x*y;
}
int n,m,inf=0x3fffffff;
struct Treap
{ int v,r,s,mk,sum,lmax,rmax,ma,se;
Treap *ch[2];
Treap(int x):v(x){ch[0]=ch[1]=NULL;s=1;se=-inf;r=rand();sum=lmax=rmax=ma=v;mk=0;}
void revs(){if(this){mk^=1;swap(ch[0],ch[1]);swap(lmax,rmax);}}
void addv(int x){if(this){se=x;v=x;sum=x*s;lmax=rmax=ma=max(x,sum);}}
void dn()
{ if(this)
{ if(mk){if(ch[0]) ch[0]->revs(); if(ch[1]) ch[1]->revs();mk=0;}
if(se!=-inf)
{ if(ch[0]) ch[0]->addv(se);
if(ch[1]) ch[1]->addv(se);
se=-inf;
}
}
}
void mt()
{ if(this)
{ s=1+sz(ch[0])+sz(ch[1]);
sum=v+ss(ch[0])+ss(ch[1]);
lmax=max(ssl(ch[0]),max(ss(ch[0])+v+sl(ch[1]),ss(ch[0])+v));
rmax=max(ssr(ch[1]),max(ss(ch[1])+v+sr(ch[0]),ss(ch[1])+v));
ma=max(max(sm(ch[0]),sm(ch[1])),max(sr(ch[0])+v+sl(ch[1]),max(max(sr(ch[0])+v,sl(ch[1])+v),v)));
}
}
~Treap(){if(this->ch[0]) delete this->ch[0];if(this->ch[1]) delete this->ch[1];}
}*root;
typedef pair<Treap*,Treap*> dt;
Treap* merge(Treap* x,Treap* y)
{ if(!x) return y;if(!y) return x;
if(x->r < y->r){x->dn();x->ch[1]=merge(x->ch[1],y);x->mt();return x;}
else{y->dn();y->ch[0]=merge(x,y->ch[0]);y->mt();return y;}
}
dt split(Treap* x,int k)
{ if(!x) return dt(NULL,NULL);
dt y;x->dn();
if(sz(x->ch[0])>=k) y=split(x->ch[0],k),x->ch[0]=y.second,x->mt(),y.second=x;
else y=split(x->ch[1],k-sz(x->ch[0])-1),x->ch[1]=y.first,x->mt(),y.first=x;
return y;
}
Treap* build(int len)
{ Treap* *st=new Treap*[len];Treap *las,*x,*ans;
int tail=0,tmp;
for(int i=1;i<=len;i++)
{ tmp=read();x=new Treap(tmp);
las=NULL;
while(tail&&st[tail-1]->r > x->r)
{
st[tail-1]->mt();las=st[tail-1];
st[--tail]=NULL;
}
if(tail) st[tail-1]->ch[1]=x;
x->ch[0]=las;
st[tail++]=x;
}
while(tail) st[--tail]->mt();
ans=st[0];
delete []st;
return ans;
}
void insert(int k,int len)
{ Treap *tmp=build(len);
dt t2=split(root,k);root=merge(t2.first,merge(tmp,t2.second));
}
void del(int k,int len)
{ dt x,y;
x=split(root,k-1);y=split(x.second,len);
root=merge(x.first,y.second);
delete y.first;y.first=NULL;
}
void reve(int k,int len)
{ dt x,y;
x=split(root,k-1);y=split(x.second,len);
y.first->revs();
root=merge(x.first,merge(y.first,y.second));
}
void updata(int k,int len,int val)
{ dt x,y;
x=split(root,k-1);y=split(x.second,len);
y.first->addv(val);
root=merge(x.first,merge(y.first,y.second));
}
void gsum(int k,int len)
{ dt x,y;
x=split(root,k-1);y=split(x.second,len);
printf("%d\n",ss(y.first));
root=merge(x.first,merge(y.first,y.second));
}
void cmp(Treap* x,Treap* y,Treap* &z)
{ if(z)
{ z->s=sz(x)+sz(y)+1;
z->sum=ss(x)+ss(y)+key(z);
z->lmax=max(ssl(x),max(ss(x)+sl(y)+key(z),ss(x)+key(z)));
z->rmax=max(ssr(y),max(ss(y)+sr(x)+key(z),ss(y)+key(z)));
z->ma=max(max(sm(x),sm(y)),max(sr(x)+sl(y)+key(z),max(sr(x)+key(z),max(sl(y)+key(z),key(z)))));
}
}
int main()
{ freopen("seq2005.in","r",stdin);
freopen("seq2005.out","w",stdout);
scanf("%d%d",&n,&m);
root=build(n);
char ord[20];int x,y,z;
for(int i=1;i<=m;i++)
{ scanf("%s",ord);
if(ord[0]=='I'){scanf("%d%d",&x,&y);insert(x,y);}
if(ord[0]=='D'){scanf("%d%d",&x,&y);del(x,y);}
if(ord[0]=='M'&&ord[2]=='K'){scanf("%d%d%d",&x,&y,&z);updata(x,y,z);}
if(ord[0]=='R'){scanf("%d%d",&x,&y);reve(x,y);}
if(ord[0]=='G'){scanf("%d%d",&x,&y);gsum(x,y);}
if(ord[0]=='M'&&ord[2]=='X') printf("%d\n",root->ma);
}
return 0;
}