poj 2777 Count Color 线段树成段更新区间统计

Count Color
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 31008 Accepted: 9291

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

Source


题意:

有一个长板子,多次操作,有两种操作,第一种是给从a到b那段染一种颜色c,另一种是询问a到b有多少种不同的颜色。

做了这题后,体会到了线段树的强大功能。线段树远比我想象的要强大。

思路:

由于颜色最多30种,所以用二进制表示。二进制第几位为1表示第几种颜色存在。通过线段树维护颜色信息。由于最后询问只关心颜色数目。所以通过按位与求和。然后统计其中一的个数就知道颜色的个数了。

详细见代码:

#include <iostream>
#include<string.h>
#include<stdio.h>
using namespace std;

const int maxn=100100;
int L,T,O;
struct node
{
    int l,r;
    bool same;//记录区间颜色是否相同
    int color;
} smt[3*maxn];
void btree(int l,int r,int k)
{
    int ls,rs,mid;
    smt[k].l=l;
    smt[k].r=r;
    smt[k].same=true;
    smt[k].color=1;
    if(smt[k].l==smt[k].r)
        return;
    ls=k<<1;
    rs=k<<1|1;
    mid=(smt[k].l+smt[k].r)>>1;
    btree(l,mid,ls);
    btree(mid+1,r,rs);
}
void update(int l,int r,int k,int c)
{
    int ls,rs,mid;
    if(smt[k].l==l&&smt[k].r==r)
    {
        smt[k].same=true;
        smt[k].color=c;
        return;
    }
    ls=k<<1;
    rs=k<<1|1;
    mid=(smt[k].l+smt[k].r)>>1;
    if(smt[k].same)
    {
        smt[ls].same=smt[rs].same=true;
        smt[k].same=false;
        smt[ls].color=smt[rs].color=smt[k].color;
    }
    if(l>mid)
       update(l,r,rs,c);
    else if(r<=mid)
       update(l,r,ls,c);
    else
    {
        update(l,mid,ls,c);
        update(mid+1,r,rs,c);
    }
    smt[k].color=smt[ls].color|smt[rs].color;//二进制压缩颜色
}
int qu(int l,int r,int k)
{
    int ls,rs,mid;
    if((smt[k].l==l&&smt[k].r==r)||smt[k].same)
        return smt[k].color;
    ls=k<<1;
    rs=k<<1|1;
    mid=(smt[k].l+smt[k].r)>>1;
    if(l>mid)
        return qu(l,r,rs);
    else if(r<=mid)
        return qu(l,r,ls);
    else
        return qu(l,mid,ls)|qu(mid+1,r,rs);
}
int countc(int cor)
{
    int cnt=0;
    while(cor)
    {
        if(cor&1)
            cnt++;
        cor>>=1;
    }
    return cnt;
}
int main()
{
    char com[10];
    int a,b,c;

    while(~scanf("%d%d%d",&L,&T,&O))
    {
        gets(com);
        btree(1,L,1);
        while(O--)
        {
            scanf("%s",com);
            if(com[0]=='C')
            {
                scanf("%d%d%d",&a,&b,&c);
                update(a,b,1,1<<(c-1));
            }
            else
            {
                scanf("%d%d",&a,&b);
                printf("%d\n",countc(qu(a,b,1)));
            }
        }
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值