Stepavly

Untitled

Jul 18th, 2019
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #pragma GCC optimize("O3")
  2. #pragma GCC target("tune=native")
  3. #pragma GCC optimize("fast-math,unroll-loops")
  4.  
  5. #include <math.h>
  6. #include <algorithm>
  7. #include <set>
  8. #include <iostream>
  9. #include <vector>
  10. #include <queue>
  11. #include <map>
  12. #include <string>
  13. #include <time.h>
  14. #include <cassert>
  15. #include <functional>
  16. #include <memory.h>
  17. #include <stack>
  18. #include <bitset>
  19. #include <unordered_map>
  20. #include <unordered_set>
  21. #include <random>
  22. #include <chrono>
  23. #include <complex>
  24. #include <fstream>
  25. #include <climits>
  26. using namespace std;
  27.  
  28. typedef unsigned long long ull;
  29. typedef long long ll;
  30. typedef unsigned u;
  31. typedef long double ld;
  32. typedef vector<vector<int>> vvi;
  33. typedef unsigned char uc;
  34. typedef unsigned short us;
  35. typedef complex<double> cd;
  36.  
  37. #define INF 1000000000
  38. #define LLINF 1000000000000000000LL
  39. #define EPS 1e-9l
  40. #define pii pair<int, int>
  41.  
  42. const int DEBUG = 0;
  43.  
  44. #ifdef LOCAL
  45. mt19937 gen(228);
  46. #else
  47. mt19937 gen((u)chrono::high_resolution_clock::now().time_since_epoch().count());
  48. #endif
  49.  
  50. #pragma comment(linker, "/STACK:76777216")
  51.  
  52. const int N = (int)1e5 + 100;
  53.  
  54. struct node
  55. {
  56. ll mx, add;
  57. };
  58.  
  59. node tree[4 * N];
  60. int a[N];
  61. int n;
  62.  
  63. void build(int v, int tl, int tr)
  64. {
  65. if (tl > tr)
  66. return;
  67.  
  68. if (tl == tr)
  69. {
  70. tree[v].mx = a[tl];
  71. return;
  72. }
  73.  
  74. int tm = (tl + tr) / 2;
  75.  
  76. build(2 * v, tl, tm);
  77. build(2 * v + 1, tm + 1, tr);
  78.  
  79. tree[v].mx = max(tree[2 * v].mx, tree[2 * v + 1].mx);
  80. }
  81.  
  82. void push(int v)
  83. {
  84. if (v < 2 * N)
  85. {
  86. tree[2 * v].mx += tree[v].add;
  87. tree[2 * v + 1].mx += tree[v].add;
  88.  
  89. tree[2 * v].add += tree[v].add;
  90. tree[2 * v + 1].add += tree[v].add;
  91. }
  92.  
  93. tree[v].add = 0;
  94. }
  95.  
  96. void modify(int v, int tl, int tr, int l, int r, int x)
  97. {
  98. push(v);
  99.  
  100. if (tr < l || tl > r)
  101. return;
  102.  
  103. if (l <= tl && tr <= r)
  104. {
  105. tree[v].add += x;
  106. tree[v].mx += x;
  107. push(v);
  108. return;
  109. }
  110.  
  111. int tm = (tl + tr) / 2;
  112.  
  113. modify(2 * v, tl, tm, l, r, x);
  114. modify(2 * v + 1, tm + 1, tr, l, r, x);
  115.  
  116. tree[v].mx = max(tree[2 * v].mx, tree[2 * v + 1].mx);
  117. }
  118.  
  119. ll getMax(int v, int tl, int tr, int l, int r)
  120. {
  121. push(v);
  122.  
  123. if (tr < l || tl > r)
  124. return -LLINF;
  125.  
  126. if (l <= tl && tr <= r)
  127. return tree[v].mx;
  128.  
  129. int tm = (tl + tr) / 2;
  130.  
  131. return max(getMax(2 * v, tl, tm, l, r), getMax(2 * v + 1, tm + 1, tr, l, r));
  132. }
  133.  
  134. int main()
  135. {
  136. ios_base::sync_with_stdio(0);
  137. cin.tie(0);
  138. cout.setf(cout.fixed);
  139. cout.precision(12);
  140. auto START_TIME = chrono::high_resolution_clock::now();
  141.  
  142. cin >> n;
  143.  
  144. for (int i = 0; i < n; i++)
  145. {
  146. cin >> a[i];
  147. }
  148.  
  149. build(1, 0, n - 1);
  150.  
  151. int q;
  152. cin >> q;
  153.  
  154. while (q--)
  155. {
  156. char c;
  157. cin >> c;
  158.  
  159. if (c == 'a')
  160. {
  161. int l, r, x;
  162. cin >> l >> r >> x;
  163.  
  164. --l; --r;
  165.  
  166. modify(1, 0, n - 1, l, r, x);
  167. }
  168. else
  169. {
  170. int l, r;
  171. cin >> l >> r;
  172.  
  173. --l; --r;
  174.  
  175. cout << getMax(1, 0, n - 1, l, r) << " ";
  176. }
  177. }
  178.  
  179. #ifdef LOCAL
  180. cerr.precision(3);
  181. cerr << "\nWorking time: " << chrono::duration<double>(chrono::high_resolution_clock::now() - START_TIME).count() << " sec.";
  182. #endif
  183.  
  184. return 0;
  185. }
Advertisement
Add Comment
Please, Sign In to add comment