PKU 4047 - Garden(线段树 + 区间修改)

题意

给出三个操作。单点更新,交换两个点,区间询问。

询问的时候要求输出区间内k个值的最大和。

思路

一开始想着维护k个值的最大和,没思路。

后来想到可以把k个值压缩成一个点,这样就变成普通的线段树了。

这样的话,当修改一个点时,影响的新的区间是l = max(1, n - k + 1), r = min(N - k + 1, n).

交换,单点更新都可以看成某个区间内增加一个值。

代码

  
  
  
  
  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 SZ(x) (int)x.size()
  21. #define Lowbit(x) ((x) & (-x))
  22. #define MP(a, b) make_pair(a, b)
  23. #define MS(arr, num) memset(arr, num, sizeof(arr))
  24. #define PB push_back
  25. #define F first
  26. #define S second
  27. #define ROP freopen("input.txt", "r", stdin);
  28. #define MID(a, b) (a + ((b - a) >> 1))
  29. #define LC rt << 1, l, mid
  30. #define RC rt << 1|1, mid + 1, r
  31. #define LRT rt << 1
  32. #define RRT rt << 1|1
  33. #define BitCount(x) __builtin_popcount(x)
  34. const double PI = acos(-1.0);
  35. const int INF = 0x3f3f3f3f;
  36. using namespace std;
  37. const LL MAXN = 2e5 + 10;
  38. const int MOD = 20071027;
  39. typedef pair<int, int> pii;
  40. typedef vector<int>::iterator viti;
  41. typedef vector<pii>::iterator vitii;
  42. int num[MAXN], n, m, k;
  43. vector<int> sum;
  44. struct SEGTREE
  45. {
  46. int add, nmax;
  47. }segt[MAXN << 2];
  48. void PushUp(int rt)
  49. {
  50. segt[rt].nmax = max(segt[LRT].nmax, segt[RRT].nmax);
  51. }
  52. void PushDown(int rt)
  53. {
  54. const int &add = segt[rt].add;
  55. segt[LRT].nmax += add;
  56. segt[RRT].nmax += add;
  57. segt[LRT].add += add;
  58. segt[RRT].add += add;
  59. segt[rt].add = 0;
  60. }
  61. int Query(int rt, int l, int r, int L, int R)
  62. {
  63. if (L <= l && r <= R) return segt[rt].nmax;
  64. if (segt[rt].add) PushDown(rt);
  65. int mid = MID(l, r);
  66. int ret = -INF;
  67. if (L <= mid) ret = max(ret, Query(LC, L, R));
  68. if (R > mid) ret = max(ret, Query(RC, L, R));
  69. return ret;
  70. }
  71. void Update(int rt, int l, int r, int L, int R, int val)
  72. {
  73. if (L <= l && r <= R)
  74. {
  75. segt[rt].add += val;
  76. segt[rt].nmax += val;
  77. return;
  78. }
  79. if (segt[rt].add) PushDown(rt);
  80. int mid = MID(l, r);
  81. if (L <= mid) Update(LC, L, R, val);
  82. if (R > mid) Update(RC, L, R, val);
  83. PushUp(rt);
  84. }
  85. void Build(int rt, int l, int r)
  86. {
  87. segt[rt].add = 0;
  88. if (l == r)
  89. {
  90. segt[rt].nmax = sum[l];
  91. return;
  92. }
  93. int mid = MID(l, r);
  94. Build(LC); Build(RC);
  95. PushUp(rt);
  96. }
  97. void Solve()
  98. {
  99. const int N = n - k + 1;
  100. int a, b, c;
  101. while (m--)
  102. {
  103. scanf("%d%d%d", &a, &b, &c);
  104. if (a == 0)
  105. {
  106. int val = c - num[b];
  107. num[b] = c;
  108. Update(1, 1, N, max(1, b - k + 1), min(b, N), val);
  109. }
  110. else if (a == 1)
  111. {
  112. int val = num[c] - num[b];
  113. Update(1, 1, N, max(1, b - k + 1), min(b, N), val);
  114. val = num[b] - num[c];
  115. swap(num[b], num[c]);
  116. Update(1, 1, N, max(1, c - k + 1), min(c, N), val);
  117. }
  118. else printf("%d\n", Query(1, 1, N, b, c - k + 1));
  119. }
  120. }
  121. int main()
  122. {
  123. //ROP;
  124. int T, i, j;
  125. scanf("%d", &T);
  126. while (T--)
  127. {
  128. sum.clear(); sum.PB(9999); sum.PB(0);
  129. scanf("%d%d%d", &n, &m, &k);
  130. int temp = 0;
  131. for (i = 1; i <= n; i++)
  132. {
  133. scanf("%d", &num[i]);
  134. temp += num[i];
  135. if (i > k)
  136. {
  137. temp -= num[i - k];
  138. sum.PB(temp);
  139. }
  140. else sum.back() = temp;
  141. }
  142. Build(1, 1, n - k + 1);
  143. Solve();
  144. }
  145. return 0;
  146. }

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