HDU 3911 Black And White

来源:互联网 发布:apache安装包下载 编辑:程序博客网 时间:2024/05/22 08:29

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
41 0 1 050 1 41 2 30 1 41 3 30 4 4
 

Sample Output
120
 

Source
2011 Multi-University Training Contest 8 - Host by HUST
 

线段树区间合并,这题坑了好久了,因为要把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
原创粉丝点击