poj 2777 Count Color

Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26845   Accepted: 8023

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

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=100003;
int flag[33],color[MAXN];
typedef struct
{
    int left,right,col;
}line;
line tree[4*MAXN];
int sum;
void Create(int l,int r,int root) //建树
{
    tree[root].left=l;
    tree[root].right=r;
    tree[root].col=1;
    if(l==r) return;
    int mid=(l+r)>>1;
    Create(l,mid,root<<1);
    Create(mid+1,r,(root<<1)+1);
}
void Updata(int l,int r,int colo,int root) //更新染色板
{
     if(r<tree[root].left||l>tree[root].right) return;
    if(l<=tree[root].left&&tree[root].right<=r)
    {
        tree[root].col=colo;
        return;
    }
    if(tree[root].col==colo) return;
    if(tree[root].left==tree[root].right) return;
    if(tree[root].col>=0)  //如果这一段的颜色超过1种,则向下更新之前的颜色,并将本段颜色赋值-1
    {
        tree[root<<1].col=tree[root].col;
        tree[(root<<1)+1].col=tree[root].col;
        tree[root].col=-1;
    }
    int mid=(tree[root].left+tree[root].right)>>1;
    if(l>mid) Updata(l,r,colo,(root<<1)+1);
    else if(r<=mid) Updata(l,r,colo,root<<1);
    else
    {
        Updata(l,mid,colo,root<<1);
        Updata(mid+1,r,colo,(root<<1)+1);
    }
}
void solve(int l,int r,int root) //询问
{
  
    if(r<tree[root].left||l>tree[root].right) return;
    if(tree[root].col!=-1) //如果父节点有单一的颜色,说明子节点也有单一颜色,故直接更新
    {
     //printf("root = %d tree[root].col = %d\n",root,tree[root].col);
        flag[tree[root].col]=1;//统计哪些颜色出现过
        return;
    }
    if(tree[root].left==tree[root].right) return;
   
    int mid=(tree[root].left+tree[root].right)>>1;
    if(l>mid) solve(l,r,(root<<1)+1);
    else if(r<=mid) solve(l,r,root<<1);
    else
    {
        solve(l,mid,root<<1);
        solve(mid+1,r,(root<<1)+1);
    }
}

int main()
{
    //freopen("input","r",stdin);
    int n,t,o;
    while(scanf("%d %d %d",&n,&t,&o)!=EOF)
    {

        memset(color,0,sizeof(color));
        Create(1,n,1);
        int tl,tr,tc;
        char str[2];
        for(int i=0;i<o;i++)
        {
            scanf("%s",str);
            if(str[0]=='C')
            {
                scanf("%d %d %d",&tl,&tr,&tc);
                if(tl>tr)
                {
                    int temp=tl;
                    tl=tr;
                    tr=temp;
                }
                Updata(tl,tr,tc,1);
            }
            else if(str[0]=='P')
            {
                memset(flag,0,sizeof(flag));
                scanf("%d %d",&tl,&tr);
                if(tl>tr)
                {
                    int temp=tl;
                    tl=tr;
                    tr=temp;
                }
                sum=0;
                solve(tl,tr,1);
                for(int i=1;i<=t;i++)
                if(flag[i]) sum++;
                printf("%d\n",sum);
            }
        }
    }

    return 0;
}

链接:http://poj.org/problem?id=2777

posted @ 2012-07-26 20:10  jiai  Views(142)  Comments(0Edit  收藏  举报