zzy0618 @ 2023-08-16 22:24:31
样例过了,但是交上去爆
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=100005;
int n,q,a[N];
int sum[N<<2],t1[N<<2],t2[N<<2];
int la[N<<2][2],ra[N<<2][2],ma[N<<2][2];
struct node{int sum,la[2],ra[2],ma[2];};
inline int ls(int p){return p<<1;}
inline int rs(int p){return p<<1|1;}
inline void push_up(int p,int l,int r){
sum[p]=sum[ls(p)]+sum[rs(p)];
int mid=(l+r)>>1;
for(int i=0;i<=1;++i){
la[p][i]=la[ls(p)][i];
if(i==1&&sum[ls(p)]==mid-l+1)
la[p][i]+=la[rs(p)][i];
else if(i==0&&sum[ls(p)]==0)
la[p][i]+=la[rs(p)][i];
ra[p][i]=ra[rs(p)][i];
if(i==1&&sum[rs(p)]==r-mid)
ra[p][i]+=ra[ls(p)][i];
else if(i==0&&sum[ls(p)]==0)
ra[p][i]+=ra[ls(p)][i];
ma[p][i]=max(ma[ls(p)][i],ma[rs(p)][i]);
ma[p][i]=max(ma[p][i],ra[ls(p)][i]+la[rs(p)][i]);
ma[p][i]=max(ma[p][i],la[ls(p)][i]);
ma[p][i]=max(ma[p][i],ra[rs(p)][i]);
}
}
inline void push_down(int p,int l,int r){
int mid=(l+r)>>1;
if(t1[p]!=-1){
t2[p]=0;
if(t1[p]==1)sum[ls(p)]=mid-l+1,sum[rs(p)]=r-mid;
else sum[ls(p)]=sum[rs(p)]=0;
t1[ls(p)]=t1[rs(p)]=t1[p];
t2[ls(p)]=t2[rs(p)]=0;
la[ls(p)][t1[p]]=ra[ls(p)][t1[p]]=ma[ls(p)][t1[p]]=mid-l+1;
la[ls(p)][t1[p]^1]=ra[ls(p)][t1[p]^1]=ma[ls(p)][t1[p]^1]=0;
la[rs(p)][t1[p]]=ra[rs(p)][t1[p]]=ma[rs(p)][t1[p]]=r-mid;
la[rs(p)][t1[p]^1]=ra[rs(p)][t1[p]^1]=ma[rs(p)][t1[p]^1]=0;
t1[p]=-1;
}
if(t2[p]){
sum[ls(p)]=mid-l+1-sum[ls(p)];
sum[rs(p)]=r-mid-sum[rs(p)];
if(t1[ls(p)]!=-1)t1[ls(p)]^=1;
else t2[ls(p)]^=1;
if(t1[rs(p)]!=-1)t1[rs(p)]^=1;
else t2[rs(p)]^=1;
swap(la[ls(p)][0],la[ls(p)][1]);
swap(ra[ls(p)][0],ra[ls(p)][1]);
swap(ma[ls(p)][0],ma[ls(p)][1]);
swap(la[rs(p)][0],la[rs(p)][1]);
swap(ra[rs(p)][0],ra[rs(p)][1]);
swap(ma[rs(p)][0],ma[rs(p)][1]);
t2[p]=0;
}
}
inline void build(int p,int l,int r){
t1[p]=-1;t2[p]=0;
if(l==r){
sum[p]=a[l];
la[p][a[l]]=ra[p][a[l]]=ma[p][a[l]]=1;
return;
}
int mid=(l+r)>>1;
build(ls(p),l,mid);
build(rs(p),mid+1,r);
push_up(p,l,r);
}
inline void update(int p,int l,int r,int L,int R,int k){
push_down(p,l,r);
if(L<=l&&r<=R){
if(k<2){
t1[p]=k;
if(k==1)sum[p]=r-l+1;
else sum[p]=0;
la[p][k]=ra[p][k]=ma[p][k]=r-l+1;
la[p][k^1]=ra[p][k^1]=ma[p][k^1]=0;
}
else if(k==2){
sum[p]=r-l+1-sum[p];
t2[p]^=1;
swap(la[p][0],la[p][1]);
swap(ra[p][0],ra[p][1]);
swap(ma[p][0],ma[p][1]);
}
return;
}
int mid=(l+r)>>1;
if(L<=mid)update(ls(p),l,mid,L,R,k);
if(mid<R)update(rs(p),mid+1,r,L,R,k);
push_up(p,l,r);
}
inline int query1(int p,int l,int r,int L,int R){
if(L<=l&&r<=R)
return sum[p];
push_down(p,l,r);
int mid=(l+r)>>1,res=0;
if(L<=mid)res+=query1(ls(p),l,mid,L,R);
if(mid<R)res+=query1(rs(p),mid+1,r,L,R);
return res;
}
inline node query2(int p,int l,int r,int L,int R){
if(L<=l&&r<=R){
node ans;
ans.sum=sum[p];
for(int i=0;i<=1;++i){
ans.la[i]=la[p][i];
ans.ra[i]=ra[p][i];
ans.ma[i]=ma[p][i];
}
return ans;
}
push_down(p,l,r);
int mid=(l+r)>>1;
if(R<=mid)return query2(ls(p),l,mid,L,R);
if(mid<L)return query2(rs(p),mid+1,r,L,R);
node x=query2(ls(p),l,mid,L,R);
node y=query2(rs(p),mid+1,r,L,R);
node ans;ans.sum=x.sum+y.sum;
for(int i=0;i<=1;++i){
ans.la[i]=x.la[i];
if(i==1&&x.sum==mid-l+1)
ans.la[i]+=y.la[i];
else if(i==0&&x.sum==0)
ans.la[i]+=y.la[i];
ans.ra[i]=y.ra[i];
if(i==1&&y.sum==r-mid)
ans.ra[i]+=x.ra[i];
else if(i==0&&y.sum==0)
ans.ra[i]+=x.ra[i];
ans.ma[i]=max(x.ma[i],y.ma[i]);
ans.ma[i]=max(ans.ma[i],x.ra[i]+y.la[i]);
ans.ma[i]=max(ans.ma[i],x.la[i]);
ans.ma[i]=max(ans.ma[i],y.ra[i]);
}
return ans;
}
int op,l,r;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++)
cin>>a[i];
build(1,1,n);
while(q--){
cin>>op>>l>>r;
l++;r++;
if(op==0){
update(1,1,n,l,r,0);
}
if(op==1){
update(1,1,n,l,r,1);
}
if(op==2){
update(1,1,n,l,r,2);
}
if(op==3){
cout<<query1(1,1,n,l,r)<<'\n';
}
if(op==4){
node ans=query2(1,1,n,l,r);
cout<<ans.ma[1]<<'\n';
}
}
return 0;
}
by Iniaugoty @ 2023-08-16 22:42:15
好长
可能是细节挂了
建议重写一遍
by Lysea @ 2023-08-16 23:01:09
@gty314159
建议重写一遍
好建议
by Iniaugoty @ 2023-08-16 23:04:59
@Tryst 我线段树调不出来就直接重构的