poj 2777 Count Color

http://poj.org/problem?id=2777


区间更新线段树。
思路:颜色总数只有30种,所以可以用一个整数的二进制形式来表示所有颜色,如第i位为1表示该区间内有颜色i。这里为了方便,第0位不计。父亲节点的值就等于两个子节点或的结果。
吐槽:忍不住骂自己一声。。刚开始看题时看到了a可能比b大,可代码写到最后就忘了。。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#define fi first
#define se second
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
//head
const int maxn = 1e5+5;
int t[maxn<<2];
int lazy[maxn<<2];

void pushUp(int rt)
{
    t[rt] = t[rt<<1] | t[rt<<1|1];
}

void pushDown(int rt)
{
    if(lazy[rt])
    {
        lazy[rt<<1] = lazy[rt<<1|1] = lazy[rt];
        t[rt<<1] = t[rt<<1|1] = lazy[rt];
        lazy[rt] = 0;
    }
}

void build(int l, int r, int rt)
{
    lazy[rt] = 0;
    t[rt] = 2;
    if(l==r)
        return;
    int m = (l+r)>>1;
    build(lson);
    build(rson);
}

void update(int L, int R, int c, int l, int r, int rt)
{
    if(L<=l && r<=R)
    {
        lazy[rt] = t[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 t[rt];
    pushDown(rt);
    int m = (l+r)>>1;
    int ret = 0;
    if(L<=m)
        ret |= query(L, R, lson);
    if(R>m)
        ret |= query(L, R, rson);
    return ret;
}

int main()
{
    int L,T,O;
    while(scanf("%d%d%d",&L,&T,&O)==3)
    {
        build(1, L, 1);
        while(O--)
        {
            char s[2];
            int a,b,c;
            scanf("%s",s);
            if(s[0]=='C')
            {
                scanf("%d%d%d",&a,&b,&c);
                if(a>b)swap(a,b);
                update(a, b, 1<<c, 1, L, 1);
            }
            else
            {
                scanf("%d%d",&a,&b);
                if(a>b)swap(a,b);
                int tmp = query(a, b, 1, L, 1);
                int ans = 0;
                while(tmp!=0)
                {
                    ans += tmp&1;
                    tmp /= 2;
                }
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值