感觉没有错啊*_*

P2572 [SCOI2010] 序列操作

machuangkun @ 2019-11-08 09:10:48


#include<cstdio>
#define int long long
#define maxn 200005
#define re register
using namespace std;

inline int read(){
    int s=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        s=(s<<1)+(s<<3)+ch-'0';
        ch=getchar();
    }
    return s*f;
}

int max(int x,int y){
    return x>y?x:y;
}

struct T{
    int tot1,tot0;
    int sum1,sum0;
    int lmax1,lmax0;
    int rmax1,rmax0;
    int lazy,len;
} tree[maxn<<2];

int a[maxn];

void update(int u){
    tree[u].sum1=max(max(tree[u<<1].sum1,tree[u<<1|1].sum1),tree[u<<1].rmax1+tree[u<<1|1].lmax1);
    tree[u].sum0=max(max(tree[u<<1].sum0,tree[u<<1|1].sum0),tree[u<<1].rmax0+tree[u<<1|1].lmax0);

    if(tree[u<<1].sum1==tree[u<<1].len) tree[u].lmax1=tree[u<<1].len+tree[u<<1|1].lmax1;
    else tree[u].lmax1=tree[u<<1].lmax1;
    if(tree[u<<1].sum0==tree[u<<1].len) tree[u].lmax0=tree[u<<1].len+tree[u<<1|1].lmax0;
    else tree[u].lmax0=tree[u<<1].lmax0;

    if(tree[u<<1|1].sum1==tree[u<<1|1].len) tree[u].rmax1=tree[u<<1|1].len+tree[u<<1].rmax1;
    else tree[u].rmax1=tree[u<<1|1].rmax1;
    if(tree[u<<1|1].sum0==tree[u<<1|1].len) tree[u].rmax0=tree[u<<1|1].len+tree[u<<1].rmax0;
    else tree[u].rmax0=tree[u<<1|1].rmax0;

    tree[u].len=tree[u<<1].len+tree[u<<1|1].len;

    tree[u].tot0=tree[u<<1].tot0+tree[u<<1|1].tot0;
    tree[u].tot1=tree[u<<1].tot1+tree[u<<1|1].tot1;
}

void build(int u,int l,int r){
    if(l==r){
        tree[u].len=1;
        if(a[l]==0){
            tree[u].sum1=tree[u].lmax1=tree[u].rmax1=tree[u].tot1=0;
            tree[u].sum0=tree[u].lmax0=tree[u].rmax0=tree[u].tot0=1;
        }
        if(a[l]==1){
            tree[u].sum1=tree[u].lmax1=tree[u].rmax1=tree[u].tot1=1;
            tree[u].sum0=tree[u].lmax0=tree[u].rmax0=tree[u].tot0=0;
        }
        return;
    }
    int mid=(l+r)>>1;
    build(u<<1,l,mid);
    build(u<<1|1,mid+1,r);
    update(u);
}

int temp;

void swap(int &x,int &y){
    temp=x,x=y,y=temp;
}

void pushdown(int u){
    if(tree[u].lazy==0) return;
    if(tree[u].lazy==1){
        tree[u<<1].lazy=tree[u<<1|1].lazy=1;
        tree[u<<1].sum1=tree[u<<1].lmax1=tree[u<<1].rmax1=tree[u<<1].tot1=0;
        tree[u<<1].sum0=tree[u<<1].lmax0=tree[u<<1].rmax0=tree[u<<1].tot0=tree[u<<1].len;
        tree[u<<1|1].sum1=tree[u<<1|1].lmax1=tree[u<<1|1].rmax1=tree[u<<1|1].tot1=0;
        tree[u<<1|1].sum0=tree[u<<1|1].lmax0=tree[u<<1|1].rmax0=tree[u<<1|1].tot0=tree[u<<1|1].len;
    }
    if(tree[u].lazy==2){
        tree[u<<1].lazy=tree[u<<1|1].lazy=2;
        tree[u<<1].sum1=tree[u<<1].lmax1=tree[u<<1].rmax1=tree[u<<1].tot1=tree[u<<1].len;
        tree[u<<1].sum0=tree[u<<1].lmax0=tree[u<<1].rmax0=tree[u<<1].tot0=0;
        tree[u<<1|1].sum1=tree[u<<1|1].lmax1=tree[u<<1|1].rmax1=tree[u<<1|1].tot1=tree[u<<1|1].len;
        tree[u<<1|1].sum0=tree[u<<1|1].lmax0=tree[u<<1|1].rmax0=tree[u<<1|1].tot0=0;
    }
    if(tree[u].lazy==3){
        tree[u<<1].lazy=tree[u<<1|1].lazy=3;
        swap(tree[u<<1].sum0,tree[u<<1].sum1);
        swap(tree[u<<1].lmax0,tree[u<<1].lmax1);
        swap(tree[u<<1].rmax0,tree[u<<1].rmax1);
        swap(tree[u<<1].tot0,tree[u<<1].tot1);
        swap(tree[u<<1|1].sum0,tree[u<<1|1].sum1);
        swap(tree[u<<1|1].lmax0,tree[u<<1|1].lmax1);
        swap(tree[u<<1|1].rmax0,tree[u<<1|1].rmax1);
        swap(tree[u<<1|1].tot0,tree[u<<1|1].tot1);
    }
    tree[u].lazy=0;
}

void change(int u,int l,int r,int tag,int L,int R){
    pushdown(u);
    if(L<=l&&r<=R){
        if(tag==1){
            tree[u].sum1=tree[u].lmax1=tree[u].rmax1=tree[u].tot1=0;
            tree[u].sum0=tree[u].lmax0=tree[u].rmax0=tree[u].tot0=tree[u].len;
        }
        if(tag==2){
            tree[u].sum1=tree[u].lmax1=tree[u].rmax1=tree[u].tot1=tree[u].len;
            tree[u].sum0=tree[u].lmax0=tree[u].rmax0=tree[u].tot0=0;
        }
        if(tag==3){
            swap(tree[u].sum0,tree[u].sum1);
            swap(tree[u].lmax0,tree[u].lmax1);
            swap(tree[u].rmax0,tree[u].rmax1);
            swap(tree[u].tot0,tree[u].tot1);
        }
        tree[u].lazy=tag;
        return;
    }
    int mid=(l+r)>>1;
    if(L<=mid) change(u<<1,l,mid,tag,L,R);
    if(R>mid) change(u<<1|1,mid+1,r,tag,L,R);
    update(u);
}

int query1(int u,int l,int r,int L,int R){
    pushdown(u);
    if(L<=l&&r<=R){
        return tree[u].tot1;
    }
    int mid=(l+r)>>1;
    int ans=0;
    if(L<=mid) ans+=query1(u<<1,l,mid,L,R);
    if(R>mid) ans+=query1(u<<1|1,mid+1,r,L,R);
    return ans;
}

int query2(int u,int l,int r,int L,int R){
    pushdown(u);
    if(L<=l&&r<=R){
        return tree[u].sum1;
    }
    int mid=(l+r)>>1;
    int ans=0;
    if(L<=mid) ans=max(ans,query2(u<<1,l,mid,L,R));
    if(R>mid) ans=max(ans,query2(u<<1|1,mid+1,r,L,R));
    ans=max(ans,tree[u<<1].rmax1+tree[u<<1|1].lmax1);
    return ans;
}

int n,m,op,x,y;

signed main()
{
    n=read();
    m=read();
    for(re int i=1;i<=n;++i) a[i]=read();
    build(1,1,n);
    for(;m;--m){
        op=read();
        x=read();
        y=read();
        ++x,++y;
        if(op==0) change(1,1,n,1,x,y);
        if(op==1) change(1,1,n,2,x,y);
        if(op==2) change(1,1,n,3,x,y);
        if(op==3) printf("%lld\n",query1(1,1,n,x,y));
        if(op==4) printf("%lld\n",query2(1,1,n,x,y));
    }
    return 0;
}

by resftlmuttmotw @ 2019-11-08 12:34:27

加油(坏笑)


by machuangkun @ 2019-11-08 14:41:41

@resftlmuttmotw +_+


|