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

Source

POJ Monthly--2006.03.26,dodo

//一道經典的線段樹題,具體在(http://www.cppblog.com/menjitianya/archive/2011/03/31/143149.html)這篇文章裏分析的很好!

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 100005
#define L(x) x<<1
#define R(x) x<<1|1

struct node
{
    int l,r,color;
    bool flag;
    int Mid() { return (l+r)>>1; }
}tree[3*maxn];
 
void Lazy(int p)
{
    if(tree[p].flag)
    {
        tree[L(p)].color=tree[p].color;
        tree[L(p)].flag=true;
        tree[R(p)].color=tree[p].color;
        tree[R(p)].flag=true;
        tree[p].flag=false;
    }
}
 
void BuildTree(int p,int l,int r)
{
    tree[p].l=l,tree[p].r=r;
    tree[p].color=1,tree[p].flag=false;
    if(l==r) 
        return;
    
    int mid=tree[p].Mid();
    BuildTree(L(p),l,mid);
    BuildTree(R(p),mid+1,r);
} 

void change(int p,int l,int r,int x)
{
    if(tree[p].l==l&&tree[p].r==r)
    {
        tree[p].color=x;
        tree[p].flag=true;
        return ;
    }
    Lazy(p);
    int mid=tree[p].Mid();
    if(r<=mid)
        change(L(p),l,r,x);
    else if(l>mid)
        change(R(p),l,r,x);
    else
    {
        change(L(p),l,mid,x);
        change(R(p),mid+1,r,x);
    }
    tree[p].color=tree[L(p)].color|tree[R(p)].color;
}

int que(int p,int l,int r)
{
    if(tree[p].flag)
        return	tree[p].color;
    if(tree[p].l==l&&tree[p].r==r)
        return tree[p].color;
    
    Lazy(p);
    int mid=tree[p].Mid();
    if(r<=mid)
        return que(L(p),l,r);
    else if(l>mid)
        return que(R(p),l,r);
    else
        return que(L(p),l,mid)|que(R(p),mid+1,r);
}
 
int make_ans(int x)
{
    int ans=0;
    while(x)
    {
        if(x%2)    ans++;
        x/=2;
    }
    return ans;
}
 
int main()
{
	int n,m,tt;
    int i,j,x,y,d;
    while(~scanf("%d%d%d",&n,&m,&tt))
    {
        char c[3];
        BuildTree(1,1,n);
        while(tt--)
        {
            scanf("%s%d%d",c,&x,&y);
            if(x>y)
                swap(x,y);
      		if(c[0]=='C')
            {
                scanf("%d",&d);
                change(1,x,y,1<<(d-1));
            }
            else 
            {
                int ans=que(1,x,y);
                printf("%d\n",make_ans(ans));
            }
        }    
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章