0ptsWA求条
查看原帖
0ptsWA求条
785639
xuchuhan楼主2024/6/25 11:38

RT,#1~#10WA,#11AC,样例可过。

代码有点冗长AwA。

#include<bits/stdc++.h>
#define int long long
#define L cur*2
#define R cur*2+1
using namespace std;
const int N=1e5+5;
int n,q;
bool a[N];
struct node{
	int sum,siz;
	int lans[2],rans[2],dat[2];
	bool retag,ontag,zetag;
}tree[N*4],pre;
void pushup(int cur){
	tree[cur].siz=tree[L].siz+tree[R].siz;
	tree[cur].sum=tree[L].sum+tree[R].sum;
	tree[cur].lans[1]=tree[L].lans[1];
	if(tree[L].rans[1]==tree[L].siz)
		tree[cur].lans[1]=tree[L].siz+tree[R].lans[1]; 
	tree[cur].lans[0]=tree[L].lans[0];
	if(tree[L].rans[1]==0)
		tree[cur].lans[0]=tree[L].siz+tree[R].lans[0]; 
	tree[cur].rans[1]=tree[R].rans[1];
	if(tree[R].lans[1]==tree[R].siz)
		tree[cur].rans[1]=tree[R].siz+tree[L].rans[1];
	tree[cur].rans[0]=tree[R].rans[0];
	if(tree[R].lans[1]==0)
		tree[cur].rans[0]=tree[R].siz+tree[L].rans[0];
	tree[cur].dat[1]=max(max(tree[L].dat[1],tree[R].dat[1]),tree[L].rans[1]+tree[R].lans[1]);
	tree[cur].dat[0]=max(max(tree[L].dat[0],tree[R].dat[0]),tree[L].rans[0]+tree[R].lans[0]);
	return;
}
void build(int cur,int lt,int rt){
	if(lt==rt){
		tree[cur].sum=tree[cur].lans[1]=tree[cur].rans[1]=tree[cur].dat[1]=a[lt];
		tree[cur].lans[0]=tree[cur].rans[0]=tree[cur].dat[0]=(!a[lt]);
		tree[cur].siz=1;
		return;
	}
	int mid=(lt+rt)/2;
	build(cur*2,lt,mid);
	build(cur*2+1,mid+1,rt);
	pushup(cur);
	return;
} 
void addtag_rev(int cur,int lt,int rt,int val){
	tree[cur].retag^=(val%2);
	if(tree[cur].retag){
		tree[cur].sum=(rt-lt+1)-tree[cur].sum;
		swap(tree[cur].lans[0],tree[cur].lans[1]);
		swap(tree[cur].rans[0],tree[cur].rans[1]);
		swap(tree[cur].dat[0],tree[cur].dat[1]);
	}
	return;
}
void addtag_zero(int cur,int lt,int rt){
	tree[cur].retag=tree[cur].ontag=0,tree[cur].zetag=1;
	tree[cur].sum=tree[cur].dat[1]=tree[cur].lans[1]=tree[cur].rans[1]=0;
	tree[cur].dat[0]=tree[cur].lans[0]=tree[cur].rans[0]=rt-lt+1;
	return;
}
void addtag_one(int cur,int lt,int rt){
	tree[cur].retag=tree[cur].zetag=0,tree[cur].ontag=1;
	tree[cur].sum=tree[cur].dat[1]=tree[cur].lans[1]=tree[cur].rans[1]=rt-lt+1;
	tree[cur].dat[0]=tree[cur].lans[0]=tree[cur].rans[0]=0;
	return;
}
void pushdown(int cur,int lt,int rt){
	if(tree[cur].retag==0&&tree[cur].ontag==0&&tree[cur].zetag==0)
		return;
	int mid=(lt+rt)/2;
	if(tree[cur].retag){
		addtag_rev(L,lt,mid,1);
		addtag_rev(R,mid+1,rt,1);
		tree[cur].retag=0;
	}
	if(tree[cur].ontag){
		addtag_one(L,lt,mid);
		addtag_one(R,mid+1,rt);
		tree[cur].ontag=0;
	}
	if(tree[cur].zetag){
		addtag_zero(L,lt,mid);
		addtag_zero(R,mid+1,rt);
		tree[cur].zetag=0;
	}
	return;
}
node query(int cur,int lt,int rt,int px,int py){
	if(lt>py||rt<px)
		return pre;
	if(px<=lt&&rt<=py)
		return tree[cur];
	pushdown(cur,lt,rt);
	int mid=(lt+rt)/2;
	node vx=query(L,lt,mid,px,py);
	node vy=query(R,mid+1,rt,px,py);
	node ans;
	ans.sum=vx.sum+vy.sum;
	ans.lans[1]=vx.lans[1];
	if(vx.rans[1]==vx.siz)
		ans.lans[1]=vx.siz+vy.lans[1]; 
	ans.lans[0]=vx.lans[0];
	if(vx.rans[1]==0)
		ans.lans[0]=vx.siz+vy.lans[0]; 
	ans.rans[1]=vy.rans[1];
	if(vy.lans[1]==vy.siz)
		ans.rans[1]=vy.siz+vx.rans[1];
	ans.rans[0]=vy.rans[0];
	if(vy.lans[1]==0)
		ans.rans[0]=vy.siz+vx.rans[0];
	ans.dat[1]=max(max(vx.dat[1],vy.dat[1]),vx.rans[1]+vy.lans[1]);
	ans.dat[0]=max(max(vx.dat[0],vy.dat[0]),vx.rans[0]+vy.lans[0]);
	return ans;
}
void update_rev(int cur,int lt,int rt,int px,int py,int val){
	if(lt>py||rt<px)
		return;
	if(px<=lt&&rt<=py){
		addtag_rev(cur,lt,rt,val);
		return;
	}
	pushdown(cur,lt,rt);
	int mid=(lt+rt)/2;
	update_rev(L,lt,mid,px,py,val);
	update_rev(R,mid+1,rt,px,py,val);
	pushup(cur);
	return;
}
void update_zero(int cur,int lt,int rt,int px,int py){
	if(lt>py||rt<px)
		return;
	if(px<=lt&&rt<=py){
		addtag_zero(cur,lt,rt);
		return;
	}
	pushdown(cur,lt,rt);
	int mid=(lt+rt)/2;
	update_zero(L,lt,mid,px,py);
	update_zero(R,mid+1,rt,px,py);
	pushup(cur);
	return;
}
void update_one(int cur,int lt,int rt,int px,int py){
	if(lt>py||rt<px)
		return;
	if(px<=lt&&rt<=py){
		addtag_one(cur,lt,rt);
		return;
	}
	pushdown(cur,lt,rt);
	int mid=(lt+rt)/2;
	update_one(L,lt,mid,px,py);
	update_one(R,mid+1,rt,px,py);
	pushup(cur);
	return;
}
signed main(){
	cin>>n>>q;
	pre={0,0,0,0,0,0,0};
	for(int i=1;i<=n;i++)
		cin>>a[i];
	build(1,1,n);
	while(q--){
		int op,x,y;
		cin>>op>>x>>y;
		x++,y++;
		if(op==0)
			update_zero(1,1,n,x,y);
		if(op==1)
			update_one(1,1,n,x,y);
		if(op==2)
			update_rev(1,1,n,x,y,1);
		if(op==3)
			cout<<query(1,1,n,x,y).sum<<"\n";
		if(op==4)
			cout<<query(1,1,n,x,y).dat[1]<<"\n";
	}
	return 0;
}
2024/6/25 11:38
加载中...