daily pastebin goal
23%
SHARE
TWEET

Segment Tree (Point-Range)

keverman Mar 25th, 2018 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void update(int pos, int val)
  2. {
  3.     val -= T[(pos += N)];
  4.  
  5.     do{
  6.         T[pos] += val;
  7.     }while((pos /= 2) > 0);
  8. }
  9.  
  10. lld query(int a, int b, int n = 1, int lhs = 0, int rhs = N - 1)
  11. {
  12.     if(lhs >= a && rhs <= b) return T[n];
  13.  
  14.     int mid = ((lhs + rhs) >> 1);
  15.  
  16.     return ((mid < a ? 0 : query(a, b, l(n), lhs, mid)) +
  17.            (mid + 1 > b ? 0 : query(a, b, r(n), mid + 1, rhs)));
  18. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top