POJ 2777

線段樹+延遲標記


題意:長爲Lcm的一塊板,可以看成長度爲1cm的m段,2種操作

(C,a,b,c)    把區間[a,b]塗成顏色c  ;

(P,a,b)    求區間[a,b]顏色數目 

1<=c<=T<=30;

所以可以用1個整數來表示1個點各種顏色的狀態

*輸入的區間右端點可能大於左端點 



#include <iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define maxn 100005
using namespace std;
int color[maxn<<2];
bool cover[maxn<<2];
void pushup(int rt){
    color[rt]=color[rt<<1]|color[rt<<1|1];
}
void pushdown(int rt){
    if(!cover[rt]){
        cover[rt<<1]=false;
        cover[rt<<1|1]=false;
        color[rt<<1]=color[rt];
        color[rt<<1|1]=color[rt];
        cover[rt]=true;
    }
}
void build(int l,int r,int rt ){
    cover[rt]=1;
    color[rt]=1;
    if(l==r)
    return;
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int L,int R,int c,int l,int r,int rt){
    if(L<=l&&r<=R){
        cover[rt]=0;
        color[rt]=c;
        return ;
    }
    pushdown(rt);
    int m=(l+r)>>1;
    if(L<=m){
        update(L,R,c,lson);
    }
    if(R>m){
        update(L,R,c,rson);
    }
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
   if(L<=l&&r<=R){
        return color[rt];
   }
   pushdown(rt);
   int m=(l+r)>>1;
   int ret=0;
   if(L<=m){
        ret=ret|query(L,R,lson);
   }
   if(R>m){
        ret=ret|query(L,R,rson);
   }
   return ret;
}
int main()
{
    int L,T,O,x,y,z;
    char op[2];
    while(~scanf("%d%d%d",&L,&T,&O)){
        build(1,L,1);
        while(O--){
            scanf("%s",op);
            scanf("%d%d",&x,&y);
            if(op[0]=='C'){
                scanf("%d",&z);
                if(x>y)
                swap(x,y);
                update(x,y,1<<(z-1),1,L,1);
            }else {
                if(x>y)
                swap(x,y);
                int tmp=query(x,y,1,L,1);
                int ans=0;
                for(int i=0;i<T;i++){
                    if(tmp&(1<<i))
                    ans++;
                }
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}


發佈了104 篇原創文章 · 獲贊 3 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章