[NOI2005]维护数列(区间splay)

时间:2021-02-23 15:49:18

[NOI2005]维护数列(luogu)

打这玩意儿真是要了我的老命

Description

请写一个程序,要求维护一个数列,支持以下 6 种操作:(请注意,格式栏 中的下划线‘ _ ’表示实际输入文件中的空格)

[NOI2005]维护数列(区间splay)

Code

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#define TAGNONE 10000001
using namespace std;
const int N=1e6+;
struct node
{
int size,rev,ch[],res,prer,prel,tag,fa,sum,val;
void clear()
{
size=rev=ch[]=ch[]=res=prer=prel=fa=sum=val=;
tag=TAGNONE;
}
}f[N];
int x,id[N],tot,n,ru[N],rt,pos,len,d[N],m,cnt;
char s[];
int get()
{
if(tot==) return ++cnt;
int x=ru[tot--];
return x;
}
void c_val(int x,int y)
{
if(!x) return ;
f[x].tag=f[x].val=y;
f[x].sum=y*f[x].size;
f[x].prel=f[x].prer=max(,f[x].sum);
f[x].res=max(y,f[x].sum);
}
void c_rev(int x)
{
swap(f[x].ch[],f[x].ch[]);
swap(f[x].prer,f[x].prel);
f[x].rev^=;
}
void push_up(int x)
{
int lc=f[x].ch[],rc=f[x].ch[];
f[x].size=f[lc].size+f[rc].size+;
f[x].sum=f[lc].sum+f[rc].sum+f[x].val;
f[x].res=max(max(f[lc].res,f[rc].res),f[lc].prer+f[rc].prel+f[x].val);
f[x].prel=max(f[lc].prel,f[lc].sum+f[rc].prel+f[x].val);
f[x].prer=max(f[rc].prer,f[rc].sum+f[lc].prer+f[x].val);
}
void push_down(int x)
{
int lc=f[x].ch[],rc=f[x].ch[];
if(f[x].tag!=TAGNONE)
{
c_val(lc,f[x].tag);
c_val(rc,f[x].tag);
f[x].tag=TAGNONE;
}
if(f[x].rev)
{
c_rev(lc),c_rev(rc);
f[x].rev=;
}
}
int kth(int k)
{
int x=rt;
while()
{
push_down(x);
int lc=f[x].ch[],rc=f[x].ch[];
if(f[lc].size+==k) return x;
else if(f[lc].size>=k) x=lc;
else k-=f[lc].size+,x=rc;
}
}
void rotate(int x)
{
int y=f[x].fa,z=f[y].fa;
int wh=(x==f[y].ch[]);
f[f[x].ch[wh^]].fa=y;
f[y].ch[wh]=f[x].ch[wh^];
f[x].ch[wh^]=y;
f[y].fa=x,f[x].fa=z;
if(z) f[z].ch[y==f[z].ch[]]=x;
push_up(y),push_up(x);
}
void splay(int x,int y)
{
for(int fx=f[x].fa;fx=f[x].fa,fx!=y;rotate(x))
if(f[fx].fa!=y) rotate((fx==f[f[fx].fa].ch[])^(x==f[fx].ch[])?x:fx);
if(!y) rt=x;
}
int split(int pos,int len)
{
int x=kth(pos),y=kth(pos+len+);
splay(x,),splay(y,x);
return f[y].ch[];
}
void creat(int x,int y)
{
f[x].val=f[x].sum=f[x].res=y;
f[x].prel=f[x].prer=max(y,);
f[x].rev=,f[x].tag=TAGNONE;
f[x].size=;
}
void build(int l,int r,int fa)
{
int mid=(l+r)>>;
int x=id[mid],fx=id[fa];
if(l==r) creat(x,d[mid]);
if(l<mid) build(l,mid-,mid);
if(mid<r) build(mid+,r,mid);
f[x].tag=TAGNONE,f[x].val=d[mid],f[x].fa=fx;
push_up(x);
f[fx].ch[mid>=fa]=x;
}
void insert(int x,int len)
{
for(int i=;i<=len;i++)
scanf("%d",&d[i]),id[i]=get();
build(,len,);
int a=kth(x+),b=kth(x+);
splay(a,),splay(b,a);
int c=id[(+len)>>];
f[c].fa=b,f[b].ch[]=c;
push_up(b),push_up(a);
}
void remove(int x)
{
if(f[x].ch[]) remove(f[x].ch[]);
if(f[x].ch[]) remove(f[x].ch[]);
ru[++tot]=x,f[x].clear();
}
void eraser(int k,int len)
{
int x=split(k,len),y=f[x].fa;
remove(x);
f[y].ch[]=;
push_up(y),push_up(f[y].fa);
}
void update(int pos,int len,int k)
{
int x=split(pos,len);
c_val(x,k);
push_up(f[x].fa),push_up(rt);
}
void rever(int pos,int len)
{
int x=split(pos,len);
if(f[x].tag!=TAGNONE) return;
c_rev(x);
push_up(f[x].fa),push_up(rt);
}
void query(int pos,int len)
{
int x=split(pos,len);
printf("%d\n",f[x].sum);
}
int main()
{
scanf("%d%d",&n,&m);
f[].res=d[]=d[n+]=-<<;
for(int i=;i<=n;i++) scanf("%d",&d[i+]);
for(int i=;i<=n+;i++) id[i]=i;
build(,n+,);
rt=(n+)>>,cnt=n+;
while(m--)
{
scanf("%s",s);
if(s[]=='M' && s[]=='A' && s[]=='X') printf("%d\n",f[rt].res);
else scanf("%d%d",&pos,&len);
if(s[]=='I') insert(pos,len);
if(s[]=='D') eraser(pos,len);
if(s[]=='M' && s[]=='A' && s[]=='K')
scanf("%d",&x),update(pos,len,x);
if(s[]=='R') rever(pos,len);
if(s[]=='G') query(pos,len);
}
return ;
}