BZOJ 1500 [NOI2005]维修数列

Description

Input

输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目。
第2行包含N个数字,描述初始时的数列。
以下M行,每行一条命令,格式参见问题描述中的表格。
任何时刻数列中最多含有500 000个数,数列中任何一个数字均在[-1 000, 1 000]内。
插入的数字总数不超过4 000 000个,输入文件大小不超过20MBytes。

Output

对于输入数据中的GET-SUM和MAX-SUM操作,向输出文件依次打印结果,每个答案(数字)占一行。

Sample Input

9 8
2 -6 3 5 1 -5 -3 6 3
GET-SUM 5 4
MAX-SUM
INSERT 8 3 -5 7 2
DELETE 12 1
MAKE-SAME 3 3 2
REVERSE 3 6
GET-SUM 5 4
MAX-SUM

Sample Output

-1
10
1
10

HINT

Source

辣鸡题目,荒废了窝一天,喵。
首先六种操作,看着就很不爽。
甚至还有最大子段和。
注意:这题卡空间,需要回收节点。
询问的区间有可能为零,需要特判。
以及一系列。。。无力吐槽。
最后向stl低头,向快读低头,勉勉强强卡过了,可能常数太大了吧。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=500100;
const int inf=1e9+7;
int n,m,rt,cnt,a[N],val[N],fa[N],ch[N][2],chg[N],rev[N],sz[N],lx[N],rx[N],mx[N],sum[N],stk[N],tp;
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;
}
void pushup(int x)
{
	int lc=ch[x][0],rc=ch[x][1];
    sz[x]=sz[lc]+sz[rc]+1;
    sum[x]=sum[lc]+sum[rc]+val[x];
    lx[x]=max(lx[lc],sum[lc]+max(0,lx[rc])+val[x]);
    rx[x]=max(rx[rc],sum[rc]+max(0,rx[lc])+val[x]);
    mx[x]=max(max(mx[lc],mx[rc]),max(0,lx[rc])+max(0,rx[lc])+val[x]);
}
void pushdown(int x)
{
	int lc=ch[x][0],rc=ch[x][1];
    if(rev[x])
    {
        rev[lc]^=1,rev[ch[x][1]]^=1;
        swap(lx[lc],rx[lc]),swap(lx[rc],rx[rc]);
        swap(ch[lc][0],ch[lc][1]),swap(ch[rc][0],ch[rc][1]);
        rev[x]=0;
    }
    if(chg[x]<inf)
    {
        if(lc)
        {
            sum[lc]=sz[lc]*chg[x];
            val[lc]=chg[lc]=chg[x];
            lx[lc]=rx[lc]=mx[lc]=max(sum[lc],chg[x]);
        }
        if(rc)
        {
            sum[rc]=sz[rc]*chg[x];
            val[rc]=chg[rc]=chg[x];
            lx[rc]=rx[rc]=mx[rc]=max(sum[rc],chg[x]);
        }
        chg[x]=inf;
    }
}
void rotate(int x,int &k)
{
    int y=fa[x],z=fa[y],lc=ch[y][1]==x,rc=lc^1;
    if(y==k)
        k=x;
    else
        ch[z][ch[z][1]==y]=x;
    fa[y]=x,fa[x]=z,fa[ch[x][rc]]=y;
    ch[y][lc]=ch[x][rc],ch[x][rc]=y;
    pushup(y),pushup(x);
}
void splay(int x,int &k)
{
    while(x!=k)
    {
        int y=fa[x],z=fa[y];
        if(y!=k)
        {
            if((ch[y][0]==x)^(ch[z][0]==y))
                rotate(x,k);
            else
                rotate(y,k);
        }
        rotate(x,k);
    }
}
void del(int x)
{
    if(!x)
        return ;
    stk[++tp]=x;
    val[x]=sum[x]=lx[x]=rx[x]=mx[x]=0;
    del(ch[x][0]),del(ch[x][1]);
    fa[x]=ch[x][0]=ch[x][1]=rev[x]=sz[x]=0;
    chg[x]=inf;
}
int build(int l,int r,int f)
{
    if(l>r)
        return 0;
    int mid=(l+r)/2,x;
    if(!tp)
        x=++cnt;
    else
        x=stk[tp--];
    val[x]=a[mid];
    ch[x][0]=build(l,mid-1,x);
    ch[x][1]=build(mid+1,r,x);
    fa[x]=f;
    chg[x]=inf;
    pushup(x);
    return x;
}
void split(int x,int y)
{
    splay(x,rt),splay(y,ch[rt][1]);
}
int fnd(int x,int rnk)
{
    pushdown(x);
    if(rnk<=sz[ch[x][0]])
        return fnd(ch[x][0],rnk);
    if(rnk>sz[ch[x][0]]+1)
        return fnd(ch[x][1],rnk-sz[ch[x][0]]-1);
    return x;
}
int main()
{
    n=read(),m=read();
    for(int i=2;i<=n+1;i++)
        a[i]=read();
    a[1]=a[n+2]=-inf;
    mx[0]=lx[0]=rx[0]=-inf;
    rt=build(1,n+2,0);
    char s[11];
    int x,y,z;
    while(m--)
    {
        scanf("%s",s);
        if(s[0]=='I')
        {
            memset(a,0,sizeof(a));
            x=read(),y=read();
            for(int i=1;i<=y;i++)
                a[i]=read();
            int frt=build(1,y,0);
            int p=fnd(rt,x+1),q=fnd(rt,x+2);
            split(p,q);
            ch[q][0]=frt;
            fa[frt]=q;
            pushup(q),pushup(p);
        }
        if(s[0]=='D')
        {
            x=read(),y=read();
            int p=fnd(rt,x),q=fnd(rt,x+y+1);
            split(p,q);
            del(ch[q][0]);
            ch[q][0]=0;
            pushup(q),pushup(p);
        }
        if(s[0]=='M'&&s[2]=='K')
        {
            x=read(),y=read(),z=read();
            int p=fnd(rt,x),q=fnd(rt,x+y+1);
            split(p,q);
            int t=ch[q][0];
            val[t]=chg[t]=z;
            sum[t]=sz[t]*z;
            lx[t]=rx[t]=mx[t]=max(sum[t],z);
            pushup(q),pushup(p);
        }
        if(s[0]=='R')
        {
            x=read(),y=read();
            int p=fnd(rt,x),q=fnd(rt,x+y+1);
            split(p,q);
            int t=ch[q][0];
            rev[t]^=1;
            swap(lx[t],rx[t]);
            swap(ch[t][0],ch[t][1]);
            pushup(q),pushup(p);
        }
        if(s[0]=='G')
        {
            x=read(),y=read();
            if(y==0)
            {
                printf("0\n");
                continue;
            }
            int p=fnd(rt,x),q=fnd(rt,x+y+1);
            split(p,q);
            printf("%d\n",sum[ch[q][0]]);
        }
        if(s[0]=='M'&&s[2]=='X')
            printf("%d\n",mx[rt]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值