poj 2777 Count Color

和hdu5023一样不过询问不同。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define up rt,rt<<1,rt<<1|1
using namespace std;
const int M = 1e6+7;
int n,q;
int c[M<<2],ans[20000],cnt;
void pushdown(int rt,int l,int r){
    c[l]=c[r]=c[rt];c[rt]=0;
}
void build(int l,int r,int rt){
    c[rt]=0;
    if(l==r) return ;
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
}
void update(int L,int R,int l,int r,int rt,int v){
    if(L==l&&r==R){
        c[rt]=v;//这个区间是整个被染色
        return ;
    }
    if(c[rt]) pushdown(up);//当这个区间不是整个会被染色那么将颜色下放
    int mid=(l+r)>>1;
    if(L<=mid) update(L,min(mid,R),lson,v);
    if(R>mid) update(max(L,mid+1),R,rson,v);
}
void query(int L,int R,int l,int r,int rt){
    //cout<<rt<<endl;
    if(l<=L&&R<=r&&c[rt]){//查询区间整个被这个区间覆盖且这个区间有颜色就返回颜色
        ans[++cnt]=c[rt];
        return ;
    }
    int mid=(l+r)>>1;
    if(L<=mid) query(L,min(mid,R),lson);
    if(R>mid) query(max(mid+1,L),R,rson);
}
int main(){
    int _;
    while(~scanf("%d%d%d",&n,&_,&q)){
        build(1,n,1);
        c[1]=1;cnt=0;memset(ans,0,sizeof(ans));
        while(q--){
            char s[10];int x,y,v;
            scanf("%s",s);
            if(s[0]=='C'){
                scanf("%d%d%d",&x,&y,&v);if(x>y) swap(x,y);
                update(x,y,1,n,1,v);
            }
            else{
                cnt=0;memset(ans,0,sizeof(ans));
                scanf("%d%d",&x,&y);if(x>y) swap(x,y);
                query(x,y,1,n,1);
                sort(ans+1,ans+cnt+1);
                int sz=unique(ans+1,ans+cnt+1)-ans-1;//去重,因为可能找到很多重复染色区间
                printf("%d\n",sz);
            }
        }
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/LMissher/p/9574803.html
今日推荐