[关闭]
@yuyuesheng 2019-02-17T20:51:22.000000Z 字数 1228 阅读 778

G - Circular RMQ

题意:
有两种操作,一种是环上的区间加,一种是环上的区间最值
题解:
线段树区间更新模板

  1. #include<iostream>
  2. #include <cstring>
  3. #include<string>
  4. #include <algorithm>
  5. #define lson l, mid, rt << 1
  6. #define rson mid + 1, r, rt << 1 | 1
  7. using namespace std;
  8. typedef long long ll;
  9. const int maxn = 2e5 + 5000;
  10. const ll inf = 1e18;
  11. ll mi[maxn << 2], add[maxn << 2];
  12. int n, q;
  13. void PushUp(int rt)
  14. {
  15. mi[rt] = min(mi[rt << 1], mi[rt << 1 | 1]);
  16. }
  17. void PushDown(int rt)
  18. {
  19. if (add[rt])
  20. {
  21. mi[rt << 1] += add[rt];
  22. mi[rt << 1 | 1] += add[rt];
  23. add[rt << 1] += add[rt];
  24. add[rt << 1 | 1] += add[rt];
  25. add[rt] = 0;
  26. }
  27. }
  28. void Build(int l, int r, int rt = 1)
  29. {
  30. add[rt] = 0;
  31. if (l == r)
  32. {
  33. cin >> mi[rt];
  34. return;
  35. }
  36. int mid = (l + r) >> 1;
  37. Build(lson);
  38. Build(rson);
  39. PushUp(rt);
  40. }
  41. void Update(int L, int R, int c, int l, int r, int rt)
  42. {
  43. if (L <= l && r <= R)
  44. {
  45. mi[rt] += c;
  46. add[rt] += c;
  47. return;
  48. }
  49. int mid = (l + r) >> 1;
  50. PushDown(rt);
  51. if (L <= mid)
  52. Update(L, R, c, lson);
  53. if (mid < R)
  54. Update(L, R, c, rson);
  55. PushUp(rt);
  56. }
  57. ll Query(int L, int R, int l, int r, int rt)
  58. {
  59. if (L <= l && r <= R)
  60. return mi[rt];
  61. int mid = (l + r) >> 1;
  62. ll ans = inf;
  63. PushDown(rt);
  64. if (L <= mid)
  65. ans = min(ans, Query(L, R, lson));
  66. if (mid < R)
  67. ans = min(ans, Query(L, R, rson));
  68. return ans;
  69. }
  70. int main()
  71. {
  72. cin >> n;
  73. Build(1, n, 1);
  74. cin >> q;
  75. while (q--)
  76. {
  77. int l, r, c;
  78. cin >> l >> r;
  79. l++;
  80. r++;
  81. if (getchar() == ' ')
  82. {
  83. cin >> c;
  84. if (l <= r)
  85. Update(l, r, c, 1, n, 1);
  86. else
  87. {
  88. Update(1, r, c, 1, n, 1);
  89. Update(l, n, c, 1, n, 1);
  90. }
  91. }
  92. else
  93. {
  94. if (l <= r)
  95. cout << Query(l, r, 1, n, 1) << endl;
  96. else
  97. cout << min(Query(1, r, 1, n, 1), Query(l, n, 1, n, 1)) << endl;
  98. }
  99. }
  100. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注