HDOJ 题目3911 Black And White(线段树区间异或区间合并)

来源:互联网 发布:mysql 数据库迁移 编辑:程序博客网 时间:2024/05/29 04:09

Black And White

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


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
 

Recommend
lcy   |   We have carefully selected several similar problems for you:  3914 3913 3912 3919 3916 
 过题啊。。。一遍过还是极好的
题目大意:两种操作,0区间异或,1查询区间连续1最长
ac代码

#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#define max(a,b) (a>b?a:b)#define min(a,b) (a>b?b:a)using namespace std;#define maxn 100010struct s{    int ml0,mr0,mx0;    int ml1,mr1,mx1;    int Xor;}node[maxn<<2];void pushup(int tr,int m){    node[tr].ml0=node[tr<<1].ml0;    node[tr].ml1=node[tr<<1].ml1;    node[tr].mr0=node[tr<<1|1].mr0;    node[tr].mr1=node[tr<<1|1].mr1;    if(node[tr<<1].ml0==(m-(m>>1)))        node[tr].ml0+=node[tr<<1|1].ml0;    if(node[tr<<1|1].mr0==(m>>1))        node[tr].mr0+=node[tr<<1].mr0;    node[tr].mx0=max(node[tr<<1].mx0,node[tr<<1|1].mx0);    node[tr].mx0=max(node[tr].mx0,node[tr<<1].mr0+node[tr<<1|1].ml0);        if(node[tr<<1].ml1==(m-(m>>1)))        node[tr].ml1+=node[tr<<1|1].ml1;    if(node[tr<<1|1].mr1==(m>>1))        node[tr].mr1+=node[tr<<1].mr1;    node[tr].mx1=max(node[tr<<1].mx1,node[tr<<1|1].mx1);    node[tr].mx1=max(node[tr].mx1,node[tr<<1].mr1+node[tr<<1|1].ml1);    }void fxor(int tr,int m){        swap(node[tr].ml0,node[tr].ml1);        swap(node[tr].mr0,node[tr].mr1);        swap(node[tr].mx0,node[tr].mx1);}void build(int l,int r,int tr){    node[tr].Xor=0;    if(l==r)    {        int x;        scanf("%d",&x);        node[tr].ml0=node[tr].mr0=node[tr].mx0=(x==0);        node[tr].ml1=node[tr].mr1=node[tr].mx1=(x==1);        return;    }    int mid=(l+r)>>1;    build(l,mid,tr<<1);    build(mid+1,r,tr<<1|1);    pushup(tr,r-l+1);}void pushdown(int tr,int m){    if(m==1)        return;    if(node[tr].Xor)    {        node[tr<<1].Xor^=1;        node[tr<<1|1].Xor^=1;        fxor(tr<<1,m-(m>>1));        fxor(tr<<1|1,m>>1);        node[tr].Xor=0;    }}void update(int L,int R,int l,int r,int tr){    pushdown(tr,r-l+1);    if(L<=l&&r<=R)    {                node[tr].Xor=1;        fxor(tr,r-l+1);        return;    }    int mid=(l+r)>>1;    if(L<=mid)        update(L,R,l,mid,tr<<1);    if(R>mid)        update(L,R,mid+1,r,tr<<1|1);    pushup(tr,r-l+1);}int query_max1(int L,int R,int l,int r,int tr){    if(L<=l&&r<=R)    {        return node[tr].mx1;    }    pushdown(tr,r-l+1);    int mid=(l+r)>>1;    int ans=0;    if(R<=mid)        ans=query_max1(L,R,l,mid,tr<<1);    else        if(L>mid)            ans=query_max1(L,R,mid+1,r,tr<<1|1);        else        {            int ans1=query_max1(L,mid,l,mid,tr<<1);            int ans2=query_max1(mid+1,R,mid+1,r,tr<<1|1);            int ans3=min(node[tr<<1].mr1,mid-L+1)+min(node[tr<<1|1].ml1,R-(mid+1)+1);            ans=max(ans1,max(ans2,ans3));        }    //    pushup(tr,r-l+1);    return ans;}int main(){    int n,m;    while(scanf("%d",&n)!=EOF)    {        build(1,n,1);scanf("%d",&m);        while(m--)        {            int op,x,y;            scanf("%d%d%d",&op,&x,&y);            if(op)                update(x,y,1,n,1);            else            {                 printf("%d\n",query_max1(x,y,1,n,1));            }        }    }} 


0 0
原创粉丝点击