poj 2777 Count Color(线段树)


Count Color
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 43857 Accepted: 13274

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

提示

题意:

你有L(1<=L<=100000)厘米长的木板,一开始这个木板都涂上了颜色1,对于这个木板我们有o(1<=o<=100000)组操作,操作方式如下:

1.C a b c表示木板上第a厘米到第b厘米涂上颜色c。

2.P a b询问木板上第a厘米到第b厘米有几种不同的颜色。

一共有t(1<=t<=30)种颜色,你能完成这个任务吗?

思路:

线段树的基本应用,具体看注释。

示例程序

Source Code

Problem: 2777		Code Length: 2162B
Memory: 3456K		Time: 500MS
Language: GCC		Result: Accepted
#include <stdio.h>
#include <string.h>
struct
{
    int l,r,val;
}line[400000];
int v[30];
void create(int l,int i,int j)
{
    line[l-1].l=i;
    line[l-1].r=j;
    line[l-1].val=1;			//全部涂为颜色1,且表示这一区间颜色数唯一
    if(i==j)
    {
        return;
    }
    else
    {
        create(2*l,i,(i+j)/2);
        create(2*l+1,(i+j)/2+1,j);
    }
}
void update(int a,int b,int c,int pos)
{
    if(line[pos-1].l==a&&line[pos-1].r==b)
    {
        line[pos-1].val=c;				//这个区间涂为颜色c
        return;
    }
    if(line[pos-1].val==c)				//若已为颜色c,返回
    {
        return;
    }
    if(line[pos-1].val!=-1)				//若这块区域用多种颜色,向下接着划分,并标记这块区域有多种颜色
    {
        line[2*pos-1].val=line[pos-1].val;
        line[2*pos].val=line[pos-1].val;
        line[pos-1].val=-1;
    }
    if(a>(line[pos-1].r+line[pos-1].l)/2)		//若这块区域用多种颜色,向下接着划分
    {
        update(a,b,c,pos*2+1);
    }
    else if(b<=(line[pos-1].r+line[pos-1].l)/2)
    {
        update(a,b,c,pos*2);
    }
    else
    {
        update(a,(line[pos-1].r+line[pos-1].l)/2,c,pos*2);
        update((line[pos-1].r+line[pos-1].l)/2+1,b,c,pos*2+1);
    }
}
void find(int a,int b,int pos)
{
    if(line[pos-1].val!=-1)				//若这块区域只有一种颜色,则记录颜色返回
    {
        v[line[pos-1].val-1]=1;
        return;
    }
    else						//否则接着向下搜索
    {
        if(a>(line[pos-1].r+line[pos-1].l)/2)
        {
            find(a,b,pos*2+1);
        }
        else if(b<=(line[pos-1].r+line[pos-1].l)/2)
        {
            find(a,b,pos*2);
        }
        else
        {
            find(a,(line[pos-1].r+line[pos-1].l)/2,pos*2);
            find((line[pos-1].r+line[pos-1].l)/2+1,b,pos*2+1);
        }
    }
}
int main()
{
    int l,t,o,i,a,b,c,d,i1,count;
    char s[2];
    scanf("%d %d %d",&l,&t,&o);
    create(1,1,l);
    for(i=1;o>=i;i++)
    {
        scanf("%s %d %d",s,&a,&b);
        if(a>b)
        {
            d=a;
            a=b;
            b=d;
        }
        if(s[0]=='C')
        {
            scanf("%d",&c);
            update(a,b,c,1);
        }
        else
        {
            memset(v,0,sizeof(v));
            find(a,b,1);
            count=0;
            for(i1=0;30>i1;i1++)
            {
                if(v[i1]==1)
                {
                    count++;
                }
            }
            printf("%d\n",count);
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值