poj2777 Count Color

Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26254   Accepted: 7833

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



有修改的线段树,颜色需要位运算。

代码很恶心,不看也罢。

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

typedef struct
{
    int l,r,lazy,vis;
}Tree;

char str[5];
Tree tree[500000];
int num;
int ans;

void Build(int t,int l,int r)
{
    int mid;
    tree[t].lazy=0;
    tree[t].l=l;
    tree[t].r=r;
    if (l==r) return;
    mid=(l+r)/2;
    Build(2*t+1,l,mid);
    Build(2*t+2,mid+1,r);
}

void Color(int t,int x,int y,int c)
{
    int l,r,mid,i,j,k;
    l=tree[t].l;
    r=tree[t].r;
    mid=(l+r)/2;
    if (l==x && r==y)
    {
        tree[t].vis=c;
        tree[t].lazy=1;
        return;
    }
    if (tree[t].lazy==1)
    {
        tree[t].lazy=0;
        Color(2*t+1,l,mid,tree[t].vis);
        Color(2*t+2,mid+1,r,tree[t].vis);
    }
    if (x<=mid)
    {
        Color(2*t+1,x,min(mid,y),c);
    }
    if (y>=mid+1)
    {
        Color(2*t+2,max(x,mid+1),y,c);
    }
    i=2*t+1;
    j=2*t+2;
    tree[t].vis=(tree[i].vis | tree[j].vis);
}

void Find(int t,int x,int y)
{
    int l,r,mid,i;
    l=tree[t].l;
    r=tree[t].r;
    if (l==x && r==y)
    {
        ans=(ans | tree[t].vis);
        return;
    }
    mid=(l+r)/2;
    if (tree[t].lazy==1)
    {
        tree[t].lazy=0;
        Color(2*t+1,l,mid,tree[t].vis);
        Color(2*t+2,mid+1,r,tree[t].vis);
    }
    if (x<=mid)
    {
        Find(2*t+1,x,min(y,mid));
    }
    if (y>=mid+1)
    {
        Find(2*t+2,max(x,mid+1),y);
    }
}

int main()
{
    int i,j,n,q,x,y,z,cnt;
    while(scanf("%d%d%d",&n,&num,&q)!=EOF)
    {
        Build(0,0,n-1);
        Color(0,0,n-1,1);
        while(q--)
        {
            scanf("%s",str);
            scanf("%d%d",&x,&y);
            if (x>y)
            {
                swap(x,y);
            }
            if (str[0]=='C')
            {
                scanf("%d",&z);
                Color(0,x-1,y-1,1<<(z-1));
            }
            else
            {
                ans=0;
                Find(0,x-1,y-1);
                cnt=0;
                for (i=0;i<num;i++)
                {
                    if ((ans & (1<<i))!=0) cnt++;
                }
                printf("%d\n",cnt);
            }
        }
    }
    return 0;
}










你可能感兴趣的:(tree,express,Build,each,output,colors)