比赛 2025暑期集训第2场 评测结果 WWWTTTTTTT
题目名称 序列操作 最终得分 0
用户昵称 汐汐很希希 运行时间 14.022 s
代码语言 C++ 内存使用 11.15 MiB
提交时间 2025-06-29 17:26:55
显示代码纯文本
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e5+10,INF=1e9;
ll n,m,a[N];
int rt=1;
struct Tree
{
    ll dmax;
    ll lmax;
    ll rmax;
    ll sum;
    ll add;
}f[4*N];
void pushup(int c,int L,int R)
{
    int lc=c*2,rc=c*2+1;
    f[c].sum=f[lc].sum+f[rc].sum;
    f[c].dmax=max(max(f[lc].dmax,f[rc].dmax),f[lc].rmax+f[rc].lmax);
    f[c].lmax=max(f[lc].lmax,f[lc].sum+f[rc].lmax);
    f[c].rmax=max(f[rc].rmax,f[rc].sum+f[lc].rmax);
    return;
}
void build(int c,int L,int R)
{
    if(L==R){
        f[c].dmax=a[L];
        f[c].lmax=a[L];
        f[c].rmax=a[L];
        f[c].sum=a[L];
        return;
    }
    int M=(L+R)/2;
    int lc=c*2,rc=c*2+1;
    build(lc,L,M);
    build(rc,M+1,R);
    pushup(c,L,R);
    return;
}
void pushdown(int c,int L,int R)
{
    if(f[c].add==0) return;
    int M=(L+R)/2,lc=c*2,rc=c*2+1;
    f[lc].sum=f[c].add*(M-L+1);
    f[rc].sum=f[c].add*(R-M);
    f[lc].add+=f[c].add;
    f[rc].add+=f[c].add;
    f[c].add=0;
}
void update_1(int c,int L,int R,int l,int r,int val)
{
    if(l<=L&&R<=r)
    {
        f[c].sum=val*(R-L+1);
        f[c].dmax=f[c].sum;
        f[c].lmax=f[c].sum;
        f[c].rmax=f[c].sum;
        f[c].add+=val;
        return;
    }
    pushdown(c,L,R);
    int M=(L+R)/2,lc=c*2,rc=c*2+1;
    if(l<=M) update_1(lc,L,M,l,r,val);
    if(M<r) update_1(rc,M+1,R,l,r,val);
    pushup(c,L,R);
    return;
}
void update_2(int c,int L,int R,int l,int r)
{
    if(l<=L&&R<=r)
    {
        for(int i=l;i<=r;i++) a[i]=a[i]^1;
        build(c,L,R);
        return;
    }
    pushdown(c,L,R);
    int M=(L+R)/2,lc=c*2,rc=c*2+1;
    if(l<=M) update_2(lc,L,M,l,r);
    if(M<r) update_2(rc,M+1,R,l,r);
    pushup(c,L,R);
    return;
}
Tree query(int c,int L,int R,ll l,ll r)
{
    if(l<=L&&R<=r) return f[c];
    int M=(L+R)/2;
    int lc=c*2,rc=c*2+1;
    Tree a={0,-INF,-INF,-INF},b=a,ans=a;
    if(l<=M) a=query(lc,L,M,l,r);
    if(M<r) b=query(rc,M+1,R,l,r);
    if(a.sum==-1e9) ans=b;
    else if(b.sum==-1e9) ans=a;
    else
    {
        ans.sum=a.sum+b.sum;
        ans.dmax=max(max(a.dmax,b.dmax),a.rmax+b.lmax);
        ans.lmax=max(a.lmax,a.sum+b.lmax);
        ans.rmax=max(b.rmax,b.sum+a.rmax);
    }
    return ans;
}
int main()
{
    freopen("sequence.in","r",stdin);
    freopen("sequence.out","w",stdout);
    
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    build(1,1,n);
    for(int T=1;T<=m;T++)
    {
        int t;
        scanf("%d",&t);
        ll x,y;
        scanf("%lld%lld",&x,&y);
        if(x>y) swap(x,y);
        if(t==0) update_1(rt,1,n,x,y,0);
        else if(t==1) update_1(rt,1,n,x,y,1);
        else if(t==2) update_2(rt,1,n,x,y);
        else{
            Tree ans=query(rt,1,n,x,y);
            if(t==3) cout<<ans.sum<<endl;
            else cout<<ans.dmax<<endl;
        }
    }
    return 0;
}