POJ 2777 Count Color 线段树

/**
 * @file main.cpp
 * @brief 线段树,注意几点:
 *        1.输入数据中有可能有a>b的情况,需要做特殊处理
 *        2.cin会超时,使用scanf
 *        3.线段树染色/插入的时候注意
 *            如果待插入区间等于线段树区间,直接插入
 *            递归处理子区间之前,需要把父亲节点的染色(如果有的话)传递给左右两个子节点,同时清除父亲节点的染色
 *        4.使用位运算会使得代码更清爽
 *
 * @author yekeren
 * @version 1.0.0
 * @date 2013-06-09
 */
#include <stdio.h>
#include <string.h>

#define LEFT(x) ((x) << 1)
#define RIGHT(x) (((x) << 1) + 1)

struct node_t {
    int left, right;
    unsigned colors;
} k[410000];

/**
 * @brief 构建线段树 
 * @param root
 * @param left
 * @param right
 */
void build(int root, int left, int right)
{
    k[root].left  = left;
    k[root].right = right;

    if (left >= right) {
        return;
    }

    int mid = (left + right) >> 1;
    build(LEFT(root), left, mid);
    build(RIGHT(root), mid + 1, right);
}

/**
 * @brief 线段树染色 
 * @param root
 * @param a
 * @param b
 * @param c
 */
void insert(int root, int a, int b, int c)
{
    if (a <= k[root].left && b >= k[root].right)
    {
        k[root].colors = 1 << (c - 1);
        return;
    }

    if (k[root].colors)
    {
        k[LEFT(root)].colors  = k[root].colors;
        k[RIGHT(root)].colors = k[root].colors;
        k[root].colors = 0;
    }

    int mid = (k[root].left + k[root].right) >> 1;
    if (b <= mid) {
        insert(LEFT(root), a, b, c);
    } else if (a > mid) {
        insert(RIGHT(root), a, b, c);
    } else {
        insert(LEFT(root), a, mid, c);
        insert(RIGHT(root), mid + 1, b, c);
    }
}

/**
 * @brief 查询染色情况 
 * @param root
 * @param a
 * @param b
 * @return   
 */
unsigned search(int root, int a, int b)
{
    if (k[root].colors) {
        return k[root].colors;
    }

    int mid = (k[root].left + k[root].right) >> 1;

    if (b <= mid) {
        return search(LEFT(root), a, b);
    } else if (a > mid) {
        return search(RIGHT(root), a, b);
    } else {
        unsigned int c1 = search(LEFT(root), a, mid);
        unsigned int c2 = search(RIGHT(root), mid + 1, b);
        return c1 | c2;
    }
}

/**
 * @brief 交换 
 * @param a
 * @param b
 */
void swap(int &a, int &b)
{
    int tmp = a;
    a = b;
    b = tmp;
}

int main(int argc, char *argv[])
{
    int l, t, o;
    scanf("%d%d%d", &l, &t, &o);

    build(1, 1, l);
    insert(1, 1, l, 1);

    while (o--)
    {
        char op[2];
        scanf("%s", op);

        if (strcmp(op, "C") == 0)
        {
            int a, b, c;
            scanf("%d%d%d", &a, &b, &c);

            if (a > b) {
                swap(a, b);
            }
            insert(1, a, b, c);
        } 
        else if (strcmp(op, "P") == 0)
        {
            int a, b;
            scanf("%d%d", &a, &b);

            if (a > b) {
                swap(a, b);
            }
            unsigned colors = search(1, a, b);
            int count = 0;
            while (colors) 
            {
                if (colors & 1) {
                    ++count;
                }
                colors = colors >> 1;
            }

            printf("%d\n", count);
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值