POJ-2777-CountColor(线段树,位运算)

た 入场券 2021-12-24 05:07 334阅读 0赞

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

  

转载于:https://www.cnblogs.com/YDDDD/p/10847767.html

发表评论

表情:
评论列表 (有 0 条评论,334人围观)

还没有评论,来说两句吧...

相关阅读