Count Color

Time Limit: 1000MS
Memory Limit: 65536K

Total Submissions: 33311
Accepted: 10058

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

                                                                                           [Submit]   [Go Back]   [Status]   [Discuss]

 

::会线段树的很容易知道怎么做这道题,这道题关键要解决重复计算的颜色,我用have[]来表示该颜色是否已经计算过

   1: #include <iostream>

   2: #include <cstdio>

   3: #include <cstring>

   4: #include <cmath>

   5: #include <algorithm>

   6: using namespace std;

   7: typedef long long ll;

   8: #define lson l,m,rt<<1

   9: #define rson m+1,r,rt<<1|1

  10: const int N=100000;

  11: int col[N<<2],t,sum;

  12: bool have[50];

  13:  

  14: void Down(int rt)

  15: {

  16:     if(col[rt]){

  17:         col[rt<<1]=col[rt<<1|1]=col[rt];

  18:         col[rt]=0;

  19:     }

  20: }

  21:  

  22: void build(int l,int r,int rt)

  23: {

  24:     col[rt]=1;

  25:     if(l==r) return;

  26:     int m=(l+r)>>1;

  27:     build(lson);

  28:     build(rson);

  29: }

  30:  

  31: void update(int L,int R,int c,int l,int r,int rt)

  32: {

  33:     if(L<=l&&R>=r){

  34:         col[rt]=c;

  35:         return;

  36:     }

  37:     Down(rt);

  38:     int m=(l+r)>>1;

  39:     if(L<=m) update(L,R,c,lson);

  40:     if(R>m) update(L,R,c,rson);

  41: }

  42:  

  43: void query(int L,int R,int l,int r,int rt)

  44: {

  45:     if(sum==t) return;

  46:     if(col[rt]) {

  47:         if(!have[col[rt]]){

  48:             have[col[rt]]=true;

  49:             sum++;

  50:         }

  51:         return;

  52:     }

  53:     Down(rt);

  54:     int m=(l+r)>>1;

  55:     if(L<=m) query(L,R,lson);

  56:     if(R>m) query(L,R,rson);

  57: }

  58:  

  59:  

  60: int main()

  61: {

  62:     int len,n;

  63:     while(scanf("%d%d%d",&len,&t,&n)>0)

  64:     {

  65:         build(1,len,1);

  66:         for(int i=0; i<n; i++)

  67:         {

  68:             char s[2];

  69:             int L,R;

  70:             scanf("%s%d%d",s,&L,&R);

  71:             if(L>R) swap(L,R);//个人觉得如果题目没有说明L<=R,那么一定要加这一句,以免出错

  72:             if(s[0]=='C'){

  73:                 int c;

  74:                 scanf("%d",&c);

  75:                 update(L,R,c,1,len,1);

  76:             }

  77:             else{

  78:                 memset(have,false,sizeof(have));

  79:                 sum=0;

  80:                 query(L,R,1,len,1);

  81:                 printf("%d\n",sum);

  82:             }

  83:         }

  84:     }

  85:     return 0;

  86: }

POJ 2777 Count Color(线段树之成段更新)的更多相关文章

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

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

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

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

  3. poj 2777 Count Color(线段树)

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

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

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

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

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

  6. Codeforces295A - Greg and Array(线段树的成段更新)

    题目大意 给定一个序列a[1],a[2]--a[n] 接下来给出m种操作,每种操作是以下形式的: l r d 表示把区间[l,r]内的每一个数都加上一个值d 之后有k个操作,每个操作是以下形式的: x ...

  7. hdu 1698 Just a Hook(线段树之 成段更新)

    Just a Hook                                                                             Time Limit: ...

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

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

  9. POJ 3468 A Simple Problem with Integers //线段树的成段更新

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59046   ...

随机推荐

  1. Winform开发框架之权限管理系统改进的经验总结(1)-TreeListLookupEdit控件的使用

    最近一直在做一些技术性的研究和框架改进工作,博客也落下好几天没有更新了,也该是时候静下心来,总结这段时间的一些技术改进的经验了.和上一阶段的CRM系统开发和技术研究一样,我都喜欢在一个项目或者模块完成 ...

  2. JavaMail入门第三篇 发送邮件

    JavaMail API中定义了一个java.mail.Transport类,它专门用于执行邮件发送任务,这个类的实例对象封装了某种邮件发送协议的底层实施细节,应用程序调用这个类中的方法就可以把Mes ...

  3. 【Effective Java】1、静态工厂的方式代替构造函数

    使用一个服提供者对进行服务的提供,服务的请求通过不同的提供者提供不同的服务,服务提供者首先必须在工厂中进行注册,然后才可以通过工厂实例化服务 Service.java package cn.xf.cp ...

  4. phpcms—— 内容中的附件调用和添加远程地址的调用

    phpcms中几个地址调用的方法 1,CSS路径有{CSS_PATH}2,图片路径有{IMG_PATH}3,JS路径有{JS_PATH} 4,那么附件的路径如何调用,使用下面的方式可以得到附件的路径前 ...

  5. CentOS 编译安装 MySQL5.7

    下载 所有版本下载地址: http://dev.mysql.com/downloads/mysql/ 此处用 5.7.10 wget http://dev.mysql.com/get/Download ...

  6. Linux下建立Nexus私服

    Linux下建立Nexus私服 要安装3个东西,然后配置私服: 1.JDK 2.Maven 3.Nexus 然后配置 1.JDK的安装 下载JDK安装包,格式为RPM格式,安装即可 安装程序 #rpm ...

  7. hadoop2.2.0伪分布式搭建2--安装JDK

    2.1上传FileZilla 上传 https://filezilla-project.org/ 2.2解压jdk #创建文件夹 mkdir /usr/java #解压 tar -zxvf jdk-7 ...

  8. mysql内存消耗分析

    最近有些生产服务器老是mysql内存不停得往上涨,开发人员和维护反馈,用了不少的临时表,问题时常线上发生,测试又一直比较难重现. 经观察mysql内存的os占用趋势,发现从8:40开始,mysql内存 ...

  9. uml入门之14图与图之间的关系

    1.先奉上整理的14图. 2.其次奉上整理的图之间的6种关系

  10. FDO error:Failed to label layer(XXX) for class Default

    描述: A column was specified that does not exist. 出现这个问题的原因在于label features 展示的字段不存在或者为空,只要将其勾选去掉或者换个显 ...