hdu 3911 Black And White

http://acm.hdu.edu.cn/showproblem.php?pid=3911

好吧,纠结很久才发现的那个错误。泪奔。。。。。

用到了线段树中的懒操作,具体的话,就看线段树里面的结点信息吧==!

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <vector>
using namespace std;

const int maxn = 100005;
struct node
{
	int l;
	int r;
	int maxb;   //最长的黑色连续长度
	int maxw;   //最长的白色连续长度
	int lmax;   //左边的连续最大长度
	int rmax;   //右边的连续最大长度
	int lcor;   //左边的颜色
	int rcor;   //右边的颜色
	int tag;    //左右儿子是否将要改变  1是 0否
}T[4*maxn];
int N;
int a[maxn];

void Build(int t,int l,int r)
{
	T[t].l=l;
	T[t].r=r;
	T[t].tag=0;
	T[t].lmax=0;
	T[t].rmax=0;
	T[t].lcor=0;
	T[t].rcor=0;
	T[t].maxb=0;
	T[t].maxw=0;
	if(l<r)
	{
		int mid=(l+r)/2;
		Build(2*t,l,mid);
		Build(2*t+1,mid+1,r);
	}
	return ;
}
void Change(int t)
{
	T[t].lcor=T[t].lcor^1;
	T[t].rcor=T[t].rcor^1;
	int temp=T[t].maxb;
	T[t].maxb=T[t].maxw;
	T[t].maxw=temp;
	T[t].tag=T[t].tag^1;
}
void Update_t(int t)
{
	T[t].lcor=T[2*t].lcor;
	T[t].rcor=T[2*t+1].rcor;
	T[t].lmax=T[2*t].lmax;
	//if(T[2*t].lcor==T[2*t].rcor && T[2*t].rcor==T[2*t+1].lcor)  一直WA在这里
	//    T[t].lmax=T[2*t].lmax+T[2*t+1].lmax;
	if(T[2*t].lmax==(T[2*t].r-T[2*t].l+1) && T[2*t].rcor==T[2*t+1].lcor)
		T[t].lmax=T[2*t].lmax+T[2*t+1].lmax;

	T[t].rmax=T[2*t+1].rmax;
	if( (T[2*t+1].rmax==T[2*t+1].r-T[2*t+1].l+1) && T[2*t].rcor==T[2*t+1].lcor )
		T[t].rmax=T[2*t+1].rmax+T[2*t].rmax;

	T[t].maxb=max(T[2*t].maxb,T[2*t+1].maxb);
	T[t].maxw=max(T[2*t].maxw,T[2*t+1].maxw);
	if(T[2*t].rcor==T[2*t+1].lcor && T[2*t+1].lcor==1)
		T[t].maxb=max(T[t].maxb,T[2*t].rmax+T[2*t+1].lmax);
	if(T[2*t].rcor==T[2*t+1].lcor && T[2*t+1].lcor==0)
		T[t].maxw=max(T[t].maxw,T[2*t].rmax+T[2*t+1].lmax);
}

void Init_T(int t)
{
	if(T[t].l==T[t].r)
	{
		T[t].tag=0;
		T[t].lmax=T[t].rmax=1;
		if(a[T[t].l]==1)
		{
			T[t].maxw=0;
			T[t].maxb=1;
			T[t].lcor=T[t].rcor=1;
		}
		else
		{
			T[t].maxb=0;
			T[t].maxw=1;
			T[t].lcor=T[t].rcor=0;
		}
		return ;
	}
	Init_T(2*t);
	Init_T(2*t+1);
	Update_t(t);
	return ;
}


void Update(int t,int l,int r)
{
	if(T[t].l==l && T[t].r==r)
	{
		Change(t);
		return ;
	}
	if(T[t].tag==1)
	{
		Change(2*t);
		Change(2*t+1);
		T[t].tag=0;
	}
	int mid=(T[t].l+T[t].r)/2;
	if(r<=mid)
		Update(2*t,l,r);
	else if(l>=mid+1)
		Update(2*t+1,l,r);
	else
	{
		Update(2*t,l,mid);
		Update(2*t+1,mid+1,r);
	}

	Update_t(t);
	return ;
}

int Query(int t,int l,int r)
{
	if(T[t].l==l && T[t].r==r)
		return T[t].maxb;
	if(T[t].tag==1)
	{
		Change(2*t);
		Change(2*t+1);
		T[t].tag=0;
	}
	int t1=0,t2=0,t3=0,t4=0,t5=0;
	int mid=(T[t].l+T[t].r)/2;
	if(r<=mid)
		t1=Query(2*t,l,r);
	else if(l>=mid+1)
		t2=Query(2*t+1,l,r);
	else
	{
		t3= Query(2*t,l,mid);
		t4= Query(2*t+1,mid+1,r);
		if(T[2*t].rcor==1 && T[2*t+1].lcor==1)
			t5= min(mid-l+1,T[2*t].rmax)+min(r-mid,T[2*t+1].lmax);
	}
	Update_t(t);
	return max(t1,max(max(t2,t3),max(t4,t5)));
}

int main()
{
	int m,x,l,r;
	while(scanf("%d",&N)!=EOF)
	{
		for(int i=1;i<=N;i++)
			scanf("%d",&a[i]);
		Build(1,1,N);
		Init_T(1);
		scanf("%d",&m);
		while(m--)
		{
			scanf("%d %d %d",&x,&l,&r);
			if(x==0)
			{
				int sum=Query(1,l,r);
				printf("%d\n",sum);
			}
			else
				Update(1,l,r);
		}
	}
	return 0;
} 

posted on 2011-08-09 00:16  lwbaptx  阅读(579)  评论(0编辑  收藏  举报

导航