poj 2777 Count Color

题目链接如下所示:

2777 -- Count Color

题目大意是一个线段,给线段染色,统计一个线段内有多少种颜色。

这里的颜色很少,甚至最大才30,可以用int的每一位代表颜色来维护线段内的颜色种类。

代码如下所示:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<vector>
#include<algorithm>
#include<cstring>
#include<set>
#include<map>
#include<queue>
#include<string>
#include<cmath>
#include<climits>

using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int MAXN=100001;
int color[MAXN<<2];
int change[MAXN<<2];
int kind,ans;

void push_up(int rt){
    color[rt]=color[rt<<1]|color[rt<<1|1];
}

void push_down(int rt){
    if (change[rt]){
        change[rt<<1]=change[rt];
        change[rt<<1|1]=change[rt];
        color[rt<<1]=change[rt];
        color[rt<<1|1]=change[rt];
        change[rt]=0;
    }
}

void build(int l,int r,int rt){
    change[rt]=0;
    if (l==r){
        color[rt]=1;
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(rt);
}

void update(int a,int b,int c,int l,int r,int rt){
    if (a<=l && b>=r){
        change[rt]=c;
        color[rt]=c;
        return;
    }
    push_down(rt);
    int mid=(l+r)>>1;
    if (a<=mid){
        update(a,b,c,lson);
    }
    if (b>mid){
        update(a,b,c,rson);
    }
    push_up(rt);
}

void query(int a,int b,int l,int r,int rt){
    if (a<=l && b>=r){
        kind|=color[rt];
        return;
    }
    push_down(rt);
    int mid=(l+r)>>1;
    if (a<=mid){
        query(a,b,lson);
    }
    if (b>mid){
        query(a,b,rson);
    }
    // update的时候该push_up的节点都push_up过了 这里只需要update节点的颜色
//    push_up(rt);
}

int main(){
    int L,T,O;
    int a,b,c;
    char op[2];
    while (~scanf("%d %d %d",&L,&T,&O)){
        build(1,L,1);
        while (O--){
            scanf("%s",op);
            if (op[0]=='C'){
                scanf("%d %d %d",&a,&b,&c);
                if (a>b) swap(a,b);
//                cout<<op<<","<<a<<","<<b<<","<<c<<endl;
                c=1<<(c-1);
                update(a,b,c,1,L,1);
            } else{
                scanf("%d %d",&a,&b);
                if (a>b) swap(a,b);
//                cout<<op<<","<<a<<","<<b<<endl;
                kind=0;
                ans=0;
                query(a,b,1,L,1);
                for (int i = 0; i < T; ++i) {
                    if (kind & (1<<i)){
                        ans++;
                    }
                }
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值