求调,玄关

P2572 [SCOI2010] 序列操作

Foraver @ 2024-11-26 21:43:31

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,lazy_a[10000005],lazy_b[10000005];
struct stu
{
    int cnt1,cnt0;
    int lsum1,lsum0;
    int rsum1,rsum0;
    int maxsum1,maxsum0;
}tree[10000005];
stu merge(stu a,stu b)
{
    stu c;
    c.cnt0=a.cnt0+b.cnt0;
    c.cnt1=a.cnt1+b.cnt1;
    c.lsum0=a.lsum0;
    if(a.cnt1==0)c.lsum0+=b.lsum0;
    c.lsum1=a.lsum1;
    if(a.cnt0==0)c.lsum1+=b.lsum1;
    c.rsum0=b.rsum0;
    if(b.cnt1==0)c.rsum0+=a.rsum0;
    c.rsum1=b.rsum1;
    if(b.cnt0==0)c.rsum1+=a.rsum1;
    c.maxsum0=max(a.maxsum0,b.maxsum0);
    c.maxsum0=max(c.maxsum0,a.rsum0+b.lsum0);
    c.maxsum1=max(a.maxsum1,b.maxsum1);
    c.maxsum1=max(c.maxsum1,a.rsum1+b.lsum1);
    return c;
}
void push_down(int u,int l,int r)
{
    if(lazy_a[u]==1)
    {
        int mid=(l+r)/2;
        lazy_a[u*2]=lazy_a[u];
        lazy_a[u*2+1]=lazy_a[u];
        lazy_b[u*2]=0;
        lazy_b[u*2+1]=0;
        lazy_a[u]=0;
        lazy_b[u]=0;
        tree[u*2].cnt0=mid-l+1;
        tree[u*2].cnt1=0;
        tree[u*2].lsum0=mid-l+1;
        tree[u*2].lsum1=0;
        tree[u*2].rsum0=mid-l+1;
        tree[u*2].rsum1=0;
        tree[u*2].maxsum0=mid-l+1;
        tree[u*2].maxsum1=0;
        tree[u*2+1].cnt0=r-mid;
        tree[u*2+1].cnt1=0;
        tree[u*2+1].lsum0=r-mid;
        tree[u*2+1].lsum1=0;
        tree[u*2+1].rsum0=r-mid;
        tree[u*2+1].rsum1=0;
        tree[u*2+1].maxsum0=r-mid;
        tree[u*2+1].maxsum1=0;
    }
    else if(lazy_a[u]==2)
    {
        int mid=(l+r)/2;
        lazy_a[u*2]=lazy_a[u];
        lazy_a[u*2+1]=lazy_a[u];
        lazy_b[u*2]=0;
        lazy_b[u*2+1]=0;
        lazy_a[u]=0;
        lazy_b[u]=0;
        tree[u*2].cnt0=0;
        tree[u*2].cnt1=mid-l+1;
        tree[u*2].lsum0=0;
        tree[u*2].lsum1=mid-l+1;
        tree[u*2].rsum0=0;
        tree[u*2].rsum1=mid-l+1;
        tree[u*2].maxsum0=0;
        tree[u*2].maxsum1=mid-l+1;
        tree[u*2+1].cnt0=0;
        tree[u*2+1].cnt1=r-mid;
        tree[u*2+1].lsum0=0;
        tree[u*2+1].lsum1=r-mid;
        tree[u*2+1].rsum0=0;
        tree[u*2+1].rsum1=r-mid;
        tree[u*2+1].maxsum0=0;
        tree[u*2+1].maxsum1=r-mid;
    }
    else if(lazy_b[u]==1)
    {
        swap(tree[u*2].cnt0,tree[u*2].cnt1);
        swap(tree[u*2].lsum0,tree[u*2].lsum1);
        swap(tree[u*2].rsum0,tree[u*2].rsum1);
        swap(tree[u*2].maxsum0,tree[u*2].maxsum1);
        swap(tree[u*2+1].cnt0,tree[u*2].cnt1);
        swap(tree[u*2+1].lsum0,tree[u*2].lsum1);
        swap(tree[u*2+1].rsum0,tree[u*2].rsum1);
        swap(tree[u*2+1].maxsum0,tree[u*2].maxsum1);
        if(lazy_a[u*2]!=0)
        {
            if(lazy_a[u*2]==1)lazy_a[u*2]=2;
            else lazy_a[u*2]=1;
        }
        else
        {
            if(lazy_b[u*2]==1)lazy_b[u*2]=0;
            else lazy_b[u*2]=1;
        }
        if(lazy_a[u*2+1]!=0)
        {
            if(lazy_a[u*2+1]==1)lazy_a[u*2+1]=2;
            else lazy_a[u*2+1]=1;
        }
        else
        {
            if(lazy_b[u*2+1]==1)lazy_b[u*2+1]=0;
            else lazy_b[u*2+1]=1;
        }
        lazy_a[u]=0;
        lazy_b[u]=0;
    }
}
void push_up(int u){tree[u]=merge(tree[u*2],tree[u*2+1]);}
void update_a(int u,int l,int r,int ql,int qr,int k)
{
    if(l>=ql&&r<=qr)
    {
        if(k==0)lazy_a[u]=1;
        else lazy_a[u]=2;
        lazy_b[u]=0;
        if(k==0)
        {
            tree[u].cnt0=r-l+1;
            tree[u].cnt1=0;
            tree[u].lsum0=r-l+1;
            tree[u].lsum1=0;
            tree[u].rsum0=r-l+1;
            tree[u].rsum1=0;
            tree[u].maxsum0=r-l+1;
            tree[u].maxsum1=0;  
        }
        else
        {
            tree[u].cnt0=0;
            tree[u].cnt1=r-l+1;
            tree[u].lsum0=0;
            tree[u].lsum1=r-l+1;
            tree[u].rsum0=0;
            tree[u].rsum1=r-l+1;
            tree[u].maxsum0=0;
            tree[u].maxsum1=r-l+1;
        }
        return ;
    }
    push_down(u,l,r);
    int mid=(l+r)/2;
    if(ql<=mid)update_a(u*2,l,mid,ql,qr,k);
    if(qr>mid)update_a(u*2+1,mid+1,r,ql,qr,k);
    push_up(u);
}
void update_b(int u,int l,int r,int ql,int qr)
{
    if(l>=ql&&r<=qr)
    {
        if(lazy_a[u]!=0)
        {
            if(lazy_a[u]==1)lazy_a[u]=2;
            else lazy_a[u]=1;
        }
        else
        {
            if(lazy_b[u]==1)lazy_b[u]=0;
            else lazy_b[u]=1;           
        }
        swap(tree[u].cnt0,tree[u].cnt1);
        swap(tree[u].lsum0,tree[u].lsum1);
        swap(tree[u].rsum0,tree[u].rsum1);
        swap(tree[u].maxsum0,tree[u].maxsum1);
        return ;
    }
    push_down(u,l,r);
    int mid=(l+r)/2;
    if(ql<=mid)update_b(u*2,l,mid,ql,qr);
    if(qr>mid)update_b(u*2+1,mid+1,r,ql,qr);
    push_up(u);
}
stu query(int u,int l,int r,int ql,int qr)
{
    if(l>=ql&&r<=qr)return tree[u];
    push_down(u,l,r);
    int mid=(l+r)/2;
    if(ql>mid)return query(u*2+1,mid+1,r,ql,qr);
    else if(qr<=mid)return query(u*2,l,mid,ql,qr);
    else return merge(query(u*2,l,mid,ql,qr),query(u*2+1,mid+1,r,ql,qr));
}
signed main()
{
    scanf("%lld%lld",&n,&m);
    for(int i=1;i<=n;i++)
    {
        int a;
        scanf("%lld",&a);
        update_a(1,1,n,i,i,a);
    }
    for(int i=1;i<=m;i++)
    {
        int op,x,y;
        scanf("%lld%lld%lld",&op,&x,&y);
        if(op==0||op==1)update_a(1,1,n,x,y,op);
        else if(op==2)update_b(1,1,n,x,y);
        else
        {
            stu answer=query(1,1,n,x,y);
            if(op==3)printf("%lld\n",answer.cnt1);
            else if(op==4)printf("%lld\n",answer.maxsum1);
        }
    }
    return 0;
}

by Foraver @ 2024-11-26 22:19:02

@zzz13579zzz我是傻逼!谢谢大佬


by Foraver @ 2024-11-26 22:22:47

@zzz13579zzz对啊,复制粘贴忘改了,我已经过了


by WuJiaNuo19 @ 2024-11-27 21:26:07

@zzz13579zzz 已关


上一页 |