rt
#include<bits/stdc++.h>
#define lch num<<1
#define rch num<<1|1
#define int long long
using namespace std;
const int N=1e5+5;
struct T{
int l,r,tag,sum,l0,r0,l1,r1,cnt0,cnt1,len;//tag=1取反,tag=0不变,tag=-1变1,tag=-2变0
}tree[N<<4];
int n,m,a[N];
void push_up(int num){
tree[num].sum=tree[lch].sum+tree[rch].sum;
tree[num].cnt1=max(max(tree[lch].cnt1,tree[rch].cnt1),tree[lch].r1+tree[rch].l1);
tree[num].cnt0=max(max(tree[lch].cnt0,tree[rch].cnt0),tree[lch].r0+tree[rch].l0);
tree[num].l1=tree[lch].l1,tree[num].l0=tree[lch].l0,tree[num].r1=tree[rch].r1,tree[num].r0=tree[rch].r0;
if(tree[lch].l1==tree[lch].len){
tree[num].l1=tree[lch].l1+tree[rch].l1;
}else if(tree[lch].l0==tree[lch].len){
tree[num].l0=tree[lch].l0+tree[rch].l0;
}
if(tree[rch].r1==tree[rch].len){
tree[num].r1=tree[rch].r1+tree[lch].r1;
}else if(tree[rch].r0==tree[rch].len){
tree[num].r0=tree[rch].r0+tree[lch].r0;
}
}
void push_down(int num){
int l=tree[num].l,r=tree[num].r,mid=(l+r)>>1;
if(tree[num].tag==0){
return;
}
if(tree[num].tag==1){
tree[lch].sum=(mid-l+1)-tree[lch].sum;
tree[rch].sum=(r-mid)-tree[rch].sum;
swap(tree[lch].l1,tree[lch].l0),swap(tree[lch].r1,tree[lch].r0),swap(tree[lch].cnt0,tree[lch].cnt1);
swap(tree[rch].l1,tree[rch].l0),swap(tree[rch].r1,tree[rch].r0),swap(tree[rch].cnt0,tree[rch].cnt1);
if(tree[lch].tag==-1){
tree[lch].tag=-2;
}else if(tree[lch].tag==-2){
tree[lch].tag=-1;
}else{
tree[lch].tag^=1;
}
if(tree[rch].tag==-1){
tree[rch].tag=-2;
}else if(tree[rch].tag==-2){
tree[rch].tag=-1;
}else{
tree[rch].tag^=1;
}
}else if(tree[num].tag==-1){
tree[lch].sum=tree[lch].len;
tree[rch].sum=tree[rch].len;
tree[lch].l1=tree[lch].r1=tree[lch].cnt1=tree[lch].len;
tree[lch].l0=tree[lch].r0=tree[lch].cnt0=0;
tree[rch].l1=tree[rch].r1=tree[rch].cnt1=tree[rch].len;
tree[rch].l0=tree[rch].r0=tree[rch].cnt0=0;
tree[lch].tag=tree[rch].tag=-1;
}else{
tree[lch].sum=tree[rch].sum=0;
tree[lch].l0=tree[lch].r0=tree[lch].cnt0=tree[lch].len;
tree[lch].l1=tree[lch].r1=tree[lch].cnt1=0;
tree[rch].l0=tree[rch].r0=tree[rch].cnt0=tree[rch].len;
tree[rch].l1=tree[rch].r1=tree[rch].cnt1=0;
tree[lch].tag=tree[rch].tag=-2;
}
tree[num].tag=0;
}
void build(int num,int l,int r){
tree[num].l=l,tree[num].r=r;
tree[num].len=(r-l+1);
if(l==r){
tree[num].sum=tree[num].l1=tree[num].r1=tree[num].cnt1=a[l];
tree[num].l0=tree[num].r0=tree[num].cnt0=!a[l];
return ;
}
int mid=(l+r)>>1;
build(lch,l,mid);
build(rch,mid+1,r);
push_up(num);
}
void modify(int num,int L,int R,int k){
int l=tree[num].l,r=tree[num].r,len=tree[num].len;
if(r<L||l>R){
return ;
}
if(L<=l&&r<=R){
if(k==-1){
tree[num].sum=tree[num].len;
tree[num].l0=tree[num].r0=tree[num].cnt0=0;
tree[num].l1=tree[num].r1=tree[num].cnt1=len;
tree[num].tag=-1;
}else if(k==-2){
tree[num].sum=0;
tree[num].l0=tree[num].r0=tree[num].cnt0=len;
tree[num].l1=tree[num].r1=tree[num].cnt1=0;
tree[num].tag=-2;
}else{
tree[num].sum=len-tree[num].sum;
swap(tree[num].l1,tree[num].l0);
swap(tree[num].r1,tree[num].r0);
swap(tree[num].cnt0,tree[num].cnt1);
if(tree[num].tag==-1){
tree[num].tag=-2;
}else if(tree[num].tag==-2){
tree[num].tag=-1;
}else{
tree[num].tag^=1;
}
}
return ;
}
push_down(num);
modify(lch,L,R,k);
modify(rch,L,R,k);
push_up(num);
}
int query(int num,int L,int R,int k){//k为1查1数量,k为0查最大连续1个数
int l=tree[num].l,r=tree[num].r,mid=(l+r)>>1;
if(L<=l&&r<=R){
if(k==1){
return tree[num].sum;
}
return tree[num].cnt1;
}
push_down(num);
if(L<=mid){
return query(lch,L,R,k);
}if(mid<R){
return query(rch,L,R,k);
}else{
if(k==1){
return query(lch,L,R,k)+query(rch,L,R,k);
}else{
return max(max(query(lch,L,mid,k),query(rch,mid+1,R,k)),min(tree[rch].l1,R-mid)+min(tree[lch].r1,mid-L+1));
}
}
}
signed main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
build(1,1,n);
while(m--){
int h,l,r;
cin>>h>>l>>r;
l++,r++;
if(h==0){
modify(1,l,r,-2);
}else if(h==1){
modify(1,l,r,-1);
}else if(h==2){
modify(1,l,r,1);
}else if(h==3){
cout<<query(1,l,r,1)<<endl;
}else if(h==4){
cout<<query(1,l,r,0)<<endl;
}
}
}
谢谢daolao们