【题目分析】
Splay维护区间操作。
SBT卡空间。
【代码】
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <queue>
using namespace std;
#define maxn 1000005
#define inf (0x3f3f3f3f)
int read()
{
int x=0,f=1; char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
int n,m,rt,st,ed,tot1=0,tot2=0;
int fa[maxn],ch[maxn][2],num[maxn],a[maxn],sta[maxn];
int sum[maxn],mx[maxn],lx[maxn],rx[maxn],tag[maxn],siz[maxn],rev[maxn];
char op[20];
void update(int k)
{
if (!k) return ;
int l=ch[k][0],r=ch[k][1];
sum[k]=sum[l]+sum[r]+num[k];
lx[k]=max(lx[l],sum[l]+num[k]+max(0,lx[r]));
rx[k]=max(rx[r],sum[r]+num[k]+max(0,rx[l]));
mx[k]=max(max(0,rx[l])+num[k]+max(0,lx[r]),max(mx[l],mx[r]));
siz[k]=siz[l]+siz[r]+1;
}
void uptag(int k,int x)
{
if (!k) return ;
tag[k]=num[k]=x;
sum[k]=x*siz[k];
mx[k]=lx[k]=rx[k]=max(x,sum[k]);
}
void uprev(int k)
{
if (!k) return ;
rev[k]^=1;
swap(ch[k][0],ch[k][1]);
swap(lx[k],rx[k]);
}
void pushdown(int k)
{
if (!k) return ;
int l=ch[k][0],r=ch[k][1];
if (rev[k]) {uprev(l);uprev(r);rev[k]=0;}
if (tag[k]!=-inf) {uptag(l,tag[k]);uptag(r,tag[k]);tag[k]=-inf;}
}
int newnode(int x)
{
int tmp;
if (tot2) tmp=sta[tot2--];
else tmp=++tot1;
num[tmp]=a[x]; tag[tmp]=-inf; rev[tmp]=0;
lx[tmp]=rx[tmp]=mx[tmp]=-inf;
return tmp;
}
int build (int l,int r)
{
int mid=(l+r)>>1,lc=0,rc=0;
if (l<mid) lc=build(l,mid-1);
int tmp=newnode(mid);
if (r>mid) rc=build(mid+1,r);
if (lc) ch[tmp][0]=lc,fa[lc]=tmp;
if (rc) ch[tmp][1]=rc,fa[rc]=tmp;
update(tmp);
return tmp;
}
void rot(int x,int &k)
{
int y=fa[x],z=fa[y],l,r;
if (ch[y][0]==x) l=0; else l=1;
r=l^1;
if (y==k) k=x;
else if (ch[z][0]==y) ch[z][0]=x; else ch[z][1]=x;
fa[x]=z; fa[y]=x; fa[ch[x][r]]=y;
ch[y][l]=ch[x][r]; ch[x][r]=y;
update(y);
update(x);
}
void relax(int x,int k)
{
if (x!=k) relax(fa[x],k);
pushdown(x);
}
void splay(int x,int &k)
{
relax(x,k);
while (x!=k)
{
int y=fa[x],z=fa[y];
if (y!=k)
{
if ((ch[y][0]==x)^(ch[z][0]==y)) rot(x,k);
else rot(y,k);
}
rot(x,k);
}
}
int find(int k,int x)
{
pushdown(k);
int l=ch[k][0],r=ch[k][1];
if (siz[l]+1==x) return k;
else if (siz[l]>=x) return find(l,x);
else return find(r,x-siz[l]-1);
}
void del(int &k)
{
if (!k) return ;
sta[++tot2]=k; fa[k]=0;
del(ch[k][0]); del(ch[k][1]);
lx[k]=rx[k]=mx[k]=-inf;
k=0;
}
void ins(int pos,int tot)
{
for (int i=1;i<=tot;++i) a[i]=read();
int x=find(rt,pos+1),y=find(rt,pos+2);
splay(x,rt); splay(y,ch[x][1]);
int tmp=build(1,tot);
fa[tmp]=y; ch[y][0]=tmp;
update(y); update(x);
}
void del(int pos,int tot)
{
int x=find(rt,pos),y=find(rt,pos+tot+1);
splay(x,rt);splay(y,ch[x][1]);
del(ch[y][0]);
update(y);update(x);
}
void soltag(int pos,int tot,int f)
{
int x=find(rt,pos),y=find(rt,pos+tot+1);
splay(x,rt); splay(y,ch[x][1]);
uptag(ch[y][0],f);
update(y);update(x);
}
void solrev(int pos,int tot)
{
int x=find(rt,pos),y=find(rt,pos+tot+1);
splay(x,rt); splay(y,ch[x][1]);
uprev(ch[y][0]);
update(y);update(x);
}
int getsum(int pos,int tot)
{
int x=find(rt,pos),y=find(rt,pos+tot+1);
splay(x,rt); splay(y,ch[x][1]);
return sum[ch[y][0]];
}
int getmax()
{
splay(st,rt);splay(ed,ch[rt][1]);
return mx[ch[ed][0]];
}
int main()
{
n=read(); m=read();
for (int i=2;i<=n+1;++i) a[i]=read();
a[st=1]=0; a[ed=n+2]=0;
rx[0]=lx[0]=mx[0]=-inf;
rt=build(1,n+2);
while (m--)
{
scanf("%s",op);
if (op[0]=='I') {int x=read(),y=read();ins(x,y);}
else if (op[0]=='D') {int x=read(),y=read();del(x,y);}
else if (op[2]=='K') {int x=read(),y=read(),z=read();soltag(x,y,z);}
else if (op[0]=='R') {int x=read(),y=read();solrev(x,y);}
else if (op[0]=='G') {int x=read(),y=read();printf("%d\n",getsum(x,y));}
else printf("%d\n",getmax());
}
}