poj 2777 tree line delayed update

 

 

Topic links: poj 2777

 

 

Meaning of the questions: Plate coloring, the number of inquiries within the range of colors.

 

 

Solving: Since the number of colors gives the title up to 30 species, can be borrowed in binary OR operation to update the parent section. But it is written in the segment tree commonly labeled lazy ------ delayed update.

 

 

Others have written very good, direct borrowing it, blog links https://blog.csdn.net/Tong_zhi/article/details/82683219

 

The implication is that when the interval encountered, has been included in the update interval, according to conventional thinking, it is to continue to traverse leaf node, modify one by one, but this is a waste of time.

Borrowing a lazy mark, which is such a pure interval (within the range of elements need to be changed to a certain value), we only need to add a tag on him. When asked when the next update or re-modified.

 

Modify operations: first about your own tag assigned to his son, and then cancel their mark, for about his son's marks, the same when the next update, or ask before changing.

 

 

Pushdown (lazy tag):

void pushdown(int rt)
{
    if (tree[rt].lazy){
        tree[rt<<1].col=tree[rt<<1|1].col=tree[rt].lazy;
        tree[rt<<1].lazy=tree[rt<<1|1].lazy=tree[rt].lazy;
        tree[rt].lazy=0;
    }
    return ;
}

 

 

AC Code:

#include<iostream>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<stack>
#include<map> 
#include<algorithm>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Mem0(x) memset(x,0,sizeof(x))
#define Mem1(x) memset(x,-1,sizeof(x))
#define MemX(x) memset(x,0x3f,sizeof(x))
using namespace std;LongLong 
typedef ll;
const int inf=0x3f3f3f;
const double pi=acos(-1.0);

const int MAXN=1e5+10;
struct s{
    int l,r;
    ll col,lazy;
}tree[MAXN<<2];
int l,t,o;//长度,颜色数,询问次数
ll ans;
void btree(int rt,int l,int r)
{
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].lazy=0;
    tree[rt].col=1;
    if (l==r){
        return ;
    }
    int mid=(l+r)>>1;
    btree(rt<<1,l,mid);
    btree(rt<<1|1,mid+1,r);
    return ;
}
void pushdown(int rt)
{
    if (tree[rt].lazy){
        tree[rt<<1].col=tree[rt<<1|1].col=tree[rt].lazy;
        tree[rt<<1].lazy=tree[rt<<1|1].lazy=tree[rt].lazy;
        tree[rt].lazy=0;
    }
    return ;
}
void update(int rt,int a,int b,int c)
{
    if (a>tree[rt].r||b<tree[rt].l){
        return ; 
    }
    else if (a<=tree[rt].l&&tree[rt].r<=b){
        tree[rt].col=tree[rt].lazy=1<<(c-1);
        return ;
    }
    pushdown(rt);
    update(rt<<1,a,b,c);
    update(rt<<1|1,a,b,c);
    tree[rt].col=tree[rt<<1].col|tree[rt<<1|1].col;
    return ;
}
void query(int rt,int a,int b)
{
     if (a>tree[rt].r||b<tree[rt].l){
         return ;
     }
     else if (a<=tree[rt].l&&tree[rt].r<=b){
         ans|=tree[rt].col;
         return ;
     }
     pushdown(rt);
     query(rt<<1,a,b);
     query(rt<<1|1,a,b);
     return ;
}
ll find(ll a)
{
    int tmp=0;
    while (a){
        if (a%2==1)
            tmp++;
        a>>=1;
    }
    return tmp;
}
int main()
{
    scanf("%d%d%d",&l,&t,&o); 
    btree(1,1,l);
    while (o--){
        char str;
        int a,b,c;
        cin>>str;
        if (str=='C'){
            scanf("%d%d%d",&a,&b,&c);
            update(1,a,b,c);
        }
        else{
            scanf("%d%d",&a,&b);
            ans=0;
            if (a>b)
                query(1,b,a);
            else    
                query(1,a,b);
            cout<<find(ans)<<endl;
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/q1204675546/p/11221097.html