Hdu 3911 Black And White (line segment tree)

Source: Internet
Author: User

This question is very troublesome. What is annoying is that the question is actually nothing gold, that is, the delay mark plus range merge.

At half past one yesterday evening, I got up and started to do this. From half past one to, submit wa. At that time, I went to bed and started debugging in the morning.

After making a small modification, I submitted it on a trial basis, without any suspense. After debugging for more than half a day, I finally found a very deep-hidden error. after submitting the error, I found 796 msAC.

The problem is that we are looking for the continuous sequence of the longest 1, but because the operation in the question is an exception or operation, we have to record the information of 0. When updating node information, convert 1 to 0 and 0 to 1. The other part is the two basic operations of the Line Segment tree.

 

#include<stdio.h>#include<string.h>#define N 100005struct node{    int x,y;    int flag;    int ll,lr;    int max1,max0;    int lflag,rflag;}a[N*3];int b[N];int Max(int x,int y){    if(x>y)        return x;    else        return y;}int Min(int x,int y){    if(x<y)        return x;    else         return y;}void CreatTree(int t,int x,int y){    a[t].x=x;    a[t].y=y;    a[t].flag=0;    a[t].ll=a[t].lr=y-x+1;    a[t].max1=0;    a[t].max0=y-x+1;    a[t].lflag=a[t].rflag=0;    //printf("%d %d %d %d\n",a[t].x,a[t].y,a[t].max0,a[t].max1);    if(x==y)        return ;    int temp=t*2;    int mid=(x+y)/2;    CreatTree(temp,x,mid);    CreatTree(temp+1,mid+1,y);    //printf("%d %d %d %d\n",a[t].x,a[t].y,a[t].max0,a[t].max1);    return ;}void ChangeTree(int t){    int temp;    temp=a[t].max0;    a[t].max0=a[t].max1;    a[t].max1=temp;    a[t].lflag=(a[t].lflag+1)%2;    a[t].rflag=(a[t].rflag+1)%2;    a[t].flag=(a[t].flag+1)%2;    return ;}void InsertTree(int t,int x,int y){    if(a[t].x==x&&a[t].y==y)    {        ChangeTree(t);        //printf("%d %d %d %d\n",a[t].x,a[t].y,a[t].max0,a[t].max1);        return ;    }    int temp=t*2;    int mid=(a[t].x+a[t].y)/2;    if(a[t].flag)    {        ChangeTree(temp);        ChangeTree(temp+1);        a[t].flag=0;    }    if(y<=mid)        InsertTree(temp,x,y);    else if(x>mid)        InsertTree(temp+1,x,y);    else    {        InsertTree(temp,x,mid);        InsertTree(temp+1,mid+1,y);    }    int tt=0;    if(a[temp].rflag==a[temp+1].lflag)        tt=a[temp].lr+a[temp+1].ll;    if(a[temp].rflag==0&&tt)        a[t].max0=Max(tt,Max(a[temp].max0,a[temp+1].max0));    else        a[t].max0=Max(a[temp].max0,a[temp+1].max0);    if(a[temp].rflag==1&&tt)        a[t].max1=Max(tt,Max(a[temp].max1,a[temp+1].max1));    else        a[t].max1=Max(a[temp].max1,a[temp+1].max1);    if(a[temp].ll==a[temp].y-a[temp].x+1&&tt)        a[t].ll=tt;    else        a[t].ll=a[temp].ll;    a[t].lflag=a[temp].lflag;    if(a[temp+1].lr==a[temp+1].y-a[temp+1].x+1&&tt)        a[t].lr=tt;    else        a[t].lr=a[temp+1].lr;    a[t].rflag=a[temp+1].rflag;    //printf("%d %d %d %d\n",a[t].x,a[t].y,a[t].max0,a[t].max1);    return ;}int FindTree(int t,int x,int y){    if(a[t].x==x&&a[t].y==y)        return a[t].max1;    int temp=t*2;    int mid=(a[t].x+a[t].y)/2;    if(a[t].flag)    {        ChangeTree(temp);        ChangeTree(temp+1);        a[t].flag=0;    }    if(y<=mid)        return FindTree(temp,x,y);    else if(x>mid)        return FindTree(temp+1,x,y);    else    {        int tt,lt,rt;        if(a[temp].rflag==a[temp+1].lflag&&a[temp].rflag)        {            lt=Min(a[temp].lr,mid-x+1);            rt=Min(a[temp+1].ll,y-mid);            tt=lt+rt;            return Max(tt,Max(FindTree(temp,x,mid),FindTree(temp+1,mid+1,y)));        }        else            return Max(FindTree(temp,x,mid),FindTree(temp+1,mid+1,y));    }    return 0;}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        int i;        CreatTree(1,1,n);        for(i=1;i<=n;i++)        {            scanf("%d",&b[i]);            if(b[i])                InsertTree(1,i,i);        }            int m;        scanf("%d",&m);        while(m--)        {            int x,y,z;            scanf("%d%d%d",&x,&y,&z);            if(x==0)                printf("%d\n",FindTree(1,y,z));            else                InsertTree(1,y,z);        }    }    return 0;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.