Count Color

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 39917 Accepted: 12037

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

POJ Monthly–2006.03.26,dodo

这道线段树的题敲的总体还算比较顺,就是在初始化的时候被坑惨了,说是第一种颜色却手残的写成了1,明明是(1<<1).悲哀啊

  1. #include <map>
  2. #include <set>
  3. #include <queue>
  4. #include <cstring>
  5. #include <string>
  6. #include <cstdio>
  7. #include <iostream>
  8. #include <algorithm>
  9. using namespace std;
  10. struct node
  11. {
  12. int sta;
  13. bool lazy;
  14. }Tree[500000];
  15. int Trans(int ans)
  16. {
  17. int sum=0;
  18. while(ans)
  19. {
  20. if(ans&1)
  21. {
  22. sum++;
  23. }
  24. ans/=2;
  25. }
  26. return sum;
  27. }
  28. void Build(int L,int R,int site)
  29. {
  30. Tree[site].lazy=false;
  31. Tree[site].sta=2;//初始化为颜色1就是(1<<1),看了一晚上才看出来
  32. if(L==R)
  33. {
  34. return ;
  35. }
  36. int mid=(L+R)>>1;
  37. Build(L,mid,site<<1);
  38. Build(mid+1,R,site<<1|1);
  39. }
  40. void update(int L,int R,int l,int r,int site,int ans)
  41. {
  42. if(L==l&&R==r)//更新到区间
  43. {
  44. Tree[site].sta=(1<<ans);
  45. Tree[site].lazy=true;
  46. return ;
  47. }
  48. if(Tree[site].lazy)//向下更新
  49. {
  50. Tree[site<<1].sta=Tree[site<<1|1].sta=Tree[site].sta;
  51. Tree[site<<1].lazy=Tree[site<<1|1].lazy=true;
  52. Tree[site].lazy=false;
  53. }
  54. int mid=(L+R)>>1;
  55. if(r<=mid)
  56. {
  57. update(L,mid,l,r,site<<1,ans);
  58. }
  59. else if(l>mid)
  60. {
  61. update(mid+1,R,l,r,site<<1|1,ans);
  62. }
  63. else
  64. {
  65. update(L,mid,l,mid,site<<1,ans);
  66. update(mid+1,R,mid+1,r,site<<1|1,ans);
  67. }
  68. Tree[site].sta=Tree[site<<1].sta|Tree[site<<1|1].sta;//区间的合并
  69. }
  70. int Query(int L,int R,int l,int r,int site)
  71. {
  72. if(L==l&&R==r)
  73. {
  74. return Tree[site].sta;
  75. }
  76. if(Tree[site].lazy)//向下更新
  77. {
  78. Tree[site<<1].sta=Tree[site<<1|1].sta=Tree[site].sta;
  79. Tree[site<<1].lazy=Tree[site<<1|1].lazy=true;
  80. Tree[site].lazy=false;
  81. }
  82. int mid=(L+R)>>1;
  83. if(r<=mid)
  84. {
  85. return Query(L,mid,l,r,site<<1);
  86. }
  87. else if(l>mid)
  88. {
  89. return Query(mid+1,R,l,r,site<<1|1);
  90. }
  91. else
  92. {
  93. return Query(L,mid,l,mid,site<<1)|Query(mid+1,R,mid+1,r,site<<1|1);
  94. }
  95. }
  96. int main()
  97. {
  98. int L,T,O;
  99. char s[3];
  100. int a,b,c;
  101. while(~scanf("%d %d %d",&L,&T,&O))
  102. {
  103. Build(1,L,1);
  104. for(int i=1;i<=O;i++)
  105. {
  106. scanf("%s %d %d",s,&a,&b);
  107. if(a>b)
  108. {
  109. swap(a,b);
  110. }
  111. if(s[0]=='C')
  112. {
  113. scanf("%d",&c);
  114. update(1,L,a,b,1,c);
  115. }
  116. else
  117. {
  118. int ans=Query(1,L,a,b,1);
  119. ans=Trans(ans);
  120. printf("%d\n",ans);
  121. }
  122. }
  123. }
  124. return 0;
  125. }

Count Color(线段树+位运算 POJ2777)的更多相关文章

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

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

  2. [poj2777] Count Color (线段树 + 位运算) (水题)

    发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...

  3. Count Colour_poj2777(线段树+位)

    POJ 2777 Count Color (线段树)   Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

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

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

  5. POJ 2777 Count Color(线段树+位运算)

    题目链接:http://poj.org/problem?id=2777 Description Chosen Problem Solving and Program design as an opti ...

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

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

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

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

  8. poj 3225 线段树+位运算

    略复杂的一道题,首先要处理开闭区间问题,扩大两倍即可,注意输入最后要\n,初始化不能随便memset 采用线段树,对线段区间进行0,1标记表示该区间是否包含在s内U T S ← S ∪ T 即将[l, ...

  9. poj 2777 Count Color(线段树)

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

随机推荐

  1. Server Error in '/' Application

    在服务器部署了网站,然后访问的时候出现异常   Server Error in '/' Application,一般这样的异常都是不明确的,我们应当把网站根目录web.config<custom ...

  2. mysql远程连接提示无法连接,报1130错误

    可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从”l ...

  3. Linux下JDK安装笔记

    环境说明: Linux版本: CentOS6.2   JDK:jdk-7u60-linux-x64.tar.gz 1.下载jdk-7u60-linux-x64.tar.gz,本人是放到了~/工具 目录 ...

  4. org.apache.catalina.LifecycleException

    web.xml中<url-pattern>.do</url-pattern>改为<url-pattern>*.do</url-pattern> 1 严重 ...

  5. ARM的一些基本概念

    MPU介绍: mpu是一个芯片,重力加速器(加速度)和陀螺仪(角速度) iic总线.在板上有iic控制器 连接着 最多128个外设,每个外设有地址,可以通信. 寄存器: cpu中的寄存器是为了加快运算 ...

  6. C# 计时器

    一.Stopwatch 主要用于测试代码段使用了多少时间 使用方法: Stopwatch sw=new Stopwatch(); sw.Start(); ... sw.Stop(); Console. ...

  7. JavaScript 回调函数中的 return false 问题

    今天一个同事问了我一个问题,就是在 Ajax 方法中,请求成功后(success)的回调函数中根据响应的值来判断程序是否继续执行,他不解的是在回调函数中已经 return false 了,但是 Aja ...

  8. python 安装 twisted 库

    pip 安装twisted库需要先安装依赖包,不然报"error: command 'gcc' failed with exit status 1" # yum install g ...

  9. iOS中集成ijkplayer视频直播框架

    ijkplayer 是一款做视频直播的框架, 基于ffmpeg, 支持 Android 和 iOS, 网上也有很多集成说明, 但是个人觉得还是不够详细, 在这里详细的讲一下在 iOS 中如何集成ijk ...

  10. 前台 JSON对象转换成字符串 相互转换 的几种方式

    在最近的工作中,使用到JSON进行数据的传递,特别是从前端传递到后台,前台可以直接采用ajax的data函数,按json格式传递,后台Request即可,但有的时候,需要传递多个参数,后台使用requ ...