POJ 2777 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
题意:给定长度为n的墙,有O次操作,c a b d 代表将区间[a,b]染成颜色d,p a b 代表询问区间[a,b]有几种颜色,墙初始颜色为1,颜色种类小于等于30
很明显这是一个线段树区间修改区间查询的题目,但是问题要求询问区间内有多少种颜色,如果将区间的数依次查询记录,时间复杂度为o*nlogn,肯定会超时,
考虑到颜色的数量小于等于30,可以采用二进制的思想,线段树的每一个节点维护一个数,该数的二进制表示中1则代表有这种颜色,pushup时更新为左右两个子节点
或运算的值
AC代码:
#include 
#include 
#include 
#include 
using namespace std;
const int maxn=1e5+5;
int tree[4*maxn];
int lazy[4*maxn];
void pushup(int root)
{
    tree[root]=tree[root<<1]|tree[root<<1|1];
}
void pushdown(int root)
{
    if(lazy[root])
    {
        lazy[root<<1]=lazy[root];
        lazy[root<<1|1]=lazy[root];
        tree[root<<1]=lazy[root];
        tree[root<<1|1]=lazy[root];
        lazy[root]=0;
    }
}
void build(int root,int l,int r)
{
    if(l==r)
    {
        tree[root]=1;
        return;
    }
    int mid=l+r>>1;
    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 bit)
{
    if(ql<=l&&qr>=r)
    {
        lazy[root]=1<<(bit-1);
        tree[root]=1<<(bit-1);
        return;
    }
    pushdown(root);
    int mid=l+r>>1;
    if(mid>=ql)
    update(root<<1,l,mid,ql,qr,bit);
    if(qr>mid)
    update(root<<1|1,mid+1,r,ql,qr,bit);
    pushup(root);
}
int query(int root,int l,int r,int ql,int qr)
{
    if(l>=ql&&r<=qr)
    {
        return tree[root];
    }
    pushdown(root);
    int mid=l+r>>1;
    int ans=0;
    if(mid>=ql)
    ans|=query(root<<1,l,mid,ql,qr);
    if(qr>mid)
    ans|=query(root<<1|1,mid+1,r,ql,qr);
    return ans;
}
int main()
{
    int n,m,q;
    scanf("%d%d%d",&n,&m,&q);
    build(1,1,n);
    while(q--)
    {
        char opt;
        scanf(" %c",&opt);
        if(opt=='C')
        {
            int l,r,c;
            scanf("%d%d%d",&l,&r,&c);
            if(l>r)
            swap(l,r);
            update(1,1,n,l,r,c);
        }
        else
        {
            int l,r;
            scanf("%d%d",&l,&r);
            if(l>r)//输入的l可能大于r,特判一下
            swap(l,r);
            int temp=query(1,1,n,l,r);
            int ans=0;
            while(temp>0)
            {
                ans+=temp%2;
                temp/=2;
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

 

 

你可能感兴趣的:(POJ 2777 Count Color)