Count Color

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 40510 Accepted: 12215

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

线段树区间染色问题,用一个tag数组标记一下就好了

#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stdlib.h> using namespace std;
#define MAX 100000
int cover[MAX*4+5];
int n;
int t;
int m;
char a;
int x,y,z;
int tag[31];
void PushDown(int node)
{
if(cover[node]!=-1)
{
cover[node<<1|1]=cover[node];
cover[node<<1]=cover[node];
cover[node]=-1;
}
}
void Update(int node,int begin,int end,int left,int right,int num)
{
if(left<=begin&&end<=right)
{
cover[node]=num;
return;
}
PushDown(node);
int m=(begin+end)>>1;
if(left<=m)
Update(node<<1,begin,m,left,right,num);
if(right>m)
Update(node<<1|1,m+1,end,left,right,num);
}
void Query(int node,int begin,int end,int left,int right,int &ans)
{
if(cover[node]!=-1)
{
if(!tag[cover[node]])
{
ans++;
tag[cover[node]]=1;
}
return;
}
PushDown(node);
if(begin==end)
return;
int m=(begin+end)>>1;
if(left<=m)
Query(node<<1,begin,m,left,right,ans);
if(right>m)
Query(node<<1|1,m+1,end,left,right,ans);
}
int main()
{ while(scanf("%d%d%d",&n,&t,&m)!=EOF)
{
memset(cover,0,sizeof(cover));
for(int i=1;i<=m;i++)
{
getchar();
scanf("%c",&a);
if(a=='C')
{
scanf("%d%d%d",&x,&y,&z);
Update(1,1,n,x,y,z-1);
} else
{
memset(tag,0,sizeof(tag));
scanf("%d%d",&x,&y);
int ans=0;
Query(1,1,n,x,y,ans);
printf("%d\n",ans);
} }
}
return 0;
}

POJ-2777 Count Color(线段树,区间染色问题)的更多相关文章

  1. poj 2777 Count Color(线段树区区+染色问题)

    题目链接:  poj 2777 Count Color 题目大意:  给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C  a  b  c 把区间[a,b]涂为c色,P  a  b 查 ...

  2. poj 2777 Count Color(线段树)

    题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  3. poj 2777 Count Color(线段树、状态压缩、位运算)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38921   Accepted: 11696 Des ...

  4. poj 2777 Count Color - 线段树 - 位运算优化

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42472   Accepted: 12850 Description Cho ...

  5. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  6. POJ 2777.Count Color-线段树(区间染色+区间查询颜色数量二进制状态压缩)-若干年之前的一道题目。。。

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53312   Accepted: 16050 Des ...

  7. POJ 2777 Count Color(线段树之成段更新)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...

  8. POJ P2777 Count Color——线段树状态压缩

    Description Chosen Problem Solving and Program design as an optional course, you are required to sol ...

  9. POJ 2777 Count Color(段树)

    职务地址:id=2777">POJ 2777 我去.. 延迟标记写错了.标记到了叶子节点上.. . . 这根本就没延迟嘛.. .怪不得一直TLE... 这题就是利用二进制来标记颜色的种 ...

  10. POJ2777 Count Color 线段树区间更新

    题目描写叙述: 长度为L个单位的画板,有T种不同的颜料.现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B&qu ...

随机推荐

  1. python垃圾回收,判断内存占用,手动回收内存,二

    以下为例子,判断计算机内存并释放程序内存. # coding=utf8 import time import psutil, gc, commands,os from logger_until imp ...

  2. [原]IOS 设备基本信息

    1.获取设备类型  (Iphone/ipad 几?) #import "sys/utsname.h" -(NSString*)getDeviceVersion{    struct ...

  3. actor binary tree lab4

    forward 与 ! (tell) 的差异,举个例子: Main(当前actor): topNode ! Insert(requester, id=1, ele = 2) topNode: root ...

  4. nodejs服务器部署教程一

    第一篇教程紧紧让你输出一个hello world 环境介绍 服务器环境:ubuntu(16.04)64位 本地环境:windows10 64位 连接工具:mobaxterm ubuntu安装和基本配置 ...

  5. 有人在群里问mysql如何选择性更新部分条件的问题

    有人在群里问这个问题 update xt_kh set zhye=zhye+1,hzyj=hzyj+1 where dlgh='kiss0451' and hzms=1 如果这样写 hzms不等于1的 ...

  6. iOS 使用动态库

    苹果的开放态度 WWDC2014上发布的Xcode6 beta版有了不少更新,其中令我惊讶的一个是苹果在iOS上开放了动态库,在Xcode6 Beta版的更新文档中是这样描述的: Frameworks ...

  7. eclipse闪退解决

    log: !SESSION 2014-03-12 14:02:45.207 -----------------------------------------------eclipse.buildId ...

  8. PHP curl get post通用类

    <?php /** * @author:xiaojiang * curl 通用方法 ..get /post 传送数据 */ class process{ const GET = 0; const ...

  9. ThinkPHP5 封装邮件发送服务(可发附件)

    1.Composer 安装 phpmailer 1 composer require phpmailer/phpmailer 2.ThinkPHP 中封装邮件服务类 我把它封装在扩展目录 extend ...

  10. 告知你不为人知的UDP-疑难杂症和使用

    版权声明:本文由黄日成原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/848077001486437077 来源:腾云阁 h ...