Combothermal

Untitled

Jun 25th, 2024
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. const ll identity = 0;
  2. const ll SZ = 131072;
  3.  
  4. ll sum[2*SZ], lazy[2*SZ];
  5.  
  6. ll combine(ll A, ll B) {
  7. return A+B;
  8. }
  9.  
  10. ll combineUpd(ll A, ll B) {
  11. return A+B;
  12. }
  13.  
  14. void push(int index, ll L, ll R) {
  15. sum[index] = combineUpd(sum[index], lazy[index]);
  16. if (L != R) lazy[2*index] = combineUpd(lazy[2*index], lazy[index]), lazy[2*index+1] = combineUpd(lazy[2*index+1], lazy[index]);
  17. lazy[index] = identity;
  18. }
  19.  
  20. void pull(int index) {
  21. sum[index] = combine(sum[2*index], sum[2*index+1]);
  22. }
  23.  
  24. ll query(int lo, int hi, int index = 1, ll L = 0, ll R = SZ-1) {
  25. push(index, L, R);
  26. if (lo > R || L > hi) return identity;
  27. if (lo <= L && R <= hi) return sum[index];
  28.  
  29. int M = (L+R) / 2;
  30. return combine(query(lo, hi, 2*index, L, M), query(lo, hi, 2*index+1, M+1, R));
  31. }
  32.  
  33. void update(int lo, int hi, ll increase, int index = 1, ll L = 0, ll R = SZ-1) {
  34. push(index, L, R);
  35. if (hi < L || R < lo) return;
  36. if (lo <= L && R <= hi) {
  37. lazy[index] = increase;
  38. push(index, L, R);
  39. return;
  40. }
  41.  
  42. int M = (L+R) / 2;
  43. update(lo, hi, increase, 2*index, L, M); update(lo, hi, increase, 2*index+1, M+1, R);
  44. pull(index);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment