POJ-2777-CountColor(线段树,位运算)

本文介绍了一种利用线段树数据结构解决颜色区间查询和更新的问题,通过位运算实现颜色数量的快速计算。文章详细解释了线段树的构建、更新和查询操作,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:https://vjudge.net/problem/POJ-2777#author=0

题意:

Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. 

There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board: 

1. "C A B C" Color the board from segment A to segment B with color C. 
2. "P A B" Output the number of different colors painted between segment A and segment B (including). 

In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your. 

思路:

线段树,还是普通的线段树,染色的查询和更新使用位运算,因为颜色区间在(1-30)之内。

所以可以使用(1<<1-1<<30)来表示这中二进制1的个数来表示颜色的数量。

不过我之前的写的普通的线段树我也不知道为啥会WA。

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <memory.h>
#include <algorithm>
#include <string>
#include <stack>
#include <vector>
#include <queue>

using namespace std;
typedef long long LL;
const int MAXN = 1e5+10;

int Seg[MAXN*4];
int lazy[MAXN*4];
int Vis[100];
int n, t, o;
int res;

void PushDown(int root)
{
    if (lazy[root] != 0)
    {
        Seg[root<<1] = (1<<lazy[root]);
        Seg[root<<1|1] = (1<<lazy[root]);

        lazy[root<<1] = lazy[root];
        lazy[root<<1|1] = lazy[root];
        lazy[root] = 0;
    }
}

void PushUp(int root)
{
    Seg[root] = Seg[root<<1]|Seg[root<<1|1];
}

void Build(int root, int l, int r)
{
    if (l == r)
    {
        Seg[root] = 2;
        return;
    }
    int mid = (l + r) / 2;
    Build(root << 1, l, mid);
    Build(root << 1 | 1, mid + 1, r);
    PushUp(root);
}

void Update(int root, int l, int r, int ql, int qr, int c)
{
    if (r < ql || qr < l)
        return;
    if (ql <= l && r <= qr)
    {
        Seg[root] = (1<<c);
        lazy[root] = c;
        return;
    }
    PushDown(root);
    int mid = (l+r)/2;
    Update(root<<1, l, mid, ql, qr, c);
    Update(root<<1|1, mid+1, r, ql, qr, c);
    PushUp(root);
}

int Query(int root, int l, int r, int ql, int qr)
{
    if (r < ql || qr < l)
        return 0;
    if (ql <= l && r <= qr)
    {
        return Seg[root];
    }
    int mid = (l+r)/2;
    PushDown(root);
    int col1 = 0, col2 = 0;
    col1 = Query(root<<1, l, mid, ql, qr);
    col2 = Query(root<<1|1, mid+1, r, ql, qr);
    return col1|col2;
}

int Get(int x)
{
    int res = 0;
    while (x)
    {
        if (x&1)
            res++;
        x >>= 1;
    }
    return res;
}

int main()
{
    char op[10];
    int a, b, c;
    while (~scanf("%d%d%d", &n, &t, &o))
    {
        Build(1, 1, n);
        while (o--)
        {
            scanf("%s", op);
            if (op[0] == 'C')
            {
                scanf("%d%d%d", &a, &b, &c);
                if (a > b)
                    swap(a, b);
                Update(1, 1, n, a, b, c);
            }
            else
            {
                scanf("%d%d", &a, &b);
                if (a > b)
                    swap(a, b);
                memset(Vis, 0, sizeof(Vis));
                int res = Query(1, 1, n, a, b);
                printf("%d\n", Get(res));
            }
        }
    }

    return 0;
}

  

转载于:https://www.cnblogs.com/YDDDD/p/10847767.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值