线段树,RE玄关

P2572 [SCOI2010] 序列操作

chenzhishuo2012 @ 2024-11-15 18:17:00

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define inf 0x3f3f3f3f3f3f3f3f
#define LOCAL
using namespace std;
namespace ly{
    namespace IO{
        #ifndef LOCAL
            constexpr auto maxn=1<<20;
            char in[maxn],out[maxn],*p1=in,*p2=in,*p3=out;
            #define getchar()(p1==p2&&(p2=(p1=in)+fread(in,1,maxn,stdin),p1==p2)?EOF:*p1++)
            #define flush()(fwrite(out,1,p3-out,stdout))
            #define putchar(x)(p3==out+maxn&&(flush(),p3=out),*p3++=(x))
            class Flush{public:~Flush(){flush();}}_;
        #endif
            namespace usr{
                template<typename type>
                inline type read(type&x){
                    x=0;bool flag(0);char ch=getchar();
                    while(!isdigit(ch))flag^=ch=='-',ch=getchar();
                    while(isdigit(ch))x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
                    return flag?x=-x:x;
                }
                template<typename type>
                inline void write(type x){
                    x<0?x=-x,putchar('-'):0;
                    static short Stack[50],top(0);
                    do Stack[++top]=x%10,x/=10;while(x);
                    while(top)putchar(Stack[top--]|48);
                }
                inline char read(char&x){do x=getchar();while(isspace(x));return x;}
                inline char write(const char&x){return putchar(x);}
                inline void read(char*x){static char ch;read(ch);do*(x++)=ch;while(!isspace(ch=getchar())&&~ch);}
                template<typename type>inline void write(type*x){while(*x)putchar(*(x++));}
                inline void read(string&x){static char ch;read(ch),x.clear();do x+=ch;while(!isspace(ch=getchar())&&~ch);}
                inline void write(const string&x){for(int i=0,len=x.length();i<len;++i)putchar(x[i]);}
                template<typename type,typename...T>inline void read(type&x,T&...y){read(x),read(y...);}
                template<typename type,typename...T>
                inline void write(const type&x,const T&...y){write(x),putchar(' '),write(y...),sizeof...(y)^1?0:putchar('\n');}
                template<typename type>
                inline void put(const type&x,bool flag=1){write(x),flag?putchar('\n'):putchar(' ');}
            }
        #ifndef LOCAL
            #undef getchar
            #undef flush
            #undef putchar
        #endif
    }
    using namespace IO::usr;
}
using namespace ly::IO::usr;
#define ls(p) (p<<1)
#define rs(p) (p<<1|1)
#define mid(l,r) ((l+r)>>1)
int n,m,a[100010],opt,l,r;
struct tree{
    int l,r,lazy1,lazy2,lans[10],rans[10],ans[10],sum;
}tr[400010];
void pushup(int p){
    tr[p].sum=tr[ls(p)].sum+tr[rs(p)].sum;
    for(int i=0;i<=1;i++){
        tr[p].lans[i]=tr[ls(p)].lans[i];
        if(i==0&&tr[ls(p)].sum==0)tr[p].lans[i]+=tr[rs(p)].lans[i];
        else if(i==1&&tr[ls(p)].sum==tr[ls(p)].r-tr[ls(p)].l+1)tr[p].lans[i]+=tr[rs(p)].lans[i];
        tr[p].rans[i]=tr[rs(p)].rans[i];
        if(i==0&&tr[rs(p)].sum==0)tr[p].rans[i]+=tr[ls(p)].rans[i];
        else if(i==1&&tr[rs(p)].sum==tr[rs(p)].r-tr[rs(p)].l+1)tr[p].rans[i]+=tr[ls(p)].rans[i];
        tr[p].ans[i]=tr[ls(p)].rans[i]+tr[rs(p)].lans[i];
        tr[p].ans[i]=max(tr[p].ans[i],tr[ls(p)].ans[i]);
        tr[p].ans[i]=max(tr[p].ans[i],tr[rs(p)].ans[i]);
    }
}
void pushdown(int p){
    if(tr[p].lazy1!=-1){
        tr[p].lazy2=0;
        tr[ls(p)].sum=(tr[ls(p)].r-tr[ls(p)].l+1)*tr[p].lazy1;
        tr[rs(p)].sum=(tr[rs(p)].r-tr[rs(p)].l+1)*tr[p].lazy1;
        tr[ls(p)].lazy1=tr[p].lazy1;
        tr[rs(p)].lazy1=tr[p].lazy1;
        tr[ls(p)].lazy2=0;
        tr[rs(p)].lazy2=0;
        tr[ls(p)].lans[tr[p].lazy1]=tr[ls(p)].rans[tr[p].lazy1]=tr[ls(p)].ans[tr[p].lazy1]=(tr[ls(p)].r-tr[ls(p)].l+1);
        tr[ls(p)].lans[1-tr[p].lazy1]=tr[ls(p)].rans[1-tr[p].lazy1]=tr[ls(p)].ans[1-tr[p].lazy1]=0;
        tr[rs(p)].lans[tr[p].lazy1]=tr[rs(p)].rans[tr[p].lazy1]=tr[rs(p)].ans[tr[p].lazy1]=(tr[rs(p)].r-tr[rs(p)].l+1);
        tr[rs(p)].lans[1-tr[p].lazy1]=tr[rs(p)].rans[1-tr[p].lazy1]=tr[rs(p)].ans[1-tr[p].lazy1]=0;
        tr[p].lazy1=-1;
    }
    if(tr[p].lazy2){
        if(tr[ls(p)].lazy1!=-1)tr[ls(p)].lazy1=1-tr[ls(p)].lazy1;
        else tr[ls(p)].lazy2=1-tr[ls(p)].lazy2;
        if(tr[rs(p)].lazy1!=-1)tr[rs(p)].lazy1=1-tr[rs(p)].lazy1;
        else tr[rs(p)].lazy2=1-tr[rs(p)].lazy2;
        tr[ls(p)].sum=(tr[ls(p)].r-tr[ls(p)].l+1)-tr[ls(p)].sum;
        tr[rs(p)].sum=(tr[rs(p)].r-tr[rs(p)].l+1)-tr[rs(p)].sum;
        swap(tr[ls(p)].lans[0],tr[ls(p)].lans[1]);
        swap(tr[ls(p)].rans[0],tr[ls(p)].rans[1]);
        swap(tr[ls(p)].ans[0],tr[ls(p)].ans[1]);
        swap(tr[rs(p)].lans[0],tr[rs(p)].lans[1]);
        swap(tr[rs(p)].rans[0],tr[rs(p)].rans[1]);
        swap(tr[rs(p)].ans[0],tr[rs(p)].ans[1]);
        tr[p].lazy2=0;
    }
}
void build(int l,int r,int p){
    tr[p].l=l;
    tr[p].r=r;
    tr[p].lazy1=-1;
    tr[p].lazy2=0;
    if(l==r){
        tr[p].sum=a[l];
        tr[p].lans[0]=tr[p].rans[0]=tr[p].ans[0]=(a[l]==0?1:0);
        tr[p].lans[1]=tr[p].rans[1]=tr[p].ans[1]=(a[l]==1?1:0);
        return;
    }
    build(l,mid(l,r),ls(p));
    build(mid(l,r)+1,r,rs(p));
    pushup(p);
}
void update(int l,int r,int opt,int p){
    if(tr[p].l==l&&tr[p].r==r){
        if(opt==0||opt==1){
            tr[p].lazy1=opt;
            tr[p].sum=(tr[p].r-tr[p].l+1)*opt;
            tr[p].lans[opt]=(tr[p].r-tr[p].l+1);
            tr[p].rans[opt]=(tr[p].r-tr[p].l+1);
            tr[p].ans[opt]=(tr[p].r-tr[p].l+1);
            tr[p].lans[1-opt]=0;
            tr[p].rans[1-opt]=0;
            tr[p].ans[1-opt]=0;
        }
        else{
            tr[p].lazy2=1-tr[p].lazy2;
            tr[p].sum=(tr[p].r-tr[p].l+1)-tr[p].sum;
            swap(tr[p].lans[0],tr[p].lans[1]);
            swap(tr[p].rans[0],tr[p].rans[1]);
            swap(tr[p].ans[0],tr[p].ans[1]);
        }
        return;
    }
    pushdown(p);
    if(r<=mid(tr[p].l,tr[p].r))update(l,r,opt,ls(p));
    else if(l>mid(tr[p].l,tr[p].r))update(l,r,opt,rs(p));
    else update(l,mid(tr[p].l,tr[p].r),opt,ls(p)),update(mid(tr[p].l,tr[p].r)+1,r,opt,rs(p));
    pushup(p);
}
int query(int l,int r,int p){
    if(tr[p].l==l&&tr[p].r==r){
        return tr[p].sum;
    }
    pushdown(p);
    if(r<=mid(tr[p].l,tr[p].r))return query(l,r,ls(p));
    if(l>mid(tr[p].l,tr[p].r))return query(l,r,rs(p));
    return query(l,mid(tr[p].l,tr[p].r),ls(p))+query(mid(tr[p].l,tr[p].r)+1,r,rs(p));
}
tree query2(int l,int r,int p){
    if(tr[p].l==l&&tr[p].r==r)return tr[p];
    pushdown(p);
    if(r<=mid(tr[p].l,tr[p].r))return query2(l,r,ls(p));
    if(l>mid(tr[p].l,tr[p].r))return query2(l,r,rs(p));
    tree ans1,ansl=query2(l,mid(tr[p].l,tr[p].r),ls(p)),ansr=query2(mid(tr[p].l,tr[p].r)+1,r,rs(p));
    ans1.sum=ansl.sum+ansr.sum;
    for(int i=0;i<=1;i++){
        ans1.lans[i]=ansl.lans[i];
        if(i==0&&ansl.sum==0)ans1.lans[i]+=ansr.lans[i];
        else if(i==1&&ansl.sum==(ansl.r-ansl.l+1))ans1.lans[i]+=ansr.lans[i];
        ans1.rans[i]=ansr.rans[i];
        if(i==0&&ansr.sum==0)ans1.rans[i]+=ansl.rans[i];
        else if(i==1&&ansr.sum==(ansr.r-ansr.l+1))ans1.rans[i]+=ansl.rans[i];
        ans1.ans[i]=ansl.rans[i]+ansr.lans[i];
        ans1.ans[i]=max(ans1.ans[i],ansl.ans[i]);
        ans1.ans[i]=max(ans1.ans[i],ansr.ans[i]);
    }
    return ans1;
}
signed main(){
    read(n,m);
    for(int i=1;i<=n;i++)read(a[i]);
    build(1,n,1);
    while(m--){
        read(opt,l,r);
        if(opt==0||opt==1||opt==2){
            update(l,r,opt,1);
        }
        if(opt==3){
            put(query(l,r,1));
        }
        if(opt==4){
            put(query2(l,r,1).ans[1]);
        }
    }
    return 0;
}

by cnmmmm @ 2024-11-15 18:32:37

不长记性


by Civilight_Eterna @ 2024-11-15 19:20:26

@cnmmmm????jc?


by chenzhishuo2012 @ 2024-11-15 21:33:46

@cnmmmm???被JC了?


|