poj 2777 Count Color(线段树 区间更新)

本文介绍了一种利用位运算解决区间染色问题的方法,包括数据结构的设计、区间更新和查询的操作实现,通过实例详细解析了算法思路。

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

Language:
Count Color
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 40057 Accepted: 12086

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  有两种操作一种是把一个区间内的气球全都换成一种颜色 一种是求一个区间内的气球有多少种颜色的气球

难点在于如果表示一个区间内的气球颜色个数

在这里 因为总的气球颜色较少 可以用int整数来表示区间内气球染色的情况 换成2进制 如果改为为1 则这位上有这个颜色的气球 添加一种颜色的时候 使用位运算| 

最后就可以求得这个区间内气球染色的情况 判断这个整数中共有几个1 就有几个颜色了

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

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 100010
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

int Read()
{
    char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    int x = 0;
    while (c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x;
}

void Print(int a)
{
     if(a>9)
         Print(a/10);
     putchar(a%10+'0');
}

struct node
{
    int l,r;
    int col,same;
};
node no[MAXN*4];

void build(int l,int r,int k)
{
    no[k].l=l;
    no[k].r=r;
    no[k].col=1;
    no[k].same=1;//用来标记这个区间里面的颜色是不是一样的
    if(l==r)
        return;
    int mid=(l+r)/2;
    build(l,mid,k*2);
    build(mid+1,r,k*2+1);
}

void pushdown(int k)
{
    if(no[k].same)
    {
        no[k*2].same=no[k*2+1].same=1;
        no[k*2].col=no[k*2+1].col=no[k].col;
        no[k].same=0;
        return;
    }
}

void update(int l,int r,int k,int col)
{
    if(l<=no[k].l&&no[k].r<=r)
    {
        no[k].col=col;
        no[k].same=1;
        return;
    }
    pushdown(k);
    int mid=(no[k].l+no[k].r)/2;
    if(r<=mid) update(l,r,k*2,col);
    else if(l>mid) update(l,r,k*2+1,col);
    else
    {
        update(l,mid,k*2,col);
        update(mid+1,r,k*2+1,col);
    }
    no[k].col=no[2*k].col|no[k*2+1].col;
}
int ans;
void query(int l,int r,int k)
{
    if(no[k].l>=l&&no[k].r<=r)
    {
        ans|=no[k].col;
        return;
    }
    pushdown(k);
    int mid=(no[k].l+no[k].r)/2;
    if(r<=mid) query(l,r,k*2);
    else if(l>mid) query(l,r,k*2+1);
    else
    {
        query(l,mid,k*2);
        query(mid+1,r,k*2+1);
    }
}

int getcnt()
{
    int cnt=0;
    while(ans)
    {
        if(ans&1)
            cnt++;
        ans>>=1;
    }
    return cnt;
}

int main()
{
//    fread;
    int n,t,q;
    while(scanf("%d%d%d",&n,&t,&q)!=EOF)
    {
        build(1,n,1);
        while(q--)
        {
            int l,r,num;
            char ch[10];
            scanf("%s",ch);
            if(ch[0]=='C')
            {
                scanf("%d%d%d",&l,&r,&num);
                if(l>r) swap(l,r);
                update(l,r,1,1<<(num-1));
            }
            else
            {
                scanf("%d%d",&l,&r);
                ans=0;
                if(l>r) swap(l,r);
                query(l,r,1);
                printf("%d\n",getcnt());
            }
        }
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值