样例不过,求调
查看原帖
样例不过,求调
561907
Furina_Saikou楼主2024/7/31 14:30
#include<bits/stdc++.h>
#define ls (p<<1)
#define rs (p<<1|1)
#define mid (l+r>>1)
#define int long long
using namespace std;
const int N=1919810,INF=0x7ffffffffff;
int n,m,tr[N],lf[N][2],maxn[N][2],rt[N][2],tag1[N],tag2[N];
void push_up(int p,int l,int r)
{
	tr[p]=tr[ls]+tr[rs];
	maxn[p][1]=max({rt[ls][1]+lf[rs][1],maxn[ls][1],maxn[rs][1]});
	maxn[p][0]=max({rt[ls][0]+lf[rs][0],maxn[ls][0],maxn[rs][0]});
	if(lf[ls][1]==mid-l+1)lf[p][1]=lf[ls][1]+lf[rs][1];
	else lf[p][1]=lf[ls][1];
	if(lf[ls][0]==mid-l+1)lf[p][0]=lf[ls][0]+lf[rs][0];
	else lf[p][0]=lf[ls][0];
	if(rt[rs][1]==r-mid)rt[p][1]=rt[rs][1]+rt[ls][1];
	else rt[p][1]=rt[rs][1];
	if(rt[rs][0]==r-mid)rt[p][0]=rt[rs][0]+rt[ls][0];
	else rt[p][0]=rt[rs][0];
}
void push_down(int p,int l,int r)
{
	if(~tag1[p])
	{
		tag1[ls]=tag1[rs]=tag1[p];
		tag2[ls]=tag2[rs]=0;
		maxn[ls][1]=lf[ls][1]=rt[ls][1]=tr[ls]=(mid-l+1)*tag1[p];
		maxn[ls][0]=lf[ls][0]=rt[ls][0]=(mid-l+1)*(tag1[p]^1);
		maxn[rs][1]=lf[rs][1]=rt[rs][1]=tr[rs]=(r-mid)*tag1[p];
		maxn[rs][0]=lf[rs][0]=rt[rs][0]=(r-mid)*(tag1[p]^1);
		tag1[p]=-1;
	}
	if(tag2[p])
	{
		tag2[ls]^=1;
		tr[ls]=(mid-l+1)-tr[ls];
		swap(maxn[ls][1],maxn[ls][0]);
		swap(lf[ls][1],lf[ls][0]);
		swap(rt[ls][1],rt[ls][0]);
		tag2[rs]^=1;
		tr[rs]=(r-mid)-tr[rs];
		swap(maxn[rs][1],maxn[rs][0]);
		swap(lf[rs][1],lf[rs][0]);
		swap(rt[rs][1],rt[rs][0]);
		tag2[p]=0;
	}
}
void build(int p,int l,int r)
{
	tag1[p]=-1;
	if(l==r)
	{
		cin>>tr[p];
		maxn[p][1]=lf[p][1]=rt[p][1]=tr[p];
		maxn[p][0]=lf[p][0]=rt[p][0]=tr[p]^1;
		return;
	}
	build(ls,l,mid);
	build(rs,mid+1,r);
	push_up(p,l,r);
}
void update(int p,int l,int r,int L,int R,int k)
{
	if(l>=L&&r<=R)
	{
		tag1[p]=k;
		tag2[p]=0;
		maxn[p][1]=lf[p][1]=rt[p][1]=tr[p]=k*(r-l+1);
		maxn[p][0]=lf[p][0]=rt[p][0]=(k^1)*(r-l+1);
		return;
	}
	push_down(p,l,r);
	if(mid>=L)update(ls,l,mid,L,R,k);
	if(mid<R)update(rs,mid+1,r,L,R,k);
	push_up(p,l,r);
}
void xors(int p,int l,int r,int L,int R)
{
	if(l>=L&&r<=R)
	{
		tag2[p]^=1;
		tr[p]=(r-l+1)-tr[p];
		swap(maxn[p][1],maxn[p][0]);
		swap(lf[p][1],lf[p][0]);
		swap(rt[p][1],rt[p][0]);
		return;
	}
	push_down(p,l,r);
	if(mid>=L)xors(ls,l,mid,L,R);
	if(mid<R)xors(rs,mid+1,r,L,R);
	push_up(p,l,r);
}
int query_sum(int p,int l,int r,int L,int R)
{
	if(l>=L&&r<=R)
	{
		return tr[p];
	}
	push_down(p,l,r);
	int ans=0;
	if(mid>=L)ans+=query_sum(ls,l,mid,L,R);
	if(mid<R)ans+=query_sum(rs,mid+1,r,L,R); 
	return ans;
}
int query_max(int p,int l,int r,int L,int R)
{
	if(l>=L&&r<=R)
	{
		return maxn[p][1];
	}
	push_down(p,l,r);
	int ql=-INF,qr=-INF;
	if(mid>=L)ql=query_max(ls,l,mid,L,R);
	if(mid<R)qr=query_max(rs,mid+1,r,L,R);
	if(mid>=L&&mid<R)return max({ql,qr,rt[ls][1]+lf[rs][1]});
	return max(ql,qr);
}
signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	build(1,1,n);
	while(m--)
	{
		int op,l,r;
		cin>>op>>l>>r;
		l++,r++;
		if(!op)
		{
			update(1,1,n,l,r,0);
		}else if(op==1)
		{
			update(1,1,n,l,r,1);
		}else if(op==2)
		{
			xors(1,1,n,l,r);
		}else if(op==3)
		{
			cout<<query_sum(1,1,n,l,r)<<"\n";
		}else
		{
			cout<<query_max(1,1,n,l,r)<<"\n";
		}
	}
	return 0;
}
2024/7/31 14:30
加载中...