hdu 3397 区间合并 + 异或处理。

本题最困难处就是 operation 2 处理上

由于数据只有 0 和1 两个数字

所以当执行operation 2 处理时 对其异或即可。

#include <iostream>   
#include <cstring>   
#include <cstdio>   
#include <algorithm>   
using namespace std;  
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int Maxn = 111111;  
int sum[Maxn<<2], lmaxn[Maxn<<2], rmaxn[Maxn<<2], maxn[Maxn<<2];  
int co[Maxn<<2];  
void pushdown(int rt,int m)
{
	if(co[rt]!=-1)
	{
		co[rt<<1]=co[rt<<1|1]=co[rt];
		maxn[rt<<1]=lmaxn[rt<<1]=rmaxn[rt<<1]=sum[rt<<1]=co[rt]?(m-(m>>1)):0;
		maxn[rt<<1|1]=lmaxn[rt<<1|1]=rmaxn[rt<<1|1]=sum[rt<<1|1]=co[rt]?(m>>1):0;
		co[rt]=-1;
	}
}
void pushup(int rt,int m)
{
	sum[rt]=sum[rt<<1]+sum[rt<<1|1];
	lmaxn[rt]=lmaxn[rt<<1];
	rmaxn[rt]=rmaxn[rt<<1|1];
	if(lmaxn[rt]==(m-(m>>1))) lmaxn[rt]+=lmaxn[rt<<1|1];
	if(rmaxn[rt]==(m>>1))	  rmaxn[rt]+=rmaxn[rt<<1];
	maxn[rt]=max(max(maxn[rt<<1],maxn[rt<<1|1]),lmaxn[rt<<1|1]+rmaxn[rt<<1]);
}
void build(int l,int r,int rt)
{
	if(l==r)
	{
	    scanf("%d",&sum[rt]);
co[rt]=maxn[rt]=lmaxn[rt]=rmaxn[rt]=sum[rt]; //对其进行标志。该标志决定该数是啥。
		return ;
	}
	co[rt]=-1;
	int m=(l+r)>>1;
	build(lson);
	build(rson);
	pushup(rt,r-l+1);
}
void update(int L,int R,int c,int l,int r,int rt)
{
	if(L<=l&&r<=R)
	{
	    co[rt]=c;
		sum[rt]=maxn[rt]=lmaxn[rt]=rmaxn[rt]=c?r-l+1:0;
		return ;
	}
	pushdown(rt,r-l+1);
	int m=(l+r)>>1;
	if(L<=m) update(L,R,c,lson);
	if(R>m) update(L,R,c,rson);
	pushup(rt,r-l+1);
}
void Xor(int L,int R,int l,int r,int rt)
{
	if(L<=l&&r<=R&&co[rt]!=-1)    //异或处理。换句话理解就是 当其完全是1 或者完全是 0 的时候 转换。
	{//在build()时已经对子叶进行标志了。子叶的co[rt]!=-1;
		co[rt]^=1;
		sum[rt]=maxn[rt]=lmaxn[rt]=rmaxn[rt]=co[rt]?r-l+1:0;
		return ;
	}
	pushdown(rt,r-l+1);
	int m=(l+r)>>1;
	if(L<=m) Xor(L,R,lson);
	if(R>m)  Xor(L,R,rson);
	pushup(rt,r-l+1);
}
int query(int L,int R,int l,int r,int rt)
{
	if(L<=l&&r<=R)
		return sum[rt];
	pushdown(rt,r-l+1);
	int m=(l+r)>>1,ret=0;
	if(L<=m)ret+=query(L,R,lson);
	if(R>m) ret+=query(L,R,rson);
	return ret;
}
int len(int L,int R,int l,int r,int rt)
{
	if(L==l&&r==R)
		return maxn[rt];
	pushdown(rt,r-l+1);
	int m=(l+r)>>1;
	if(R<=m) return len(L,R,lson);
	if(L>m)  return len(L,R,rson);
	int ans=max(len(L,m,lson),len(m+1,R,rson));
	int ret=min(lmaxn[rt<<1|1],R-m)+min(rmaxn[rt<<1],m-L+1);
	return max(ans,ret);
}
int main()
{
    int cas;
	scanf("%d",&cas);
	while(cas--)
	{
	    int n,m;
		scanf("%d %d",&n,&m);
		n--;
		build(0,n,1);
		while(m--)
		{
		    int c,l,r;
			scanf("%d %d %d",&c,&l,&r);
			if(c<=1)
				update(l,r,c,0,n,1);
			else if(c==2)
				Xor(l,r,0,n,1);
			else if(c==3)
				printf("%d\n",query(l,r,0,n,1));
			else
				printf("%d\n",len(l,r,0,n,1));
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值