链接:https://vjudge.net/problem/POJ-2777#author=0

题意:

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.

思路:

线段树,还是普通的线段树,染色的查询和更新使用位运算,因为颜色区间在(1-30)之内。

所以可以使用(1<<1-1<<30)来表示这中二进制1的个数来表示颜色的数量。

不过我之前的写的普通的线段树我也不知道为啥会WA。

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <memory.h>
  5. #include <algorithm>
  6. #include <string>
  7. #include <stack>
  8. #include <vector>
  9. #include <queue>
  10.  
  11. using namespace std;
  12. typedef long long LL;
  13. const int MAXN = 1e5+10;
  14.  
  15. int Seg[MAXN*4];
  16. int lazy[MAXN*4];
  17. int Vis[100];
  18. int n, t, o;
  19. int res;
  20.  
  21. void PushDown(int root)
  22. {
  23. if (lazy[root] != 0)
  24. {
  25. Seg[root<<1] = (1<<lazy[root]);
  26. Seg[root<<1|1] = (1<<lazy[root]);
  27.  
  28. lazy[root<<1] = lazy[root];
  29. lazy[root<<1|1] = lazy[root];
  30. lazy[root] = 0;
  31. }
  32. }
  33.  
  34. void PushUp(int root)
  35. {
  36. Seg[root] = Seg[root<<1]|Seg[root<<1|1];
  37. }
  38.  
  39. void Build(int root, int l, int r)
  40. {
  41. if (l == r)
  42. {
  43. Seg[root] = 2;
  44. return;
  45. }
  46. int mid = (l + r) / 2;
  47. Build(root << 1, l, mid);
  48. Build(root << 1 | 1, mid + 1, r);
  49. PushUp(root);
  50. }
  51.  
  52. void Update(int root, int l, int r, int ql, int qr, int c)
  53. {
  54. if (r < ql || qr < l)
  55. return;
  56. if (ql <= l && r <= qr)
  57. {
  58. Seg[root] = (1<<c);
  59. lazy[root] = c;
  60. return;
  61. }
  62. PushDown(root);
  63. int mid = (l+r)/2;
  64. Update(root<<1, l, mid, ql, qr, c);
  65. Update(root<<1|1, mid+1, r, ql, qr, c);
  66. PushUp(root);
  67. }
  68.  
  69. int Query(int root, int l, int r, int ql, int qr)
  70. {
  71. if (r < ql || qr < l)
  72. return 0;
  73. if (ql <= l && r <= qr)
  74. {
  75. return Seg[root];
  76. }
  77. int mid = (l+r)/2;
  78. PushDown(root);
  79. int col1 = 0, col2 = 0;
  80. col1 = Query(root<<1, l, mid, ql, qr);
  81. col2 = Query(root<<1|1, mid+1, r, ql, qr);
  82. return col1|col2;
  83. }
  84.  
  85. int Get(int x)
  86. {
  87. int res = 0;
  88. while (x)
  89. {
  90. if (x&1)
  91. res++;
  92. x >>= 1;
  93. }
  94. return res;
  95. }
  96.  
  97. int main()
  98. {
  99. char op[10];
  100. int a, b, c;
  101. while (~scanf("%d%d%d", &n, &t, &o))
  102. {
  103. Build(1, 1, n);
  104. while (o--)
  105. {
  106. scanf("%s", op);
  107. if (op[0] == 'C')
  108. {
  109. scanf("%d%d%d", &a, &b, &c);
  110. if (a > b)
  111. swap(a, b);
  112. Update(1, 1, n, a, b, c);
  113. }
  114. else
  115. {
  116. scanf("%d%d", &a, &b);
  117. if (a > b)
  118. swap(a, b);
  119. memset(Vis, 0, sizeof(Vis));
  120. int res = Query(1, 1, n, a, b);
  121. printf("%d\n", Get(res));
  122. }
  123. }
  124. }
  125.  
  126. return 0;
  127. }

  

POJ-2777-CountColor(线段树,位运算)的更多相关文章

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

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

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

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

  3. poj 3225 线段树+位运算

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

  4. hdu 5023 线段树+位运算

    主要考线段树的区间修改和区间查询,这里有一个问题就是这么把一个区间的多种颜色上传给父亲甚至祖先节点,在这里题目告诉我们最多30颜色,那么我们可以把这30中颜色用二进制储存和传给祖先节点,二进制的每一位 ...

  5. Codeforces 620E New Year Tree(线段树+位运算)

    题目链接 New Year Tree 考虑到$ck <= 60$,那么用位运算统计颜色种数 对于每个点,重新标号并算出他对应的进和出的时间,然后区间更新+查询. 用线段树来维护. #includ ...

  6. Count Color(线段树+位运算 POJ2777)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...

  7. Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树, 位运算)

    链接: https://codeforces.com/contest/1234/problem/D 题意: You are given a string s consisting of lowerca ...

  8. poj_2777线段树+位运算

    第一次没想到用位运算,不出意料的T了,,, PS:在床上呆了接近两个月后,我胡汉三又杀回来刷题啦-- #include<iostream> #include<cstdio> # ...

  9. poj 2777(线段树的节点更新策略)

    /* 之前的思想是用回溯的方式进行颜色的更新的!如果用回溯的方法的话,就是将每一个节点的颜色都要更新 通过子节点的颜色情况来判断父节点的颜色情况 !这就是TLE的原因! 后来想一想没有必要 !加入[a ...

  10. 【洛谷】【线段树+位运算】P2574 XOR的艺术

    [题目描述:] AKN觉得第一题太水了,不屑于写第一题,所以他又玩起了新的游戏.在游戏中,他发现,这个游戏的伤害计算有一个规律,规律如下 1. 拥有一个伤害串为长度为n的01串. 2. 给定一个范围[ ...

随机推荐

  1. Mysql转换表存储引擎的三种方式

    或许会有一些场景需要改变表的存储引擎,例如存储日志的表如果几乎只有insert和少量的select操作,为了追求更好的插入性能有可能会需要把存储引擎更换为MyISAM.但是,本文不建议在同一个数据库中 ...

  2. ffmpeg 调试

    --enable-debug=3 --disable-optimizations --disable-yasm --disable-asm

  3. frame标签

    frame中有一个属性scrolling,可以这样设置它 <frame src="top.html" noresize scrolling="no"/&g ...

  4. listen 72

    Warmer Temps May Bollux Botanicals Global warming might seem like a botanical boon. After all, milde ...

  5. The Review Plan I-禁位排列和容斥原理

    The Review Plan I Time Limit: 5000ms Case Time Limit: 5000ms Memory Limit: 65536KB   64-bit integer ...

  6. jQuery之简单的表单验证

    html部分: <body> <form method="post" action=""> <div class="in ...

  7. RedisDesktopManager 可视化工具提示:无法加载键:Scan..

    原因是redis的版本过低,window下的redis-cli.exe客户端输入 info 命令可看到该redis的版本,这个scan查看要redis2.80版本以上!!!!

  8. WPF学习系列之二 (依赖项属性)

    依赖属性;(dependency property)  它是专门针对WPF创建的,但是WPF库中的依赖项属性都使用普通的.NET属性过程进行了包装.从而可能通过常规的方式使用它们,即使使用他们的代码不 ...

  9. matlab下的caffe接口配置(Windows)

    本文基于大部分网上方法 http://blog.csdn.net/d5224/article/details/51916178,外加一点自己的个人实际配置经历,环境变量在配置后尽管显示正确并且重启多次 ...

  10. go语言中将函数作为变量传递

    在Go中函数也是一种变量,我们可以通过type来定义它,它的类型就是所有拥有相同的参数,相同的返回值的一种类型,函数当做值和类型在我们写一些通用接口的时候非常有用,通过下面这个例子我们可以看到test ...