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. UnityShader快速上手指南(二)

    简介 前一篇介绍了如果编写最基本的shader,接下来本文将会简单的深入一下,我们先来看下效果吧 呃,gif效果不好,实际效果是很平滑的动态过渡 实现思路 1.首先我们要实现一个彩色方块 2.让色彩动 ...

  2. 用C#开发的双色球走势图(一)

    首先声明,个人纯粹无聊之作,不作商业用途. 我相信每个人都拥有一个梦想那就是有朝一日能中500W,这个也一直是我的梦想,并默默每一期双色球或多或少要贡献自己一点点力量,本人并不属于那种铁杆的彩票迷,每 ...

  3. Spring管理 hibernate 事务配置的五种方式

    Spring配置文件中关于事务配置总是由三个组成部分,DataSource.TransactionManager和代理机制这三部分,无论是那种配置方法,一般变化的只是代理机制这块! 首先我创建了两个类 ...

  4. Tomcat配置文件server.xml

    Tomcat目录中的server.xml配置文件 server.xml称为主配置文件或全局配置文件 它完成以下两个目标: 1,提供Tomcat组件的初始化配置 2,说明Tomcat的结构和含义,使得T ...

  5. 怎样在win7系统配置数据源

    1.点击桌面的我Windows 图标,找打控制面板 2.进入控制面板主页,选择系统和安全,进入系统和安全 3.进入系统和安全主页后选择管理工具,点击进入 4.进入管理工具后,选择数据源,进行数据源的配 ...

  6. 个人总结 HTML+CSS

    从大一下学期接触,一直到今年,接触的时间也挺长的了,最近一些认识的盆友和同学说是想学习前端,自己也开始慢慢停下脚步,不再拼命地去学很多框架的东西,回归到基础,慢慢把基础打牢 很多知识碎片一直来不及整理 ...

  7. javascript数组浅谈2

    上次说了数组元素的增删,的这次说说数组的一些操作方法 join()方法: ,,] arr.join("_") //1_2_3 join方法会返回一个由数组中每个值的字符串形式拼接而 ...

  8. Microsoft Dynamics 2013 --Social Pane

    Microsoft Dynamics 2013 有一个新的东西--Social Pane (图1) 进入窗体设置,发现改选项卡的详细设置如下 (图2) Tab键的选项有3种[活动][公告][注释],若 ...

  9. 桥牌笔记:Skill 4 Series A–Deal 5

    南主打5C. 此牌的难点在于:如果黑桃4-2分布时,有没有打成的希望?看来黑桃.红桃.方块各1个失张无法避免? 但希望还是有的,那就是东家拿2张黑桃,并且有3张将牌. 这时庄家可以清2轮将牌,拔2轮黑 ...

  10. 浅析LruCache原理

    Android用LruCache来取代原来强引用和软引用实现内存缓存,因为据说自2.3以后Android将更频繁的调用GC,导致软引用缓存的数据极易被释放. LruCache使用一个LinkedHas ...