PKU 2777 - Count Color(线段树 + 区间修改)

来源:互联网 发布:数据预测公式 编辑:程序博客网 时间:2024/06/15 00:49

思路

线段树练习第四发

一开始用set判重,TLE了。

后来压缩了一下

这题和前几题都差不多,一开始不想写。

但是写的时候一直WA,也发现了自己的一些问题。

所以题海战术还是有用的╮(╯▽╰)╭

  1. #include <cstdio>
  2. #include <stack>
  3. #include <set>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <queue>
  8. #include <functional>
  9. #include <cstring>
  10. #include <algorithm>
  11. #include <cctype>
  12. #include <ctime>
  13. #include <cstdlib>
  14. #include <fstream>
  15. #include <string>
  16. #include <sstream>
  17. #include <map>
  18. #include <cmath>
  19. #define LL long long
  20. #define Lowbit(x) ((x) & (-x))
  21. #define MP(a, b) make_pair(a, b)
  22. #define MS(arr, num) memset(arr, num, sizeof(arr))
  23. #define PB push_back
  24. #define F first
  25. #define S second
  26. #define ROP freopen("input.txt", "r", stdin);
  27. #define MID(a, b) (a + ((b - a) >> 1))
  28. #define LC rt << 1, l, mid
  29. #define RC rt << 1|1, mid + 1, r
  30. #define LRT rt << 1
  31. #define RRT rt << 1|1
  32. #define BitCount(x) __builtin_popcount(x)
  33. const double PI = acos(-1.0);
  34. const int INF = 0x3f3f3f3f;
  35. using namespace std;
  36. const int MAXN = 1e5 + 5;
  37. const int MOD = 20071027;
  38. typedef pair<int, int> pii;
  39. typedef vector<int>::iterator viti;
  40. typedef vector<pii>::iterator vitii;
  41. int col[MAXN << 2], cnt;
  42. set<int> mp;
  43. void PushDown(int rt)
  44. {
  45. if (col[rt])
  46. {
  47. col[LRT] = col[RRT] = col[rt];
  48. col[rt] = 0;
  49. return;
  50. }
  51. }
  52. void Query(int rt, int l, int r, int L, int R)
  53. {
  54. if (col[rt])
  55. {
  56. cnt |= (1 << col[rt]);
  57. return;
  58. }
  59. int mid = MID(l, r);
  60. if (L <= mid) Query(LC, L, R);
  61. if (R > mid) Query(RC, L, R);
  62. }
  63. void Update(int rt, int l, int r, int L, int R, int val)
  64. {
  65. if (L <= l && r <= R)
  66. {
  67. col[rt] = val;
  68. return;
  69. }
  70. PushDown(rt);
  71. int mid = MID(l, r);
  72. if (L <= mid) Update(LC, L, R, val);
  73. if (R > mid) Update(RC, L, R, val);
  74. }
  75. void Build(int rt, int l, int r)
  76. {
  77. col[rt] = 1;
  78. if (l == r) return;
  79. int mid = MID(l, r);
  80. Build(LC); Build(RC);
  81. }
  82. int main()
  83. {
  84. //ROP;
  85. int n, i, j, nQuary, nCol;
  86. while (~scanf("%d%d%d", &n, &nCol, &nQuary))
  87. {
  88. Build(1, 1, n);
  89. char str[2];
  90. int a, b, c;
  91. for (i = 0; i < nQuary; i++)
  92. {
  93. scanf("%s", str);
  94. if (str[0] == 'C')
  95. {
  96. scanf("%d%d%d", &a, &b, &c);
  97. if (a > b) swap(a, b);
  98. Update(1, 1, n, a, b, c);
  99. }
  100. else
  101. {
  102. MS(vis, 0), cnt = 0;
  103. scanf("%d%d", &a, &b);
  104. if (a > b) swap(a, b);
  105. Query(1, 1, n, a, b);
  106. printf("%d\n", BitCount(cnt));
  107. }
  108. }
  109. }
  110. return 0;
  111. }
0 0