HDU 3911 Black And White

Black And White

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4447    Accepted Submission(s): 1297


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
 

线段树区间合并,这题坑了好久了,因为要把01翻转,所以要存储左边起连0,右边起连0,左边起连1,右边起连2,区间0,区间1,还有lazy标记。终于A了。

#include<stdio.h>
#include<algorithm>
using namespace std;

const int MAXN  =  100005;
int middle1[MAXN<<2],left1[MAXN<<2],right1[MAXN<<2],lazy[MAXN<<2],middle0[MAXN<<2],left0[MAXN<<2],right0[MAXN<<2],num[MAXN];

void pushUp(int m,int x)
{
    left0[x]=left0[x<<1];
    left1[x]=left1[x<<1];
    right0[x]=right0[x<<1|1];
    right1[x]=right1[x<<1|1];
    if(right0[x]==(m>>1))
        right0[x]+=right0[x<<1];
    if(right1[x]==(m>>1))
        right1[x]+=right1[x<<1];
    if(left0[x]==m-(m>>1))
        left0[x]+=left0[x<<1|1];
    if(left1[x]==m-(m>>1))
        left1[x]+=left1[x<<1|1];
    middle0[x] = max(max(middle0[x<<1],middle0[x<<1|1]), right0[x<<1] + left0[x<<1|1]);
    middle1[x] = max(max(middle1[x<<1],middle1[x<<1|1]), right1[x<<1] + left1[x<<1|1]);
}

void change(int x)
{
    swap(middle1[x],middle0[x]);
    swap(left0[x],left1[x]);
    swap(right0[x],right1[x]);
}

void pushDown(int x){
    if(lazy[x])
    {
        lazy[x<<1]^=1;
        lazy[x<<1|1]^=1;
        lazy[x]=0;
        change(x<<1);
        change(x<<1|1);
    }
}

void build(int l,int r,int x)
{
    lazy[x]=left1[x]=right1[x]=left0[x]=right0[x]=middle1[x]=middle0[x]=0;
    if(l==r)
    {
        if(num[l]==1) 
            middle1[x]=left1[x]=right1[x]=1;
        else
            middle0[x]=left0[x]=right0[x]=1;
        return ;
    }
    int m=(l+r)>>1;
    build(l,m,x<<1);
    build(m+1,r,x<<1|1);
    pushUp(r-l+1,x);
}
void update(int L,int R,int l,int r,int x)
{
    if(L<=l&&r<=R)
    {
        lazy[x]^=1;
        change(x);
        return ;
    }
    pushDown(x);
    int m=(l+r)>>1;
    if(L<=m) 
        update(L,R,l,m,x<<1);
    if(R>m) 
        update(L,R,m+1,r,x<<1|1);
    pushUp(r-l+1,x);
}
int query(int L,int R,int l,int r,int x)
{
    if(L<=l&&r<=R)
    {
        return middle1[x];
    }
    pushDown(x);
    int m=(l+r)>>1;
    if ( R <= m )
        return query(L, R, l,m,x<<1);
    if ( L > m )
        return query(L, R, m+1,r,x<<1|1);
    int t1 = query(L, R, l,m,x<<1);
    int t2 = query(L, R, m+1,r,x<<1|1);
    int a = min(m-L+1, right1[x<<1]);
    int b = min(R-m, left1[x<<1|1]);
    return max(max(t1, t2), a + b);
}
int main()
{
    int n,m,a,b,type;
    while(scanf("%d",&n)>0)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&num[i]);
        build(1,n,1);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d%d%d",&type,&a,&b);
            if(type==1)
                update(a,b,1,n,1);
            else
            {
                int ans=query(a,b,1,n,1);
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值