Count Color poj 2777

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

Count Color

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 29470

Accepted: 8800

Description

Chosen ProblemSolving and Program design as an optional course, you are required to solve allkinds 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 colorthe board - one segment with only one color. We can do following two operationson the board: 

1. "C A B C" Color the board from segment A to segment B with colorC. 
2. "P A B" Output the number of different colors painted betweensegment 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 Tis 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. Nowthe rest of problem is left to your. 

Input

First line ofinput contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1<= O <= 100000). Here O denotes the number of operations. Following Olines, each contains "C A B C" or "P A B" (here A, B, C areintegers, and A may be larger than B) as an operation defined previously.

Output

Ouput results ofthe 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

 

题意:求一块L长的木板被平分成L块后,用T中颜色来染色,有O次操作。

思路:考虑到只有三十种颜色,我们每次查询的时候,都可以用一个数据col来标记第i种颜色是否被染色,染色标记为1,最后遍历一遍col数组就可以算出染色的总数。

整体还是用线段树来实现,线段树每个节点存放颜色标记,看染上哪种颜色,初始值都为颜色1,用懒操作来标记更新后的颜色。

 

代码:

#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define maxn 100010

using namespace std;

struct Tree
{
    int l,r,Color;
}tree[maxn*4];

int col[32];

void buildTree(int l,int r,int id)
{//所有的都被上色1;
    int mid = (l+r)/2;
    tree[id].l = l;
    tree[id].r = r;

    if(l==r)
    {//最先都上颜色1
        tree[id].Color = 1;
        return ;
    }
    buildTree(l,mid,id*2);
    buildTree(mid+1,r,id*2+1);
    tree[id].Color = 1;
}

void pushDown(int id)
{
	//如果该点有标记染色,那么该点的孩子节点也需要
    if(tree[id].Color!=-1)
    {
        tree[id*2].Color = tree[id*2+1].Color = tree[id].Color;
		tree[id].Color=-1;
    }
}

//查询区间A,B颜色数量;
void queryColor(int l,int r,int id)
{
    if(tree[id].Color!=-1)
    {//表示在该区间已经有颜色了,颜色编号为tree[id].Color
        col[tree[id].Color] = 1;
        return ;
    }
    //查询到了叶子节点
    if(tree[id].l == tree[id].r)
		return;
    pushDown(id);
	if(l<=tree[id*2].r)
		queryColor(l,r,id*2);
	if(r>=tree[id*2+1].l)
		queryColor(l,r,id*2+1);
}


//更新颜色区间
void updateNote(int l,int r,int val,int id)
{
    int left = tree[id].l;
    int right = tree[id].r;
    if(l<=left && right <= r)
    {//表示要查询的区间[l,r]涵盖线段树区间[left,right],那么该[left,right]区间上色(标记)
        tree[id].Color = val;//给该区间上色val
        return ;
    }
	else
	{
		pushDown(id);
		if(l<=tree[id*2].r)
			updateNote(l,r,val,id*2);
		if(r>=tree[id*2+1].l)
			updateNote(l,r,val,id*2+1);

	}
}

int main()
{
    int L,T,O;
    int i,j;
    char ch;
    int A,B,C;
    scanf("%d%d%d",&L,&T,&O);
    buildTree(1,L,1);
    for(i=1;i<=O;i++)
    {
        getchar();
        scanf("%c ",&ch);
        if(ch=='C')
        {//更新
            scanf("%d%d%d",&A,&B,&C);
            updateNote(A,B,C,1);//更新区间A,B的值为颜色C
        }
        else if(ch=='P')
        {//查询
            scanf("%d%d",&A,&B);
            memset(col,-1,sizeof(col));
            queryColor(A,B,1);//查询区间A,B颜色数量;
            int count = 0;
            for(j=1;j<=30;j++)
            {
                if(col[j]==1)
                    count++;
            }
            printf("%d\n",count);

        }

    }
	getchar();
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯的世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值