P2572 [SCOI2010]序列操作

没什么好说的,细节题
注释放代码里

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+7;
template <class I>
inline void read(I &x){
    int f=1;
    char c;
    for(c=getchar();c<'0'||c>'9';c=getchar()) if(c=='-') f=-1;
    for(x=0;c>='0'&&c<='9';x=(x<<3)+(x<<1)+(c&15),c=getchar());
    x*=f;
}
int n,m;
bool a[N];
struct tree{
	int l=1<<30,r=0,sum=0,lm1=0,rm1=0,lm0=0,rm0=0,mx1=0,mx0=0,op1=-1;
	bool xr=0;
	tree(int l=0,int r=0,int sum=0,int lm1=0,int rm1=0,int lm0=0,int rm0=0,int mx1=0,int mx0=0,int op1=-1,bool xr=0):
	l(l),r(r),sum(sum),lm1(lm1),rm1(rm1),lm0(lm0),rm0(rm0),mx1(mx1),mx0(mx0),op1(op1),xr(xr){}
	#define l(x) t[x].l
	#define r(x) t[x].r
	#define mx1(x) t[x].mx1
	#define mx0(x) t[x].mx0
	#define sum(x) t[x].sum
	#define lm1(x) t[x].lm1
	#define rm1(x) t[x].rm1
	#define lm0(x) t[x].lm0
	#define rm0(x) t[x].rm0
	#define op1(x) t[x].op1
	#define xr(x) t[x].xr
}t[4*N];
inline void c1(int p,bool v){
	sum(p)=v*(r(p)-l(p)+1);
	lm1(p)=v*(r(p)-l(p)+1);
	rm1(p)=v*(r(p)-l(p)+1);
	mx1(p)=v*(r(p)-l(p)+1);
	lm0(p)=(v^1)*(r(p)-l(p)+1);
	rm0(p)=(v^1)*(r(p)-l(p)+1);
	mx0(p)=(v^1)*(r(p)-l(p)+1);
	op1(p)=v;
	xr(p)=0;
	return;
}
inline void c2(int p){
	sum(p)=r(p)-l(p)+1-sum(p);
	swap(lm1(p),lm0(p));
	swap(rm1(p),rm0(p));
	swap(mx1(p),mx0(p));
	xr(p)^=1;//注意异或
	if(op1(p)!=-1) op1(p)^=1,xr(p)=0;
	return;
}
inline void pd(int p){
	if(l(p)==r(p)) return;
	if(op1(p)!=-1){
		c1(p*2,op1(p));
		c1(p*2+1,op1(p));
		op1(p)=-1;
	}
	if(xr(p)){
		c2(p*2);
		c2(p*2+1);
		xr(p)=0;
		return;
	}
}
inline void upd(int p){
	sum(p)=sum(p*2)+sum(p*2+1);
	if(lm1(p*2)==r(p*2)-l(p*2)+1) lm1(p)=lm1(p*2)+lm1(p*2+1);
		else lm1(p)=lm1(p*2);
	if(rm1(p*2+1)==r(p*2+1)-l(p*2+1)+1) rm1(p)=rm1(p*2)+rm1(p*2+1);
		else rm1(p)=rm1(p*2+1);
	if(lm0(p*2)==r(p*2)-l(p*2)+1) lm0(p)=lm0(p*2)+lm0(p*2+1);
		else lm0(p)=lm0(p*2);
	if(rm0(p*2+1)==r(p*2+1)-l(p*2+1)+1) rm0(p)=rm0(p*2)+rm0(p*2+1);
		else rm0(p)=rm0(p*2+1);
	mx0(p)=max(mx0(p*2),max(mx0(p*2+1),rm0(p*2)+lm0(p*2+1)));
	mx1(p)=max(mx1(p*2),max(mx1(p*2+1),rm1(p*2)+lm1(p*2+1)));
	return;
}
void build(int p,int l,int r){
	l(p)=l,r(p)=r,op1(p)=-1;
	if(l==r){
		c1(p,a[l]);
		return;
	}
	int mid=(l+r)/2;
	build(p*2,l,mid);
	build(p*2+1,mid+1,r);
	upd(p);
	return;
}
void change1(int p,int l,int r,bool v){
	if(l(p)>=l&&r(p)<=r){
		c1(p,v);
		return;
	}
	pd(p);
	int mid=(l(p)+r(p))/2;
	if(mid>=l) change1(p*2,l,r,v);
	if(mid+1<=r) change1(p*2+1,l,r,v);
	upd(p);
	return;
}
void change2(int p,int l,int r){
	if(l(p)>=l&&r(p)<=r){
		c2(p);
		return;
	}
	pd(p);
	int mid=(l(p)+r(p))/2;
	if(mid>=l) change2(p*2,l,r);
	if(mid<r) change2(p*2+1,l,r);
	upd(p);
	return;
}
int ask1(int p,int l,int r){
	if(l(p)>=l&&r(p)<=r){
		return sum(p);
	}
	pd(p);
	int res=0;
	int mid=(l(p)+r(p))/2;
	if(mid>=l) res+=ask1(p*2,l,r);
	if(mid<r) res+=ask1(p*2+1,l,r);
	upd(p);
	return res;
}
tree ask2(int p,int l,int r){//写这一段一定要注意各种边界问题之类的
	if(l(p)>=l&&r(p)<=r){
		return t[p];
	}
	pd(p);
	tree res,res1,res2;
	int mid=(l(p)+r(p))/2;
	if(mid>=l) res1=ask2(p*2,l,r);
	if(mid<r) res2=ask2(p*2+1,l,r);
	if(mid<l) return res2;
	if(mid>=r) return res1;
	res.l=res1.l,res.r=res2.r;
	if(res1.lm1==res1.r-res1.l+1) res.lm1=res1.lm1+res2.lm1;
		else res.lm1=res1.lm1;
	if(res2.rm1==res2.r-res2.l+1) res.rm1=res2.rm1+res1.rm1;
		else res.rm1=res2.rm1;
	res.mx1=max(res1.mx1,max(res2.mx1,res1.rm1+res2.lm1));
	return res;
}
int main(){
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	read(n),read(m);
	for(int i=1;i<=n;i++)
		read(a[i]);
	build(1,1,n);
	for(int i=1;i<=m;i++){
		int op,A,b;
		read(op),read(A),read(b);
		A++,b++;
		if(op==0) change1(1,A,b,0);
		if(op==1) change1(1,A,b,1);
		if(op==2) change2(1,A,b);
		if(op==3) printf("%d\n",ask1(1,A,b));
		if(op==4) printf("%d\n",ask2(1,A,b).mx1);
	}
	return 0;
}
posted @ 2019-10-16 11:33  天才美少女雪乃  阅读(137)  评论(0编辑  收藏  举报