bhupixb

Segment Tree from tourist

Mar 23rd, 2020
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. /**
  2. * author: tourist
  3. * created: 22.03.2020 19:11:34
  4. **/
  5. #include <bits/stdc++.h>
  6.  
  7. using namespace std;
  8.  
  9. class segtree {
  10. public:
  11. struct node {
  12. // don't forget to set default value (used for leaves)
  13. // not necessarily neutral element!
  14. long long mx = 0;
  15. long long mn = 0;
  16. long long add = 0;
  17.  
  18. void apply(int l, int r, long long v) {
  19. mn += v;
  20. mx += v;
  21. add += v;
  22. }
  23. };
  24.  
  25. node unite(const node &a, const node &b) const {
  26. node res;
  27. res.mn = min(a.mn, b.mn);
  28. res.mx = max(a.mx, b.mx);
  29. return res;
  30. }
  31.  
  32. inline void push(int x, int l, int r) {
  33. int y = (l + r) >> 1;
  34. int z = x + ((y - l + 1) << 1);
  35. if (tree[x].add != 0) {
  36. tree[x + 1].apply(l, y, tree[x].add);
  37. tree[z].apply(y + 1, r, tree[x].add);
  38. tree[x].add = 0;
  39. }
  40. }
  41.  
  42. inline void pull(int x, int z) {
  43. tree[x] = unite(tree[x + 1], tree[z]);
  44. }
  45.  
  46. int n;
  47. vector<node> tree;
  48.  
  49. void build(int x, int l, int r) {
  50. if (l == r) {
  51. return;
  52. }
  53. int y = (l + r) >> 1;
  54. int z = x + ((y - l + 1) << 1);
  55. build(x + 1, l, y);
  56. build(z, y + 1, r);
  57. pull(x, z);
  58. }
  59.  
  60. template <typename M>
  61. void build(int x, int l, int r, const vector<M> &v) {
  62. if (l == r) {
  63. tree[x].apply(l, r, v[l]);
  64. return;
  65. }
  66. int y = (l + r) >> 1;
  67. int z = x + ((y - l + 1) << 1);
  68. build(x + 1, l, y, v);
  69. build(z, y + 1, r, v);
  70. pull(x, z);
  71. }
  72.  
  73. node get(int x, int l, int r, int ll, int rr) {
  74. if (ll <= l && r <= rr) {
  75. return tree[x];
  76. }
  77. int y = (l + r) >> 1;
  78. int z = x + ((y - l + 1) << 1);
  79. push(x, l, r);
  80. node res{};
  81. if (rr <= y) {
  82. res = get(x + 1, l, y, ll, rr);
  83. } else {
  84. if (ll > y) {
  85. res = get(z, y + 1, r, ll, rr);
  86. } else {
  87. res = unite(get(x + 1, l, y, ll, rr), get(z, y + 1, r, ll, rr));
  88. }
  89. }
  90. pull(x, z);
  91. return res;
  92. }
  93.  
  94. template <typename... M>
  95. void modify(int x, int l, int r, int ll, int rr, const M&... v) {
  96. if (ll <= l && r <= rr) {
  97. tree[x].apply(l, r, v...);
  98. return;
  99. }
  100. int y = (l + r) >> 1;
  101. int z = x + ((y - l + 1) << 1);
  102. push(x, l, r);
  103. if (ll <= y) {
  104. modify(x + 1, l, y, ll, rr, v...);
  105. }
  106. if (rr > y) {
  107. modify(z, y + 1, r, ll, rr, v...);
  108. }
  109. pull(x, z);
  110. }
  111.  
  112. int find_first_knowingly(int x, int l, int r, const function<bool(const node&)> &f) {
  113. if (l == r) {
  114. return l;
  115. }
  116. push(x, l, r);
  117. int y = (l + r) >> 1;
  118. int z = x + ((y - l + 1) << 1);
  119. int res;
  120. if (f(tree[x + 1])) {
  121. res = find_first_knowingly(x + 1, l, y, f);
  122. } else {
  123. res = find_first_knowingly(z, y + 1, r, f);
  124. }
  125. pull(x, z);
  126. return res;
  127. }
  128.  
  129. int find_first(int x, int l, int r, int ll, int rr, const function<bool(const node&)> &f) {
  130. if (ll <= l && r <= rr) {
  131. if (!f(tree[x])) {
  132. return -1;
  133. }
  134. return find_first_knowingly(x, l, r, f);
  135. }
  136. push(x, l, r);
  137. int y = (l + r) >> 1;
  138. int z = x + ((y - l + 1) << 1);
  139. int res = -1;
  140. if (ll <= y) {
  141. res = find_first(x + 1, l, y, ll, rr, f);
  142. }
  143. if (rr > y && res == -1) {
  144. res = find_first(z, y + 1, r, ll, rr, f);
  145. }
  146. pull(x, z);
  147. return res;
  148. }
  149.  
  150. int find_last_knowingly(int x, int l, int r, const function<bool(const node&)> &f) {
  151. if (l == r) {
  152. return l;
  153. }
  154. push(x, l, r);
  155. int y = (l + r) >> 1;
  156. int z = x + ((y - l + 1) << 1);
  157. int res;
  158. if (f(tree[z])) {
  159. res = find_last_knowingly(z, y + 1, r, f);
  160. } else {
  161. res = find_last_knowingly(x + 1, l, y, f);
  162. }
  163. pull(x, z);
  164. return res;
  165. }
  166.  
  167. int find_last(int x, int l, int r, int ll, int rr, const function<bool(const node&)> &f) {
  168. if (ll <= l && r <= rr) {
  169. if (!f(tree[x])) {
  170. return -1;
  171. }
  172. return find_last_knowingly(x, l, r, f);
  173. }
  174. push(x, l, r);
  175. int y = (l + r) >> 1;
  176. int z = x + ((y - l + 1) << 1);
  177. int res = -1;
  178. if (rr > y) {
  179. res = find_last(z, y + 1, r, ll, rr, f);
  180. }
  181. if (ll <= y && res == -1) {
  182. res = find_last(x + 1, l, y, ll, rr, f);
  183. }
  184. pull(x, z);
  185. return res;
  186. }
  187.  
  188. segtree(int _n) : n(_n) {
  189. assert(n > 0);
  190. tree.resize(2 * n - 1);
  191. build(0, 0, n - 1);
  192. }
  193.  
  194. template <typename M>
  195. segtree(const vector<M> &v) {
  196. n = v.size();
  197. assert(n > 0);
  198. tree.resize(2 * n - 1);
  199. build(0, 0, n - 1, v);
  200. }
  201.  
  202. node get(int ll, int rr) {
  203. assert(0 <= ll && ll <= rr && rr <= n - 1);
  204. return get(0, 0, n - 1, ll, rr);
  205. }
  206.  
  207. node get(int p) {
  208. assert(0 <= p && p <= n - 1);
  209. return get(0, 0, n - 1, p, p);
  210. }
  211.  
  212. template <typename... M>
  213. void modify(int ll, int rr, const M&... v) {
  214. assert(0 <= ll && ll <= rr && rr <= n - 1);
  215. modify(0, 0, n - 1, ll, rr, v...);
  216. }
  217.  
  218. // find_first and find_last call all FALSE elements
  219. // to the left (right) of the sought position exactly once
  220.  
  221. int find_first(int ll, int rr, const function<bool(const node&)> &f) {
  222. assert(0 <= ll && ll <= rr && rr <= n - 1);
  223. return find_first(0, 0, n - 1, ll, rr, f);
  224. }
  225.  
  226. int find_last(int ll, int rr, const function<bool(const node&)> &f) {
  227. assert(0 <= ll && ll <= rr && rr <= n - 1);
  228. return find_last(0, 0, n - 1, ll, rr, f);
  229. }
  230. };
  231.  
  232. int main() {
  233. ios::sync_with_stdio(false);
  234. cin.tie(0);
  235. int tt;
  236. cin >> tt;
  237. while (tt--) {
  238. int n, q;
  239. cin >> n >> q;
  240. vector<int> b(n);
  241. for (int i = 0; i < n; i++) {
  242. cin >> b[i];
  243. }
  244. segtree st(n + 1);
  245. long long s = 0;
  246. for (int i = 0; i <= n; i++) {
  247. st.modify(i, i, s);
  248. if (i < n) {
  249. s += b[i];
  250. }
  251. }
  252. while (q--) {
  253. string foo;
  254. cin >> foo;
  255. if (foo == "Q") {
  256. int x, y;
  257. cin >> x >> y;
  258. --x; --y;
  259. cout << st.get(y + 1, n).mx - st.get(0, x).mn << '\n';
  260. } else {
  261. int i, v;
  262. cin >> i >> v;
  263. --i;
  264. st.modify(i + 1, n, v - b[i]);
  265. b[i] = v;
  266. }
  267. }
  268. }
  269. return 0;
  270. }
Add Comment
Please, Sign In to add comment