for_cat @ 2024-02-20 16:36:00
调了一下午了,只能过hack和讨论区的数据
//最大连续0个数,最大连续1的个数,区间0个数,区间1个数,tag是否取反,tag全为1,tag全为0,前缀0最多,前缀1最多,后缀0最多,后缀1最多
#include <iostream>
using namespace std;
const int N=1e5+5;
struct node{
int maxn0,maxn1,sum0,sum1,maxnqz0,maxnqz1,maxnhz0,maxnhz1,cd;
bool tagf,tag1,tag0;
}data[4*N];
int a[N];
node merge(node x,node y){
//cout<<1;
node t;
if(y.maxnhz1==y.cd){
t.maxnhz1=y.maxnhz1+x.maxnhz1;
}
else{
t.maxnhz1=y.maxnhz1;
}
//最大后缀1
if(y.maxnhz0==y.cd){
t.maxnhz0=y.maxnhz0+x.maxnhz0;
}
else{
t.maxnhz0=y.maxnhz0;
}
//最大后缀0
if(x.maxnqz0==x.cd){
t.maxnqz0=x.maxnqz0+y.maxnqz0;
}
else{
t.maxnqz0=x.maxnqz0;
}
//最大前缀0
if(x.maxnqz1==x.cd){
t.maxnqz1=x.maxnqz1+y.maxnqz1;
}
else{
t.maxnqz1=x.maxnqz1;
}
//最大前缀1
t.maxn1=max(max(x.maxn1,y.maxn1),x.maxnhz1+y.maxnqz1);
t.maxn0=max(max(x.maxn0,y.maxn0),x.maxnhz0+y.maxnqz0);
t.sum0=x.sum0+y.sum0;
t.sum1=x.sum1+y.sum1;
t.cd=x.cd+y.cd;
return t;
}
void build(int x,int l,int r){
if(l==r){
if(a[l]==0){
data[x].maxn0=1;
data[x].sum0=1;
data[x].maxnqz0=1;
data[x].maxnhz0=1;
data[x].cd=1;
}
else{
data[x].maxn1=1;
data[x].sum1=1;
data[x].maxnqz1=1;
data[x].maxnhz1=1;
data[x].cd=1;
}
return ;
}
int mid=(l+r)/2;
build(x*2,l,mid);
build(x*2+1,mid+1,r);
data[x]=merge(data[x*2],data[x*2+1]);
//cout<<x<<' '<<l<<" "<<r<<" "<<"maxn0 "<<data[x].maxn0<<" "<<"maxn1 "<<data[x].maxn1<<" maxnhz0 "<<data[x].maxnhz0<<" maxnhz1 "<<data[x].maxnhz1<<" maxnqz0 "<<data[x].maxnqz0<<" maxnqz1 "<<data[x].maxnqz1<<" sum0 "<<data[x].sum0<<" sum1 "<<data[x].sum1<<"*\n";
}
void pushdown(int x,int l,int r){
//cout<<1;
if(l!=r){
//cout<<x*2<<' '<<x*2+1<<"\n";
if(data[x].tag0==1){
data[x*2].tag0=1;
data[x*2].tag1=0;
data[x*2].tagf=0;
data[x*2].maxn0=data[x*2].cd;
data[x*2].maxn1=0;
data[x*2].maxnhz0=data[x*2].cd;
data[x*2].maxnqz0=data[x*2].cd;
data[x*2].maxnhz1=0;
data[x*2].maxnqz1=0;
data[x*2].sum1=0;
data[x*2].sum0=data[x*2].cd;
//
data[x*2+1].tag0=1;
data[x*2+1].tag1=0;
data[x*2+1].tagf=0;
data[x*2+1].maxn0=data[x*2+1].cd;
data[x*2+1].maxn1=0;
data[x*2+1].maxnhz0=data[x*2+1].cd;
data[x*2+1].maxnqz0=data[x*2+1].cd;
data[x*2+1].maxnhz1=0;
data[x*2+1].maxnqz1=0;
data[x*2+1].sum1=0;
data[x*2+1].sum0=data[x*2+1].cd;
}
if(data[x].tag1==1){
data[x*2].tag0=0;
data[x*2].tag1=1;
data[x*2].tagf=0;
data[x*2].maxn1=data[x*2].cd;
data[x*2].maxn0=0;
data[x*2].maxnhz1=data[x*2].cd;
data[x*2].maxnqz1=data[x*2].cd;
data[x*2].maxnhz0=0;
data[x*2].maxnqz0=0;
data[x*2].sum0=0;
data[x*2].sum1=data[x*2].cd;
//
data[x*2+1].tag0=0;
data[x*2+1].tag1=1;
data[x*2+1].tagf=0;
data[x*2+1].maxn1=data[x*2+1].cd;
data[x*2+1].maxn0=0;
data[x*2+1].maxnhz1=data[x*2+1].cd;
data[x*2+1].maxnqz1=data[x*2+1].cd;
data[x*2+1].maxnhz0=0;
data[x*2+1].maxnqz0=0;
data[x*2+1].sum0=0;
data[x*2+1].sum1=data[x*2+1].cd;
}
//maxn0,maxn1,sum0,sum1,maxnqz0,maxnqz1,maxnhz0,maxnhz1,cd;
if(data[x].tagf==1){
data[x*2].tagf=1;
data[x*2+1].tagf=1;
//maxn0,maxn1
int t=data[x*2].maxn0;
data[x*2].maxn0=data[x*2].maxn1;
data[x*2].maxn1=t;
t=data[x*2+1].maxn0;
data[x*2+1].maxn0=data[x*2+1].maxn1;
data[x*2+1].maxn1=t;
//sum0,sum1
t=data[x*2].sum0;
data[x*2].sum0=data[x*2].sum1;
data[x*2].sum1=t;
t=data[x*2+1].sum0;
data[x*2+1].sum0=data[x*2+1].sum1;
data[x*2+1].sum1=t;
//maxnqz0,maxnqz1
t=data[x*2].maxnqz0;
data[x*2].maxnqz0=data[x*2].maxnqz1;
data[x*2].maxnqz1=t;
t=data[x*2+1].maxnqz0;
data[x*2+1].maxnqz0=data[x*2+1].maxnqz1;
data[x*2+1].maxnqz1=t;
//maxnhz0,maxnhz1
t=data[x*2].maxnhz0;
data[x*2].maxnhz0=data[x*2].maxnhz1;
data[x*2].maxnhz1=t;
t=data[x*2+1].maxnhz0;
data[x*2+1].maxnhz0=data[x*2+1].maxnhz1;
data[x*2+1].maxnhz1=t;
}
data[x].tag0=0;
data[x].tag1=0;
data[x].tagf=0;
}
}
void b0(int x){
//cout<<1;
data[x].tag0=1;
data[x].tag1=0;
data[x].tagf=0;
data[x].maxn0=data[x].cd;
data[x].maxn1=0;
data[x].maxnhz0=data[x].cd;
data[x].maxnqz0=data[x].cd;
data[x].maxnhz1=0;
data[x].maxnqz1=0;
data[x].sum1=0;
data[x].sum0=data[x].cd;
}
void b1(int x){
data[x].tag0=0;
data[x].tag1=1;
data[x].tagf=0;
data[x].maxn1=data[x].cd;
data[x].maxn0=0;
data[x].maxnhz1=data[x].cd;
data[x].maxnqz1=data[x].cd;
data[x].maxnhz0=0;
data[x].maxnqz0=0;
data[x].sum0=0;
data[x].sum1=data[x].cd;
}
void bf(int x){
data[x].tagf^=1;
//maxn0,maxn1
int t=data[x].maxn0;
data[x].maxn0=data[x].maxn1;
data[x].maxn1=t;
//sum0,sum1
t=data[x].sum0;
data[x].sum0=data[x].sum1;
data[x].sum1=t;
//maxnqz0,maxnqz1
t=data[x].maxnqz0;
data[x].maxnqz0=data[x].maxnqz1;
data[x].maxnqz1=t;
//maxnhz0,maxnhz1
t=data[x].maxnhz0;
data[x].maxnhz0=data[x].maxnhz1;
data[x].maxnhz1=t;
}
void modify(int x,int l,int r,int a,int b,int op){
//cout<<l<<" "<<r<<" "<<a<<" "<<b<<"\n";
pushdown(x,l,r);
//cout<<1;
if(l==a&&r==b){
//cout<<1;
if(op==0){
b0(x);
}
else{
if(op==1){
b1(x);
}
else{
if(data[x].tag0==1||data[x].tag1==1){
int t=data[x].tag0;
data[x].tag0=data[x].tag1;
data[x].tag1=t;
if(data[x].tag1==1){
b1(x);
}
else{
b0(x);
}
}
else{
bf(x);
}
}
}
return ;
}
//cout<<1;
int mid=(l+r)/2;
//cout<<1;
if(b<=mid){
modify(x*2,l,mid,a,b,op);
//cout<<1;
}
else{
if(a>=mid+1){
modify(x*2+1,mid+1,r,a,b,op);
//cout<<1;
}
else{
modify(x*2,l,mid,a,mid,op);
//cout<<1;
modify(x*2+1,mid+1,r,mid+1,b,op);
//cout<<1;
}
}
data[x]=merge(data[x*2],data[x*2+1]);
//cout<<x<<' '<<l<<" "<<r<<" "<<"maxn0 "<<data[x].maxn0<<" "<<"maxn1 "<<data[x].maxn1<<" maxnhz0 "<<data[x].maxnhz0<<" maxnhz1 "<<data[x].maxnhz1<<" maxnqz0 "<<data[x].maxnqz0<<" maxnqz1 "<<data[x].maxnqz1<<" sum0 "<<data[x].sum0<<" sum1 "<<data[x].sum1<<"*\n";
//cout<<1;
}
node find(int x,int l,int r,int a,int b){
pushdown(x,l,r);
if(l==a&&r==b){
return data[x];
}
int mid=(l+r)/2;
if(a>=mid+1){
return find(x*2+1,mid+1,r,a,b);
}
else{
if(b<=mid){
return find(x*2,l,mid,a,b);
}
else{
return merge(find(x*2,l,mid,a,mid),find(x*2+1,mid+1,r,mid+1,b));
}
}
}
int main(){
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
build(1,1,n);
for(int i=1;i<=m;i++){
int op;
cin>>op;
int l,r;
cin>>l>>r;
if(op>=0&&op<=2){
modify(1,1,n,l+1,r+1,op);
}
else{
node t=find(1,1,n,l+1,r+1);
if(op==3){
cout<<t.sum1<<"\n";
}
else{
cout<<t.maxn1<<"\n";
}
}
}
return 0;
}
/*
10 10
0 0 0 1 1 0 1 0 1 1
1 0 2
3 0 5
2 2 2
4 0 4
0 3 6
2 3 7
4 2 8
1 0 5
0 5 6
3 3 9
*/
by Trubiacy_ @ 2024-02-20 19:58:55
%%%
by he_qwq @ 2024-02-20 22:58:44
我压行后200多行交上去0分:)