题目
思路
这题比文艺平衡树还板子……
直接FHQ啊,就是要加一个垃圾栈(你没有看错,我也没有打错,就是垃圾栈
来对空间重复利用
code:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<iostream>
#include<cstdlib>
using namespace std;
inline long long read()
{
long long ret,c,f=1;
while (((c=getchar())> '9'||c< '0')&&c!='-');
if (c=='-') f=-1,ret=0;
else ret=c-'0';
while ((c=getchar())>='0'&&c<='9') ret=ret*10+c-'0';
return ret*f;
}
struct f{
long long val,key,l,r,siz,rev,tag,tp,lmx,rmx,sum,cmx;
} tree[500006];
long long cnt,n,m,root,stak[500006],tot;
long long New(long long x)//建新点,传真值
{
cnt=stak[tot--];
tree[cnt].siz=1;
tree[cnt].key=rand();
tree[cnt].l=tree[cnt].r=0;
tree[cnt].rev=tree[cnt].tag=0;
tree[cnt].lmx=tree[cnt].rmx=max(0ll,x);
tree[cnt].sum=tree[cnt].val=tree[cnt].cmx=tree[cnt].tp=x;
return cnt;
}
void Cov(long long id,long long x)
{
tree[id].tag=1;
tree[id].tp=tree[id].val=x;
tree[id].sum=tree[id].siz*x;
tree[id].cmx=max(x,tree[id].sum);
tree[id].lmx=tree[id].rmx=max(0ll,tree[id].siz*x);
return;
}
void Rev(long long id)
{
swap(tree[id].l,tree[id].r);
swap(tree[id].lmx,tree[id].rmx);
tree[id].rev^=1;
return;
}
void down(long long id)//标记下传
{
if (tree[id].rev)
{
tree[id].rev=0;
if (tree[id].l) Rev(tree[id].l);
if (tree[id].r) Rev(tree[id].r);
}
if (tree[id].tag)
{
tree[id].tag=0;
if (tree[id].l) Cov(tree[id].l,tree[id].tp);
if (tree[id].r) Cov(tree[id].r,tree[id].tp);
}
return;
}
void up(long long id)//上传
{
tree[id].siz=tree[tree[id].l].siz+tree[tree[id].r].siz+1;
tree[id].sum=tree[tree[id].l].sum+tree[tree[id].r].sum+tree[id].val;
tree[id].cmx=max(tree[id].val,tree[tree[id].l].rmx+tree[id].val+tree[tree[id].r].lmx);
if (tree[id].l) tree[id].cmx=max(tree[id].cmx,tree[tree[id].l].cmx);
if (tree[id].r) tree[id].cmx=max(tree[id].cmx,tree[tree[id].r].cmx);
tree[id].lmx=max(0ll,max(tree[tree[id].l].lmx,tree[tree[id].l].sum+tree[id].val+tree[tree[id].r].lmx));
tree[id].rmx=max(0ll,max(tree[tree[id].r].rmx,tree[tree[id].r].sum+tree[id].val+tree[tree[id].l].rmx));
return;
}
void split(long long id,long long siz,long long &l,long long &r)//分裂,把从1开始长度为siz的部分拆出来
{
if (id==0)
{
l=r=0;
return;
}
down(id);
if (siz>=tree[tree[id].l].siz+1)//递归左儿子
{
l=id;
split(tree[id].r,siz-1-tree[tree[id].l].siz,tree[id].r,r);
}
else//递归右儿子
{
r=id;
split(tree[id].l,siz,l,tree[id].l);
}
up(id);
return;
}
long long merge(long long l,long long r)//合并,传入根节点编号
{
if (l==0||r==0)
{
return l+r;
}
if (tree[l].key<tree[r].key)//维护小根堆性质
{
down(l);
tree[l].r=merge(tree[l].r,r);
up(l);
return l;
}
else
{
down(r);
tree[r].l=merge(l,tree[r].l);
up(r);
return r;
}
}
long long oo[1000005];
long long build(long long l,long long r)//建树
{
if (l==r)
{
return New(oo[l]);
}
long long mid=l+r>>1;
return merge(build(l,mid),build(mid+1,r));
}
void Erase(long long id)
{
stak[++tot]=id;
if (tree[id].l) Erase(tree[id].l);
if (tree[id].r) Erase(tree[id].r);
return;
}
int main()
{
for (int i=500000;i>=1;i--) stak[++tot]=i;
n=read(),m=read();
for (int i=1;i<=n;i++) oo[i]=read();
root=build(1,n);
while (m--)
{
string x;
cin>>x;
if (x=="GET-SUM")
{
long long y=read(),z=read(),l,r,r2;
split(root,y-1,l,r);
split(r,z,r,r2);
printf("%lld\n",tree[r].sum);
root=merge(merge(l,r),r2);
}
if (x=="INSERT")
{
long long y=read(),z=read(),l,r;
split(root,y,l,r);
for (int i=1;i<=z;i++) oo[i]=read();
root=merge(merge(l,build(1,z)),r);
}
if (x=="DELETE")
{
long long y=read(),z=read(),l,r,r2;
split(root,y-1,l,r);
split(r,z,r,r2);
Erase(r);
root=merge(l,r2);
}
if (x=="REVERSE")
{
long long y=read(),z=read(),l,r,r2;
split(root,y-1,l,r);
split(r,z,r,r2);
Rev(r);
root=merge(merge(l,r),r2);
}
if (x=="MAKE-SAME")
{
long long y=read(),z=read(),zz=read(),l,r,r2;
split(root,y-1,l,r);
split(r,z,r,r2);
Cov(r,zz);
root=merge(merge(l,r),r2);
}
if (x=="MAX-SUM")
{
printf("%lld\n",tree[root].cmx);
}
}
return 0;
}