HDU 3397 line segment tree double lazy flag

Source: Internet
Author: User

This is a historical problem left over from last year. The previous ideas are chaotic. If we had a lot of posts, we would not have done it.

Since I made the double laziness mark in the big white book last time, the idea of doing this is very clear.

Similar to the one on the big white paper, this also has a set mark, which indicates that this range is set to 0 or 1, and-1 is set when no position is set.

There is also a rev mark, which indicates the flip operation. 0 indicates that the current flip is not performed, and 1 indicates that the current flip is performed.

Pay attention to the priority. I found that there are different ways to solve this problem. When there is a set operation, set the flag to set the value and set the rev flag of the current node to 0, because the current set operation must directly overwrite regardless of whether or not to use rev.

The Rev operation does not change the set operation. When Pushdown, you should first consider the set flag and then get the rev flag. This is also easy to understand, because once the Rev and set coexist, the rev must be behind the set.

There are several details to note. At first, the cloud was flowing in one breath, and we found that we were still wa, which is just a few places,

1. in addition to forcing rev to be set to 0, the rev flag operation is always ^ 1 at any time. This is also easy to understand. In Pushdown, I directly passed the value, definitely not.

2. in the Pushdown operation, remember to erase the rev mark of the subtree. It was written only in the primary modification function at the beginning. It was not written here, and Wa was unclear for the same reason as above.

Then there's basically no problem.

#include <iostream>#include <cstdio>#include <cstring>#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,rusing namespace std;const int N=100000+10;int d[N*3],maxc[N*3],lc[N*3],rc[N*3],maxc0[N*3],lc0[N*3],rc0[N*3];int sets[N*3],rev[N*3];int n,Q;int A[N];void up(int rt,int l,int r){    int mid=(l+r)>>1;    d[rt]=d[rt<<1]+d[rt<<1|1];    maxc[rt]=max(maxc[rt<<1],maxc[rt<<1|1]);    maxc[rt]=max(maxc[rt],lc[rt<<1|1]+rc[rt<<1]);    lc[rt]=lc[rt<<1];    rc[rt]=rc[rt<<1|1];    if (lc[rt<<1]==mid-l+1) lc[rt]+=lc[rt<<1|1];    if (rc[rt<<1|1]==r-mid) rc[rt]+=rc[rt<<1];    maxc0[rt]=max(maxc0[rt<<1],maxc0[rt<<1|1]);    maxc0[rt]=max(maxc0[rt],lc0[rt<<1|1]+rc0[rt<<1]);    lc0[rt]=lc0[rt<<1];    rc0[rt]=rc0[rt<<1|1];    if (lc0[rt<<1]==mid-l+1) lc0[rt]+=lc0[rt<<1|1];    if (rc0[rt<<1|1]==r-mid) rc0[rt]+=rc0[rt<<1];}void build(int rt,int l,int r){    sets[rt]=-1;    rev[rt]=0;    if (l>=r){        d[rt]=maxc[rt]=A[l];        lc[rt]=rc[rt]=A[l];        lc0[rt]=rc0[rt]=maxc0[rt]=1-A[l];        return;    }    int mid=(l+r)>>1;    build(lson);    build(rson);    up(rt,l,r);}void pushdown(int rt,int l,int r){    if (l>=r) return;    int mid=(l+r)>>1;    if (sets[rt]>=0){        maxc[rt<<1]=lc[rt<<1]=rc[rt<<1]=d[rt<<1]=(mid-l+1)*sets[rt];        maxc[rt<<1|1]=lc[rt<<1|1]=rc[rt<<1|1]=d[rt<<1|1]=(r-mid)*sets[rt];        maxc0[rt<<1]=lc0[rt<<1]=rc0[rt<<1]=(mid-l+1)*(1-sets[rt]);        maxc0[rt<<1|1]=lc0[rt<<1|1]=rc0[rt<<1|1]=(r-mid)*(1-sets[rt]);        sets[rt<<1]=sets[rt<<1|1]=sets[rt];        rev[rt<<1]=rev[rt<<1|1]=0;        sets[rt]=-1;    }    if (rev[rt]>0){        d[rt<<1]=(mid-l+1)-d[rt<<1];        d[rt<<1|1]=(r-mid)-d[rt<<1|1];        int t1,t2,t3;        t1=maxc[rt<<1];t2=lc[rt<<1];t3=rc[rt<<1];        maxc[rt<<1]=maxc0[rt<<1];        lc[rt<<1]=lc0[rt<<1];        rc[rt<<1]=rc0[rt<<1];        maxc0[rt<<1]=t1;        lc0[rt<<1]=t2;        rc0[rt<<1]=t3;        t1=maxc[rt<<1|1];t2=lc[rt<<1|1];t3=rc[rt<<1|1];        maxc[rt<<1|1]=maxc0[rt<<1|1];        lc[rt<<1|1]=lc0[rt<<1|1];        rc[rt<<1|1]=rc0[rt<<1|1];        maxc0[rt<<1|1]=t1;        lc0[rt<<1|1]=t2;        rc0[rt<<1|1]=t3;        rev[rt<<1]^=1;        rev[rt<<1|1]^=1;        rev[rt]=0;    }}void change(int val,int L,int R,int rt,int l,int r){    if (L<=l && r<=R){        d[rt]=(r-l+1)*val;        lc[rt]=rc[rt]=(r-l+1)*val;        maxc[rt]=(r-l+1)*val;        lc0[rt]=rc0[rt]=maxc0[rt]=(r-l+1)*(1-val);        sets[rt]=val;        rev[rt]=0;        return;    }    pushdown(rt,l,r);    int mid=(l+r)>>1;    if (L>mid) change(val,L,R,rson);    else    if (R<=mid) change(val,L,R,lson);    else{        change(val,L,R,rson);        change(val,L,R,lson);    }    up(rt,l,r);}void revers(int L,int R,int rt,int l,int r){    if (L<=l && r<=R)    {        d[rt]=(r-l+1)-d[rt];        int t1,t2,t3;        t1=maxc[rt];t2=lc[rt];t3=rc[rt];        maxc[rt]=maxc0[rt];lc[rt]=lc0[rt];rc[rt]=rc0[rt];        maxc0[rt]=t1;lc0[rt]=t2;rc0[rt]=t3;        rev[rt]^=1;        return;    }    pushdown(rt,l,r);    int mid=(l+r)>>1;    if (R<=mid) revers(L,R,lson);    else    if (L>mid) revers(L,R,rson);    else    {        revers(L,R,lson);        revers(L,R,rson);    }    up(rt,l,r);}int output(int op,int L,int R,int rt,int l,int r){    if (L==l && r==R){        if (op==3) return d[rt];        else  return maxc[rt];    }    pushdown(rt,l,r);    int mid=(l+r)>>1;    if (L>mid) return output(op,L,R,rson);    else    if (R<=mid) return output(op,L,R,lson);    else    {        int ret1=output(op,L,mid,lson);        int ret2=output(op,mid+1,R,rson);        if (op==3) return ret1+ret2;        else{            int ret=max(ret1,ret2);            int t1=min(rc[rt<<1],mid-L+1);            int t2=min(lc[rt<<1|1],R-mid);            ret=max(ret,t1+t2);            return ret;        }    }}int main(){    int t,op,a,b;    scanf("%d",&t);    while (t--)    {        scanf("%d%d",&n,&Q);        for (int i=1;i<=n;i++) scanf("%d",&A[i]);        build(1,1,n);        while (Q--)        {            scanf("%d%d%d",&op,&a,&b);            a++;b++;            if (op<=1){                change(op,a,b,1,1,n);            }            else            if (op==2){                revers(a,b,1,1,n);            }            else            {                int ans=output(op,a,b,1,1,n);                printf("%d\n",ans);            }        }    }    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.