HDU 3911 Black And White (线段树区间更新)
扫描二维码
随时随地手机看文章
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3911
题面:
Black And White Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4358 Accepted Submission(s): 1271
Problem Description There are a bunch of stones on the beach; Stone color is white or black. Little Sheep has a magic brush, she can change the color of a continuous stone, black to white, white to black. Little Sheep like black very much, so she want to know the longest period of consecutive black stones in a range [i, j].
Input There are multiple cases, the first line of each case is an integer n(1<= n <= 10^5), followed by n integer 1 or 0(1 indicates black stone and 0 indicates white stone), then is an integer M(1<=M<=10^5) followed by M operations formatted as x i j(x = 0 or 1) , x=1 means change the color of stones in range[i,j], and x=0 means ask the longest period of consecutive black stones in range[i,j]
Output When x=0 output a number means the longest length of black stones in range [i,j].
Sample Input 4 1 0 1 0 5 0 1 4 1 2 3 0 1 4 1 3 3 0 4 4
Sample Output 1 2 0
Source 2011 Multi-University Training Contest 8 - Host by HUST
题目大意:
初始给定一01序列,两种操作,1 a b,取反a到b间的元素,0 a b,问a b区间内最长的连续1的长度。
解题:
第一颗区间更新的线段树,来得有点晚啊,借鉴、理解也算是写出来了。题意看似简单,合并操作比较复杂。需要维护5个值。maxw,maxb,le,ri,lazy。
maxw/maxb,该区间的最长连续0/1长度,取反时直接交换两值。
le/ri,该区间的左右连续块长度,用于合并。
lazy该区间是否需要继续向下更新。
代码:
#include
#include
#include
#define siz 100010
//左右子树编号
#define ls i<<1
#define rs (i<<1)|1
using namespace std;
//左右边界,该区间最多连续黑块数,白块数,因为反转区间需要
//区间左边界连续块数,右边解连续块数,正表黑,负表白,用于区间合并
//lazy标记如果为1,代表更新到该区间,其下区间尚未更新
struct SEGTree
{
int l,r,maxb,maxw,le,ri,lazy;
}STree[siz<<2];
int a[siz];
//取大
int max(int x,int y)
{
return x>y?x:y;
}
//取小
int min(int a,int b)
{
return a0&&STree[rs].le>0)
{
tmp=STree[ls].ri+STree[rs].le;
if(tmp>maxb)
maxb=tmp;
}
//更新
STree[i].maxb=maxb;
//白块更新同上
int maxw=max(STree[ls].maxw,STree[rs].maxw);
if(STree[ls].ri<0&&STree[rs].le<0)
{
tmp=STree[ls].ri+STree[rs].le;
tmp=-tmp;
if(tmp>maxw)
maxw=tmp;
}
STree[i].maxw=maxw;
//区间左端最长连续块数le,如果左子区单色,且能与右子区相连,更新le
if(abs(STree[ls].le)==(STree[ls].r-STree[ls].l+1)&&(STree[ls].ri*STree[rs].le>0))
STree[i].le=STree[ls].le+STree[rs].le;
//否则直接取左子区le
else STree[i].le=STree[ls].le;
//更新ri,同上
if(abs(STree[rs].ri)==(STree[rs].r-STree[rs].l+1)&&(STree[ls].ri*STree[rs].le>0))
STree[i].ri=STree[ls].ri+STree[rs].ri;
else STree[i].ri=STree[rs].ri;
}
//向下更新
void push_down(int i)
{
//该区间的懒标记清零
STree[i].lazy=0;
//更新左右子区间
swap(STree[ls].maxw,STree[ls].maxb);
STree[ls].le=-STree[ls].le;
STree[ls].ri=-STree[ls].ri;
STree[ls].lazy=!STree[ls].lazy;
swap(STree[rs].maxw,STree[rs].maxb);
STree[rs].le=-STree[rs].le;
STree[rs].ri=-STree[rs].ri;
STree[rs].lazy=!STree[rs].lazy;
}
//建树
void build(int i,int l,int r)
{
STree[i].l=l;
STree[i].r=r;
STree[i].lazy=0;
//叶子节点
if(l==r)
{
//如果是黑块
if(a[l])
{
STree[i].maxb=1;
STree[i].maxw=0;
STree[i].le=1;
STree[i].ri=1;
}
//白块
else
{
STree[i].maxw=1;
STree[i].maxb=0;
STree[i].le=-1;
STree[i].ri=-1;
}
return;
}
//向下递归
int mid=(l+r)>>1;
build(i<<1,l,mid);
build((i<<1)|1,mid+1,r);
//向上更新
push_up(i);
}
void update(int i,int l,int r)
{
//如果刚好区间吻合
if(STree[i].l==l&&STree[i].r==r)
{
//黑白块交换
swap(STree[i].maxw,STree[i].maxb);
STree[i].le=-STree[i].le;
STree[i].ri=-STree[i].ri;
//lazy取反
STree[i].lazy=!STree[i].lazy;
return;
}
//如果没有刚好吻合,要访问的区间为当前区间子区间,
//且该区间还有懒标记未下放,则向下更新,并向下访问
if(STree[i].lazy)
push_down(i);
int mid=(STree[i].l+STree[i].r)>>1;
if(r<=mid)
update(ls,l,r);
else if(l>mid)
update(rs,l,r);
else
{
update(ls,l,mid);
update(rs,mid+1,r);
}
//更新之后,向上更新
push_up(i);
}
int query(int i,int l,int r)
{
//如果刚好吻合,则返回区间最大连续黑块
if(l==STree[i].l&&r==STree[i].r)
return STree[i].maxb;
int mid=(STree[i].l+STree[i].r)>>1;
//不能完全符合,且该区间尚有懒标记,下放懒标记
if(STree[i].lazy)
push_down(i);
//向下查询
if(r<=mid)
return query(ls,l,r);
else if(l>mid)
return query(rs,l,r);
else
{
int res,tmp;
//取左右最值,合并的最值
res=max(query(ls,l,mid),query(rs,mid+1,r));
//此处不仅仅合并左右区间最值,还需限制左右边界不能超出查询区间以mid为中心的左右边界
tmp=min(mid-l+1,STree[ls].ri)+min(r-mid,STree[rs].le);
if(tmp>res)
res=tmp;
return res;
}
}
int main()
{
int n,q,oper,fm,to;
while(~scanf("%d",&n))
{
//读入
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
//建树
build(1,1,n);
scanf("%d",&q);
for(int i=1;i<=q;i++)
{
scanf("%d",&oper);
scanf("%d%d",&fm,&to);
if(oper)
update(1,fm,to);
else
{
int ans=query(1,fm,to);
printf("%dn",ans);
}
}
}
return 0;
}