Advertisement
999ms

Untitled

Apr 26th, 2020
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. struct SegmentTree {
  2. vector<ll> t;
  3. vector<ll> a;
  4. SegmentTree(int n) : t(n * 4), a(n * 4) {}
  5. void Push(int v, int tl, int tr) {
  6. t[v] += a[v];
  7. if (tl + 1 < tr) {
  8. a[v * 2 + 1] += a[v];
  9. a[v * 2 + 2] += a[v];
  10. }
  11. a[v] = 0;
  12. }
  13. void Update(int v, int tl, int tr, int l, int r, ll value) {
  14. Push(v, tl, tr);
  15. if (tl >= r || tr <= l) return;
  16. if (tl >= l && tr <= r) {
  17. a[v] += value;
  18. Push(v, tl, tr);
  19. } else {
  20. int tm = (tl + tr) >> 1;
  21. Update(v * 2 + 1, tl, tm, l, r, value);
  22. Update(v * 2 + 2, tm, tr, l, r, value);
  23. t[v] = max(t[v * 2 + 1], t[v * 2 + 2]);
  24. }
  25. }
  26. ll Get(int v, int tl, int tr, int l, int r) {
  27. Push(v, tl, tr);
  28. if (tl >= r || tr <= l) return -1e18;
  29. if (tl >= l && tr <= r) return t[v];
  30. int tm = (tl + tr) >> 1;
  31. return max(Get(v * 2 + 1, tl, tm, l, r), Get(v * 2 + 2, tm, tr, l, r));
  32. }
  33. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement