样例没过求调

P2572 [SCOI2010] 序列操作

吴思诚 @ 2023-10-06 20:44:15

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define ls p<<1
#define rs p<<1|1
#define Ed for(int i=h[x];~i;i=ne[i])
#define Ls(i,l,r) for(int i=l;i<r;++i)
#define Rs(i,l,r) for(int i=l;i>r;--i)
#define Le(i,l,r) for(int i=l;i<=r;++i)
#define Re(i,l,r) for(int i=l;i>=r;--i)
#define L(i,l) for(int i=0;i<l;++i)
#define E(i,l) for(int i=1;i<=l;++i)
#define W(t) while(t--)
#define Wh while

const int N=100010,M=N<<2;
int n,m,len[N];
char tg1[N];
bool a[N],tg2[N];
struct seg{
    int l0,l1,r0,r1,m0,m1,s0,s1;
}tr[M];
seg pushup(seg a,seg b){
    return {a.s1?a.l0:a.s0+b.l0,a.s0?a.l1:a.s1+b.l1,
    b.s1?b.r0:b.s0+a.r0,b.s0?b.r1:b.s1+a.r1,
    max(max(a.m0,b.m0),a.r0+b.l0),
    max(max(a.m1,b.m1),a.r1+b.l1),
    a.s0+b.s0,a.s1+b.s1
    };
}
void change(int p,char ty){
    seg&t=tr[p];
    if(!ty)tg2[p]=tg1[p]=0,
    t={len[p],0,len[p],0,len[p],0,len[p],0};
    else if(ty==1)tg1[p]=1,tg2[p]=0,
    t={0,len[p],0,len[p],0,len[p],0,len[p]};
    else tg2[p]^=1,swap(t.l0,t.l1),swap(t.r0,t.r1),swap(t.s0,t.s1),swap(t.m0,t.m1);
}
void pushdown(int p){
    if(~tg1[p])change(ls,tg1[p]),change(rs,tg1[p]);
    if(tg2[p])change(ls,2),change(rs,2);
    tg1[p]=-1,tg2[p]=0;
}
void build(int p,int l,int r){
    len[p]=r-l+1,tg1[p]=-1;
    if(l==r){
        bool t=a[l];
        tr[p]={t^1,t,t^1,t,t^1,t,t^1,t};
        return;
    }
    int mid=l+r>>1;
    build(ls,l,mid),build(rs,mid+1,r);
    tr[p]=pushup(tr[ls],tr[rs]);
}
void upd(int p,int L,int R,int l,int r,char ty){
    if(l<=L&&R<=r){
        change(p,ty);
        return;
    }
    pushdown(p);
    int mid=L+R>>1;
    if(l<=mid)upd(ls,L,mid,l,r,ty);
    if(r>mid)upd(rs,mid+1,R,l,r,ty);
    tr[p]=pushup(tr[ls],tr[rs]);
}
int q1(int p,int L,int R,int l,int r){
    if(l<=L&&R<=r)return tr[p].s1;
    pushdown(p);
    int mid=L+R>>1,cnt=0;
    if(l<=mid)cnt=q1(ls,L,mid,l,r);
    if(r>mid)cnt+=q1(rs,mid+1,R,l,r);
    return cnt;
}
seg query(int p,int L,int R,int l,int r){
    if(R<l||L>r)return {0,0,0,0,0,0,0,0};
    if(l<=L&&R<=r)return tr[p];
    int mid=L+R>>1;
    return pushup(query(ls,L,mid,l,r),query(rs,mid+1,R,l,r));
}
int main(){
    #ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    E(i, n)cin>>a[i];
    build(1,1,n);
    while(m--){
        int op,l,r;
        cin>>op>>l>>r;
        if(op<3)upd(1,1,n,l,r,op);
        else if(op==3)cout<<q1(1,1,n,l,r)<<'\n';
        else cout<<query(1,1,n,l,r).m1<<'\n';
    }
    return 0;
}

by 吴思诚 @ 2023-10-07 19:11:59

更新,在query中添加pushdown依然0分


by Sword_wielder @ 2023-10-25 12:49:53

这么短的吗?


by Sword_wielder @ 2023-10-25 12:50:30

我打完180行


|