Advertisement
Gosunov

Untitled

Sep 22nd, 2022
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define int long long
  6.  
  7. const int maxn = 1e5 + 100;
  8. const int inf = 1e9;
  9.  
  10. struct Node {
  11. int mx;
  12. int push;
  13.  
  14. Node() { mx = -inf; push = 0; }
  15. Node(int a) { mx = a; push = 0; }
  16. };
  17.  
  18. Node combine(const Node &a, const Node &b) {
  19. return Node(max(a.mx, b.mx));
  20. }
  21.  
  22.  
  23. int a[maxn];
  24. Node t[4 * maxn];
  25.  
  26. void build(int id, int l, int r) {
  27. if (r - l == 1) {
  28. t[id].mx = a[l];
  29. return;
  30. }
  31. int m = (l + r) / 2;
  32. build(id * 2 + 1, l, m);
  33. build(id * 2 + 2, m, r);
  34. t[id] = combine(t[id * 2 + 1], t[id * 2 + 2]);
  35. }
  36.  
  37. void push(int id, int l, int r) {
  38. t[id].mx += t[id].push;
  39. if (r - l > 1) {
  40. t[2 * id + 1].push += t[id].push;
  41. t[2 * id + 2].push += t[id].push;
  42. }
  43. t[id].push = 0;
  44. }
  45.  
  46. Node get(int id, int l, int r, int lq, int rq) {
  47. push(id, l, r);
  48. if (r <= lq || l >= rq)
  49. return Node();
  50. if (lq <= l && r <= rq)
  51. return t[id];
  52. int m = (l + r) / 2;
  53. Node x = get(id * 2 + 1, l, m, lq, rq);
  54. Node y = get(id * 2 + 2, m, r, lq, rq);
  55. return combine(x, y);
  56. }
  57.  
  58. void upd(int id, int l, int r, int lq, int rq, int x) {
  59. push(id, l, r);
  60. if (r <= lq || l >= rq)
  61. return;
  62. if (lq <= l && r <= rq) {
  63. t[id].push += x;
  64. push(id, l, r);
  65. return;
  66. }
  67.  
  68. int m = (l + r) / 2;
  69. upd(id * 2 + 1, l, m, lq, rq, x);
  70. upd(id * 2 + 2, m, r, lq, rq, x);
  71.  
  72. t[id] = combine(t[id * 2 + 1], t[id * 2 + 2]);
  73. }
  74.  
  75. void solve() {
  76. int n, q;
  77. cin >> n;
  78. for (int i = 0; i < n; ++i) {
  79. cin >> a[i];
  80. }
  81. build(0, 0, n);
  82. cin >> q;
  83. int l, r, v;
  84. char c;
  85. while (q--) {
  86. cin >> c >> l >> r;
  87. l -= 1;
  88. if (c == 'a') {
  89. cin >> v;
  90. upd(0, 0, n, l, r, v);
  91. }
  92. if (c == 'm') {
  93. cout << get(0, 0, n, l, r).mx << ' ';
  94. }
  95. }
  96. cout << '\n';
  97. }
  98.  
  99. signed main() {
  100. ios_base::sync_with_stdio(0);
  101. cin.tie(0);
  102. cout.setf(ios_base::boolalpha);
  103. solve();
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement