线段树水题,练习区间翻转,插入,query
总之tag打好,剩下的就是noip基本功了。
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 400005
using namespace std;
int n,m,sum[N],c[N][2],val[N],tun[N]; struct node{ int x,y,z; }f[N][2];
int read(){
int x=0; char ch=getchar();
while (ch<'0' || ch>'9') ch=getchar();
while (ch>='0' && ch<='9'){ x=x*10+ch-'0'; ch=getchar(); }
return x;
}
void maintain(int k){
int l=c[k][0],r=c[k][1],mid=(l+r)>>1,i;
sum[k]=sum[k<<1]+sum[k<<1|1];
for (i=0; i<2; i++){
if (sum[k<<1]==(mid-l+1)*i) f[k][i].x=mid-l+1+f[k<<1|1][i].x; else f[k][i].x=f[k<<1][i].x;
if (sum[k<<1|1]==(r-mid)*i) f[k][i].y=r-mid+f[k<<1][i].y; else f[k][i].y=f[k<<1|1][i].y;
f[k][i].z=max(max(f[k<<1][i].z,f[k<<1|1][i].z),f[k<<1][i].y+f[k<<1|1][i].x);
}
}
void paint(int k,int u){
int l=c[k][0],r=c[k][1],v=!u;
f[k][u].x=f[k][u].y=f[k][u].z=r-l+1;
f[k][v].x=f[k][v].y=f[k][v].z=0;
sum[k]=u*(r-l+1); val[k]=u; tun[k]=0;
}
void rever(int k){
if (val[k]!=-1) paint(k,!val[k]); else{
swap(f[k][0],f[k][1]); tun[k]^=1;
sum[k]=c[k][1]-c[k][0]+1-sum[k];
}
}
void pushdown(int k){
if (val[k]!=-1){
paint(k<<1,val[k]); paint(k<<1|1,val[k]); val[k]=-1;
}
if (tun[k]){
rever(k<<1); rever(k<<1|1); tun[k]=0;
}
}
void build(int k,int x,int y){
c[k][0]=x; c[k][1]=y; val[k]=-1;
if (x==y){
sum[k]=read();
if (sum[k]){ f[k][1].x=f[k][1].y=f[k][1].z=1; }
else{ f[k][0].x=f[k][0].y=f[k][0].z=1; }
return;
}
int mid=(x+y)>>1;
build(k<<1,x,mid); build(k<<1|1,mid+1,y); maintain(k);
}
void ins(int k,int x,int y,int t){
int l=c[k][0],r=c[k][1],mid=(l+r)>>1;
if (x==l && y==r){
if (t<2) paint(k,t); else rever(k); return;
}
pushdown(k);
if (y<=mid) ins(k<<1,x,y,t); else
if (x>mid) ins(k<<1|1,x,y,t); else{
ins(k<<1,x,mid,t); ins(k<<1|1,mid+1,y,t);
}
maintain(k);
}
int getsum(int k,int x,int y){
int l=c[k][0],r=c[k][1],mid=(l+r)>>1;
if (x==l && y==r) return sum[k];
pushdown(k);
if (y<=mid) return getsum(k<<1,x,y); else
if (x>mid) return getsum(k<<1|1,x,y); else
return getsum(k<<1,x,mid)+getsum(k<<1|1,mid+1,y);
}
node getlen(int k,int x,int y){
int l=c[k][0],r=c[k][1],mid=(l+r)>>1;
if (x==l && y==r) return f[k][1];
pushdown(k);
if (y<=mid) return getlen(k<<1,x,y); else
if (x>mid) return getlen(k<<1|1,x,y); else{
node t1=getlen(k<<1,x,mid),t2=getlen(k<<1|1,mid+1,y),t3;
if (t1.x==mid-l+1) t3.x=t1.x+t2.x; else t3.x=t1.x;
if (t2.y==r-mid) t3.y=t1.y+t2.y; else t3.y=t2.y;
t3.z=max(max(t1.z,t2.z),t1.y+t2.x);
return t3;
}
}
int main(){
n=read(); m=read(); build(1,1,n);
while (m--){
int t=read(),x=read()+1,y=read()+1;
if (t<3) ins(1,x,y,t); else
if (t<4) printf("%d\n",getsum(1,x,y)); else printf("%d\n",getlen(1,x,y).z);
}
return 0;
}