【链接】
bzoj1500
【题目大意】
【解题报告】
splay终极模板题,详见splay总结
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=500005,INF=1e9;
int n,m,top,a[maxn];
struct Splay
{
Splay* son[2];
int num,s,sum,pre,suf,MAX,Save;
bool flip;
int Cmp(int &k){if (k==son[0]->s+1) return -1; if (k<son[0]->s+1) return 0; k-=son[0]->s+1; return 1;}
void Pushup(){
s=son[0]->s+son[1]->s+1; sum=son[0]->sum+son[1]->sum+num;
pre=max(son[0]->pre,max(son[0]->sum+num,son[0]->sum+num+son[1]->pre));
suf=max(son[1]->suf,max(son[1]->sum+num,son[1]->sum+num+son[0]->suf));
MAX=max(son[0]->MAX,son[1]->MAX);
MAX=max(MAX,max(max(0,son[0]->suf+son[1]->pre),max(son[0]->suf,son[1]->pre))+num);
}
}tem[maxn],*Null=tem,*ro=Null,*stk[maxn];
inline int Read()
{
int res=0,f=1;
char ch=getchar(),cc=ch;
while (ch<'0'||ch>'9') cc=ch,ch=getchar();
if (cc=='-') f=-1;
while (ch>='0'&&ch<='9') res=res*10+ch-48,ch=getchar();
return res*f;
}
void Pushflip(Splay* k)
{
swap(k->son[0],k->son[1]); swap(k->pre,k->suf); k->flip^=1;
}
void PushSave(Splay* k,int num)
{
k->num=k->Save=num; k->sum=k->s*num;
if (num>0) k->MAX=k->pre=k->suf=k->s*num; else k->MAX=k->pre=k->suf=num;
}
void Pushdown(Splay* k)
{
if (k->flip)
{
if (k->son[0]!=Null) Pushflip(k->son[0]);
if (k->son[1]!=Null) Pushflip(k->son[1]);
k->flip=0;
}
if (k->Save!=-INF)
{
if (k->son[0]!=Null) PushSave(k->son[0],k->Save);
if (k->son[1]!=Null) PushSave(k->son[1],k->Save);
k->Save=-INF;
}
}
void New(Splay* &k,int num)
{
k=stk[top--]; k->son[0]=k->son[1]=Null; k->flip=0; k->Save=-INF; k->s=1; k->num=k->sum=k->pre=k->suf=k->MAX=num;
}
Splay* Build(int L,int R)
{
if (L>R) return Null;
int mid=(R-L>>1)+L;
Splay* now; New(now,a[mid]);
now->son[0]=Build(L,mid-1); now->son[1]=Build(mid+1,R);
now->Pushup();
return now;
}
void Rotate(Splay* &k,int d)
{
Splay* t=k->son[d]; k->son[d]=t->son[d^1]; t->son[d^1]=k;
k->Pushup(); t->Pushup(); k=t;
}
void Splay_Work(Splay* &k,int x)
{
Pushdown(k);
int d1=k->Cmp(x);
if (d1>-1)
{
Splay* p=k->son[d1]; Pushdown(p);
int d2=p->Cmp(x);
if (d2>-1)
{
Splay_Work(p->son[d2],x);
if (d1==d2) Rotate(k,d1),Rotate(k,d1); else Rotate(k->son[d1],d2),Rotate(k,d1);
} else Rotate(k,d1);
}
}
void GetLR(Splay* &k,int L,int R)
{
L--; R++;
Splay_Work(k,L);
k->Cmp(R); Splay_Work(k->son[1],R);
}
void Insert(Splay* &k,int where,Splay* now)
{
GetLR(k,where+1,where);
k->son[1]->son[0]=now;
ro->son[1]->Pushup(); ro->Pushup();
}
void Delete_Work(Splay* k)
{
if (k==Null) return;
Delete_Work(k->son[0]); stk[++top]=k; Delete_Work(k->son[1]);
}
void Delete(Splay* &k,int L,int R)
{
GetLR(k,L,R);
Delete_Work(k->son[1]->son[0]);
k->son[1]->son[0]=Null;
ro->son[1]->Pushup(); ro->Pushup();
}
bool Check(char ch)
{
if ((ch>='A'&&ch<='Z')||ch=='-') return 1;
return 0;
}
int main()
{
n=Read(); m=Read();
for (int i=1; i<=n; i++) a[i]=Read();
top=0; Null->pre=Null->suf=Null->MAX=-INF;
for (int i=1; i<maxn; i++) stk[++top]=tem+i;
ro=Build(0,n+1);
GetLR(ro,2,n+1);
for (int i=1,L,R,where,tot; i<=m; i++)
{
char ch=getchar(),c1,c2;
while (!Check(ch)) ch=getchar();
c1=ch; getchar(); c2=getchar();
while (Check(ch)) ch=getchar();
if (c1=='I')
{
where=Read(); tot=Read();
for (int j=1; j<=tot; j++) a[j]=Read();
Insert(ro,where+1,Build(1,tot));
}
if (c1=='D') L=Read(),R=L+Read()-1,Delete(ro,L+1,R+1);
if (c1=='M'&&c2=='K') L=Read(),R=L+Read()-1,tot=Read(),GetLR(ro,L+1,R+1),PushSave(ro->son[1]->son[0],tot);
if (c1=='R') L=Read(),R=L+Read()-1,GetLR(ro,L+1,R+1),Pushflip(ro->son[1]->son[0]);
if (c1=='G') L=Read(),R=L+Read()-1,GetLR(ro,L+1,R+1),printf("%d\n",ro->son[1]->son[0]->sum);
if (c1=='M'&&c2=='X') GetLR(ro,2,ro->s-1),printf("%d\n",ro->son[1]->son[0]->MAX);
}
return 0;
}