hdu 3397

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cstring>
#include <list>
#include <queue>
#include <stack>
#include <cmath>

using namespace std;

#define PF(x) (scanf("%d",&x))
#define PT(x,y) (scanf("%d%d",&x,&y))
#define PR(x) (printf("%d\n",x))
#define PRT(x,y)(printf("%d %d\n",x,y))
#define PB(x)(scanf("%I64d",&x))
#define PRB(x)(printf("%I64d\n",(x)))
#define For(i,n) for(int i=0;i<(n);i++)
#define CLR(ar) (memset(ar,0,sizeof(ar)))
#define CLR1(ar) (memset(ar,-1,sizeof(ar)))
#define PBE(x) (scanf("%I64d",&x)!=EOF)
#define Max(x,y) (x)>(y)?(x):(y)
#define Min(x,y) (x)>(y)?(y):(x)
#define L(x) (x<<1)
#define R(x) ((x<<1)|1)
#define Mid(x,y) ((x+y)>>1)

typedef __int64 LL;
#define N 100005
#define M 105
#define Mod 1000
#define Inf 0x7fffffff

struct tree
{
	int l,r;
	int var;
	int cover;
	int len;
	int lens()
	{
		return r -l+1;
	}
};
tree T[4*N];
int ar[N];
int rl,rr;
int maxlen;
int n,m;

void build(int l,int r,int k)
{
	T[k].l = l;
	T[k].r = r;
	T[k].var = -1;
	T[k].cover = 0;
	T[k].len = 0;
	if(l == r)
	{
		T[k].var = ar[l];
		T[k].cover = 1;
		if(ar[l]==1) T[k].len = 1;
		return ;
	}
	int mid = Mid(l,r);
	build(l,mid,L(k));
	build(mid+1,r,R(k));
	if(T[L(k)].cover && T[R(k)].cover && T[L(k)].var == T[R(k)].var)
		T[k].var = T[L(k)].var,T[k].cover = 1;
	T[k].len = T[L(k)].len + T[R(k)].len ;
}

void update(int l,int r,int k,int var)
{
	if(T[k].l>=l && T[k].r<=r)
	{
		T[k].var = var;
		if(var == 1) T[k].len = T[k].lens();
		else T[k].len = 0;
		T[k].cover = 1;
		return ;
	}
	if(T[k].var == var && T[k].cover) return;

	if(T[k].cover==1)
	{
		T[L(k)].var = T[k].var;
		T[R(k)].var = T[k].var;
		T[L(k)].cover = T[R(k)].cover = 1;
		if(T[k].var == 1) T[R(k)].len = T[R(k)].lens(),T[L(k)].len = T[L(k)].lens();
		else T[R(k)].len = 0,T[L(k)].len = 0;
		T[k].cover = 0;
	}
	int mid = Mid(T[k].l,T[k].r);
	if(l<=mid) update(l,r,L(k),var);
	if(r>mid) update(l,r,R(k),var);
	if(T[L(k)].cover && T[R(k)].cover && T[L(k)].var == T[R(k)].var)
		T[k].var = T[L(k)].var,T[k].cover = 1;
	T[k].len = T[L(k)].len + T[R(k)].len;

}
void handleor(int l,int r,int k)
{
	if(T[k].l>=l && T[k].r <= r && T[k].cover)
	{
		T[k].var ^= 1;
		if(T[k].var ==1) T[k].len = T[k].lens();
		else T[k].len = 0;
		return ;
	}

	if(T[k].cover)
	{
		T[L(k)].var = T[k].var;
		T[R(k)].var = T[k].var;
		T[L(k)].cover = T[R(k)].cover = 1;
		if(T[k].var == 1) T[R(k)].len = T[R(k)].lens(),T[L(k)].len = T[L(k)].lens();
		else T[R(k)].len = 0,T[L(k)].len = 0;
		T[k].cover = 0;
	}

	int mid = Mid(T[k].l ,T[k].r);
	if(l<=mid) handleor(l,r,L(k));
	if(r>mid) handleor(l,r,R(k));
	if(T[L(k)].cover && T[R(k)].cover && T[L(k)].var == T[R(k)].var)
		T[k].var = T[L(k)].var,T[k].cover = 1;
	
	T[k].len = T[L(k)].len + T[R(k)].len;
}

int ll(int l,int r,int k)
{
	if(T[k].r == r && T[k].l == l)
	{
		return T[k].len;
	}
	if(T[k].cover)
	{
		if(T[k].var==1) return r-l+1;
		return 0;
	}
	int mid = Mid(T[k].l,T[k].r);
	if(r<=mid) return ll(l,r,L(k));
	if(l>mid) return ll(l,r,R(k));
	return ll(l,mid,L(k)) + ll(mid+1,r,R(k));
}

void len(int l,int r,int k)
{
	if(T[k].l>=l && T[k].r<=r && T[k].cover)
	{
		if(T[k].var == 1)
		{
			if(rl == -1) {rl=T[k].l ,rr = T[k].r;return ;}
			else if(rr+1==T[k].l) {rr = T[k].r;return ;}
			else {maxlen = Max(maxlen,rr-rl+1);rl=T[k].l ,rr = T[k].r;return ;}
		}
		return ;
	}

	if(T[k].cover)
	{
		T[L(k)].var = T[k].var;
		T[R(k)].var = T[k].var;
		T[L(k)].cover = T[R(k)].cover = 1;
		if(T[k].var == 1) T[R(k)].len = T[R(k)].lens(),T[L(k)].len = T[L(k)].lens();
		else T[R(k)].len = 0,T[L(k)].len = 0;
		T[k].cover = 0;
	}

	int mid = Mid(T[k].l ,T[k].r);
	if(l<=mid) len(l,r,L(k));
	if(r>mid) len(l,r,R(k));
	if(T[L(k)].cover && T[R(k)].cover && T[L(k)].var == T[R(k)].var)
		T[k].var = T[L(k)].var,T[k].cover = 1;
	
	T[k].len = T[L(k)].len + T[R(k)].len;
	
}
void init()
{
	//freopen("fds.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int test;
	PF(test);
	while(test--)
	{
		PT(n,m);
		For(i,n) PF(ar[i+1]);
		build(1,n,1);

		For(i,m)
		{
			int x,y,z;
			PT(x,y);PF(z);
			y++;z++;
			switch(x)
			{
			case 0:update(y,z,1,0);break;
			case 1:update(y,z,1,1);break;
			case 2:handleor(y,z,1);break;
			case 3:PR(ll(y,z,1));break;
			case 4: rl = -1;rr = -1;maxlen=0;
				len(y,z,1);
				if(rl!=-1) maxlen = Max(maxlen,rr-rl+1);
				PR(maxlen);
				break;
			}
		}
	}
	return ;
}

int main()
{
	init();
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值