HDU 4046 - Panda (线段树 + 单点更新)

题意

给出一个字符串,有两种操作。一种是询问[l, r]里有几个wbw,还有是修改某个点的字符。

思路

可以用一个点表示这个点的前三个是不是组成wbw,是就1,不是0.这样就可以通过线段树来处理。

如果修改了一个点,就要修改另外被影响的两个点。

代码

  
  
  
  
  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 <string>
  13. #include <map>
  14. #include <cmath>
  15. #define LL long long
  16. #define SZ(x) (int)x.size()
  17. #define Lowbit(x) ((x) & (-x))
  18. #define MP(a, b) make_pair(a, b)
  19. #define MS(arr, num) memset(arr, num, sizeof(arr))
  20. #define PB push_back
  21. #define F first
  22. #define S second
  23. #define ROP freopen("input.txt", "r", stdin);
  24. #define MID(a, b) (a + ((b - a) >> 1))
  25. #define LC rt << 1, l, mid
  26. #define RC rt << 1|1, mid + 1, r
  27. #define LRT rt << 1
  28. #define RRT rt << 1|1
  29. #define BitCount(x) __builtin_popcount(x)
  30. const double PI = acos(-1.0);
  31. const int INF = 0x3f3f3f3f;
  32. using namespace std;
  33. const int MAXN = 5e4 + 10;
  34. const int MOD = 100000007;
  35. typedef pair<int, int> pii;
  36. typedef vector<int>::iterator viti;
  37. typedef vector<pii>::iterator vitii;
  38. int vmax[MAXN << 2];
  39. char str[MAXN];
  40. void PushUp(int rt)
  41. {
  42. vmax[rt] = vmax[LRT] + vmax[RRT];
  43. }
  44. void Build(int rt, int l, int r)
  45. {
  46. if (l == r)
  47. {
  48. if (l >= 3)
  49. if (str[l] == 'w' && str[l - 1] == 'b' && str[l - 2] == 'w') vmax[rt] = 1;
  50. else vmax[rt] = 0;
  51. else vmax[rt] = 0;
  52. return;
  53. }
  54. int mid = MID(l, r);
  55. Build(LC); Build(RC);
  56. PushUp(rt);
  57. }
  58. void Update(int rt, int l, int r, int pos)
  59. {
  60. if (l == r)
  61. {
  62. if (str[l] == 'w' && str[l - 1] == 'b' && str[l - 2] == 'w') vmax[rt] = 1;
  63. else vmax[rt] = 0;
  64. return;
  65. }
  66. int mid = MID(l, r);
  67. if (pos <= mid) Update(LC, pos);
  68. else Update(RC, pos);
  69. PushUp(rt);
  70. }
  71. int Query(int rt, int l, int r, int L, int R)
  72. {
  73. if (L <= l && r <= R) return vmax[rt];
  74. int mid = MID(l, r), ans = 0;
  75. if (L <= mid) ans += Query(LC, L, R);
  76. if (R > mid) ans += Query(RC, L, R);
  77. return ans;
  78. }
  79. int main()
  80. {
  81. //ROP;
  82. int T, i, j, cases = 0;
  83. scanf("%d", &T);
  84. while (T--)
  85. {
  86. printf("Case %d:\n", ++cases);
  87. int m, n;
  88. scanf("%d%d", &m, &n);
  89. scanf("%s", str + 1);
  90. Build(1, 1, m);
  91. while (n--)
  92. {
  93. int a, b;
  94. scanf("%d%d%*c", &a, &b);
  95. if (a == 0)
  96. {
  97. int c;
  98. scanf("%d", &c);
  99. if (c - b < 2)
  100. {
  101. puts("0");
  102. continue;
  103. }
  104. printf("%d\n", Query(1, 1, m, b + 3, c + 1));
  105. }
  106. else
  107. {
  108. b++;
  109. char c;
  110. scanf("%c", &c);
  111. str[b] = c;
  112. if (b >= 3) Update(1, 1, m, b);
  113. if (b >= 2 && b + 1 <= m) Update(1, 1, m, b + 1);
  114. if (b >= 1 && b + 2 <= m) Update(1, 1, m, b + 2);
  115. }
  116. }
  117. }
  118. return 0;
  119. }

你可能感兴趣的:(ACM,HDU)