POJ 2777 Count Color ( 线段树&&位运算

本文介绍了一种基于线段树的数据结构实现方案,用于解决区间染色及查询区间内不同颜色数量的问题。通过懒标记优化更新操作,提高算法效率。

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

Count Color

Description

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.

Input

First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains “C A B C” or “P A B” (here A, B, C are integers, and A may be larger than B) as an operation defined previously.

Output

Ouput results of the output operation in order, each line contains a number.

Sample Input

2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2

Sample Output

2
1

题意

有两个操作
1 把一个区间变成一个颜色
2 查询区间中的颜色总量

AC代码

#include <cstdio>
#include <algorithm>
using namespace std;
#define LL long long
const int MAXN = 1e5+10;
struct node
{
    int l, r;
    int lazy;
    int color;
}tree[MAXN*4];
void build(int l,int r,int st)
{
    tree[st].l = l; tree[st].r = r;
    tree[st].lazy = 0; tree[st].color = 0;
    if(l == r) return ;
    int mid = (l+r) >> 1;
    build(l,mid,st<<1); build(mid+1,r,st<<1|1);
}
void update(int l, int r,int st,int val)
{
    if(tree[st].l==l && tree[st].r==r) {
        tree[st].lazy = 1;
        tree[st].color = 1<<(val-1);  //我们知道颜色的种类数一共不超过30,所以TAT
        return;
    }
    if(tree[st].lazy) {
        tree[st].lazy = 0;
        tree[st<<1].lazy = tree[st<<1|1].lazy = 1;
        tree[st<<1].color = tree[st<<1|1].color = tree[st].color;
    }
    int mid = (tree[st].l+tree[st].r) >> 1;
    if(r <= mid) update(l,r,st<<1,val);
    else if(l > mid) update(l,r,st<<1|1,val);
    else {
        update(l,mid,st<<1,val); update(mid+1,r,st<<1|1,val);
    }
    tree[st].color = tree[st<<1].color | tree[st<<1|1].color;
    if(tree[st<<1].lazy && tree[st<<1|1].lazy && tree[st<<1].color==tree[st<<1|1].color)
        tree[st].lazy = 1;
}
int query(int l,int r,int st)
{
    if(l==tree[st].l && r==tree[st].r) return tree[st].color;
    if(tree[st].lazy) return tree[st].color;     //剪下枝
    int mid = (tree[st].l+tree[st].r) >> 1;
    if(r <= mid) return query(l,r,st<<1);
    else if(l > mid) return query(l,r,st<<1|1);
    else {
        return query(l,mid,st<<1)|query(mid+1,r,st<<1|1);
    }
}
int main()
{
    int n, t, q;
    while(~scanf("%d%d%d",&n,&t,&q)) {
        build(1,n,1);
        tree[1].lazy = 1;  tree[1].color = 1;
        while(q--) {
            char ch[10];
            scanf("%s",ch);
            int x, y, z;
            if(ch[0] == 'C') {
                scanf("%d%d%d",&x,&y,&z);
                if(x > y) swap(x,y);
                update(x,y,1,z);
            }
            else {
                scanf("%d%d",&x,&y);
                if(x > y) swap(x,y);
                LL k = query(x,y,1);
                int num = 0;
                for(int i = 0; i < t; i++) {
                    if(k&(1<<i)) num++;
                }
                printf("%d\n",num);
            }
        }
    }
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值