【POJ 2777】 Count Color(线段树区间更新与查询)

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 40949   Accepted: 12366

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

  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

Source

感觉有些神经衰弱了。。。写发水水的线段树放松放松。。。

一直在搞图论 是该换换弦 磨得快锈了 太折磨人了……(弱也不知道说了些啥

一块木板 长L(1~L) 有T种颜色的油漆标号1~T 默认木板初始是1号颜色

进行O次操作 操作有两种

C a b c 表示木板a~b段涂c种油漆(若之前涂过其它颜色 则覆盖掉)

P a b 表示询问木板a~b段如今涂了几种油漆

两个数组 一个存树 一个存涂了哪几种油漆

存树的表示a~b涂的某种颜色 然后搞搞就出来了……好乏

代码例如以下:

  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <cstdlib>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <queue>
  8. #include <stack>
  9. #include <list>
  10. #include <algorithm>
  11. #include <map>
  12. #include <set>
  13. #define LL long long
  14. #define Pr pair<int,int>
  15. #define fread() freopen("in.in","r",stdin)
  16. #define fwrite() freopen("out.out","w",stdout)
  17.  
  18. using namespace std;
  19. const int INF = 0x3f3f3f3f;
  20. const int msz = 10000;
  21. const int mod = 1e9+7;
  22. const double eps = 1e-8;
  23.  
  24. int tr[400400];
  25. bool col[33];
  26. int L,T,O;
  27.  
  28. void Color(int root,int l,int r,int a,int b,int c)
  29. {
  30. if(a == l && b == r)
  31. {
  32. tr[root] = c;
  33. return;
  34. }
  35. int mid = (l+r)>>1;
  36. if(tr[root])
  37. tr[root<<1] = tr[root<<1|1] = tr[root];
  38. tr[root] = 0;
  39.  
  40. if(mid >= b) Color(root<<1,l,mid,a,b,c);
  41. else if(mid+1 <= a) Color(root<<1|1,mid+1,r,a,b,c);
  42. else
  43. {
  44. Color(root<<1,l,mid,a,mid,c);
  45. Color(root<<1|1,mid+1,r,mid+1,b,c);
  46. }
  47. }
  48.  
  49. void Search(int root,int l,int r,int a,int b)
  50. {
  51. //printf("root:%d l:%d r:%d co:%d\n",root,l,r,tr[root]);
  52. if(tr[root])
  53. {
  54. col[tr[root]] = 1;
  55. return;
  56. }
  57. int mid = (l+r)>>1;
  58. if(mid >= b) Search(root<<1,l,mid,a,b);
  59. else if(mid+1 <= a) Search(root<<1|1,mid+1,r,a,b);
  60. else
  61. {
  62. Search(root<<1,l,mid,a,mid);
  63. Search(root<<1|1,mid+1,r,mid+1,b);
  64. }
  65. }
  66.  
  67. int main()
  68. {
  69. //fread();
  70. //fwrite();
  71. char opt[3];
  72. int a,b,c;
  73.  
  74. while(~scanf("%d%d%d",&L,&T,&O))
  75. {
  76. memset(tr,0,sizeof(tr));
  77. tr[1] = 1;
  78. while(O--)
  79. {
  80. scanf("%s",opt);
  81. scanf("%d%d",&a,&b);
  82. if(a > b) swap(a,b);
  83.  
  84. if(opt[0] == 'C')
  85. {
  86. scanf("%d",&c);
  87. Color(1,1,L,a,b,c);
  88. }
  89. else
  90. {
  91. memset(col,0,sizeof(col));
  92. Search(1,1,L,a,b);
  93.  
  94. int ans = 0;
  95. for(int i = 1; i <= T; ++i)
  96. ans += col[i];
  97. printf("%d\n",ans);
  98. }
  99. }
  100. }
  101.  
  102. return 0;
  103. }



【POJ 2777】 Count Color(线段树区间更新与查询)的更多相关文章

  1. poj 2777 Count Color(线段树)

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

  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 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

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

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

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

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

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

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

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

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

  8. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  9. ZOJ 1610 Count the Color(线段树区间更新)

    描述Painting some colored segments on a line, some previously painted segments may be covered by some ...

随机推荐

  1. servlet——web应用中路径问题

    target.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html ...

  2. 梦想CAD控件自定义实体实现

    一.增加自定义实体对象 调用DrawCustomEntity函数,绘制一个自定义实体对象. 下面代码绘制一个自定义实体,C#代码实现如下: private void DrawMlineCommand( ...

  3. eclipse配置Tomcat服务器server locations的方法

    最近放弃了使用Myeclipse,转而使用eclipse作为开发工具,确实Myeclipse集成了太多东西,使得开发人员的配置越来越少,这不是个好事,使用eclipse后,有些地方就得自己去配置,比如 ...

  4. 通过Oracle函数SQL实现C# String.Format字符串格式化功能

    语言国际化要求,开发上要求Oracle数据库SQL中对应的返回信息-Message,实现一个通用函数调用,比如:提示信息内容:条码123456当前工站在FCT!”,即通用的信息内容格式化标准为:“条码 ...

  5. Autolayout性能优化

    客户的需求就是我们进步的动力.最近有客户提出大数据量Topo图的自动布局问题,在Topo中除了Node.Link,还包括Group.Subnetwork等容器组件.在这样的情况下,我们抛开布局算法不谈 ...

  6. extjs动态插入一列

    StdDayWordQuery:function(btn,event){ var form=Ext.getCmp('queryFormSDW'); var userNameORuserCode = f ...

  7. Game Rank(NCPC 2016 大模拟)

    题目: The gaming company Sandstorm is developing an online two player game. You have been asked to imp ...

  8. linux nethogs-终端下的网络流量监控工具

    推荐:更多linux 性能监测与优化 关注:linux命令大全 有很多适用于Linux系统的开源网络监视工具.比如说,你可以用命令iftop来检查带宽使用情况.netstat用来查看接口统计报告,还有 ...

  9. Django 命令行调用模版渲染

    首先我们需要进入 Django 的 shell python manage.py shell 渲染模版中的 name from django.template import Context,Templ ...

  10. Grid Convergence Index-- Post Processing in CFD

    t Grid Convergence Index Table of Contents 1. Grid/mesh independence   GCI 1.1. Richardson extrapola ...