Count Color

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 42507   Accepted: 12856

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.

Ouput

results of the output operation in order, each line contains a number.

Sample Input

  1. 2 2 4
  2. C 1 1 2
  3. P 1 2
  4. C 2 2 2
  5. P 1 2

Sample Output

  1. 2
  2. 1

题目大意:

有L个画板,30种颜色,o个操作:P a b :询问a-b 种有多少种颜色不同的,C  a b c:把a-b全部涂成c的颜色(覆盖掉)

解题思路:

线段树+二进制判重

代码

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. #define N 100005
  5. using namespace std;
  6. int n,m,q;
  7. char c;
  8. int x,y,z;
  9. int sum;
  10. struct Node
  11. {
  12. int l,r,col;//col用位运算,颜色30种,
  13. int cover;//延时更新,是否涂了颜色
  14. }no[*N];
  15. void pushup(int u)
  16. {
  17. no[u].col=no[u*].col|no[u*+].col;
  18. return;
  19. }
  20. void pushdown(int u)
  21. {
  22. no[u].cover=;
  23. no[u*].cover=;
  24. no[u*].col=no[u].col;
  25. no[u*+].cover=;
  26. no[u*+].col=no[u].col;
  27. return;
  28. }
  29. void build(int u,int left,int right)
  30. {
  31. no[u].l=left;
  32. no[u].r=right;
  33. no[u].col=;
  34. no[u].cover=;//初始状态全为1
  35. if(left==right)
  36. return;//没有赋值
  37. int mid=(no[u].l+no[u].r)>>;
  38. build(u*,left,mid);
  39. build(u*+,mid+,right);
  40. pushup(u);
  41. }
  42. void updata(int u,int left,int right,int val)
  43. {
  44. if(no[u].l==left&&no[u].r==right)
  45. {
  46. no[u].col=<<(val-);//直接等于,覆盖原有颜色
  47. no[u].cover=;
  48. return;
  49. }
  50. if(no[u].col==<<(val-))return;//剪枝:如果颜色一样,不用更新
  51. if(no[u].cover)pushdown(u);//延时更新
  52. int mid=(no[u].l+no[u].r)>>;
  53. if(right<=mid)updata(u*,left,right,val);
  54. else if(left>mid)updata(u*+,left,right,val);
  55. else
  56. {
  57. updata(u*,left,mid,val);
  58. updata(u*+,mid+,right,val);
  59. }
  60. pushup(u);
  61. }
  62. void query(int u,int left,int right)
  63. {
  64. if(no[u].l==left&&no[u].r==right)
  65. {
  66. sum|=no[u].col;
  67. return ;
  68. }
  69. if(no[u].cover)pushdown(u);
  70. int mid=(no[u].l+no[u].r)>>;
  71. if(right<=mid)query(u*,left,right);
  72. else if(left>mid)query(u*+,left,right);
  73. else
  74. {
  75. query(u*,left,mid);
  76. query(u*+,mid+,right);
  77. }
  78. }
  79. int Ans(int sum)
  80. {
  81. int ans=;
  82. while(sum)
  83. {
  84. if(sum&)
  85. ans++;
  86. sum=(sum>>);
  87. }
  88. return ans;
  89. }
  90. int main()
  91. {
  92. freopen("poj2777.in","r",stdin);
  93. freopen("poj2777.out","w",stdout);
  94. scanf("%d%d%d",&n,&m,&q);
  95. build(,,n);
  96. getchar();
  97. for(int i=;i<=q;i++)
  98. {
  99. scanf("%s",&c);
  100. if(c=='C')
  101. {
  102. scanf("%d%d%d",&x,&y,&z);
  103. if(x>y)swap(x,y);
  104. getchar();
  105. updata(,x,y,z);
  106. }
  107. else
  108. {
  109. scanf("%d%d",&x,&y);
  110. getchar();
  111. sum=;
  112. if(x>y)swap(x,y);//x可能>y
  113. query(,x,y);
  114. cout<<Ans(sum)<<endl;;
  115. }
  116. }
  117. return ;
  118. }

POJ 2777(线段树)的更多相关文章

  1. poj 2777(线段树+lazy思想) 小小粉刷匠

    http://poj.org/problem?id=2777 题目大意 涂颜色,输入长度,颜色总数,涂颜色次数,初始颜色都为1,然后当输入为C的时候将x到y涂为颜色z,输入为Q的时候输出x到y的颜色总 ...

  2. POJ 2777——线段树Lazy的重要性

    POJ 2777 Count Color --线段树Lazy的重要性 原题 链接:http://poj.org/problem?id=2777 Count Color Time Limit: 1000 ...

  3. POJ 2777 线段树基础题

    题意: 给你一个长度为N的线段数,一开始每个树的颜色都是1,然后有2个操作. 第一个操作,将区间[a , b ]的颜色换成c. 第二个操作,输出区间[a , b ]不同颜色的总数. 直接线段树搞之.不 ...

  4. poj 2777线段树应用

    敲了n遍....RE愉快的debug了一晚上...发现把#define maxn = 100000 + 10 改成 #define maxn = 100010 就过了....感受一下我呵呵哒的表情.. ...

  5. Count Color POJ - 2777 线段树

    Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds ...

  6. poj 2777 线段树的区间更新

    Count Color Time Limit: 1000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java ...

  7. poj 2777 线段树 区间更新+位运算

    题意:有一个长板子,分成多段,有两种操作,第一种是C给从a到b那段染一种颜色c,另一种是P询问a到b有多少种不同的颜色.Sample Input2 2 4  板长 颜色数目 询问数目C 1 1 2P ...

  8. poj 2886 线段树+反素数

    Who Gets the Most Candies? Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 12744   Acc ...

  9. poj 3468(线段树)

    http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...

随机推荐

  1. Android设计模式系列-单例模式

    单例模式,可以说是GOF的23种设计模式中最简单的一个. 这个模式相对于其他几个模式比较独立,它只负责控制自己的实例化数量单一(而不是考虑为用户产生什么样的实例),很有意思,是一个感觉上很干净的模式, ...

  2. 将java源码打成jar包

    方法一:通过jar命令 jar命令的用法: 下面是jar命令的帮助说明: 用法:jar {ctxui}[vfm0Me] [jar-file] [manifest-file] [entry-point] ...

  3. 某项目 需要在UITabbar 上显示小红点,在此搜罗了三个方法。

    1.使用系统自带的,并且可以在小红点上显示数字. [itemOne setBadgeValue:@""]; //显示不带数字的小红点 [itemOne setBadgeValue: ...

  4. C#实现在Winform中嵌入Word和Excel

    http://www.cnblogs.com/wuzi145/archive/2012/05/08/2490680.html 在此只是介绍一个简单控件:dsoframer.ocx的使用,这个控件需要通 ...

  5. [MODx] 7. MIGX DB

    MODx provides a really unfriendly way to work with xPDO class. What I means is you need to define XM ...

  6. 10 Technologies That will Shape Future Education--reference

    http://dizyne.net/technologies-that-will-shape-future-education/ Technology is on the rise and with ...

  7. CreateToolhelp32Snapshot

    CreateToolhelp32Snapshot CreateToolhelp32Snapshot函数为指定的进程.进程使用的堆[HEAP].模块[MODULE].线程[THREAD])建立一个快照[ ...

  8. Mysql在windows下的免安装配置步骤和重新安装的步骤

    windows下mysql免安装配置 1. 下载mysql免安装压缩包 下载mysql-5.6.22-winx64.zip 解压到本地D:\mysql-5.6.22-winx64 2. 修改配置文件 ...

  9. Java设计模式10:设计模式之 值对象

    1. 场景和问题: 在Java程序中,需要在对象之间交互大量的数据,比如要为方法传入参数,也要获取方法的返回值,请问如何能更好的进行数据的交互? 2. 解决方案:      值对象 3. 值对象的本质 ...

  10. jsp The requested resource (/demo10/loginBean) is not available.

    The requested resource (/demo10/loginBean) is not available. <?xml version="1.0" encodi ...