Advertisement
nasarouf

Range tree

Mar 14th, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. // range tree
  2. // nasarouf@cs.ubc.ca
  3.  
  4. #define BASE (1<<17)
  5. #define MAX ((BASE)*2)
  6. template<class INT>
  7. class BIT{
  8.     INT bit[MAX];
  9. public:
  10.     inline INT sum(int pos){
  11.         INT ret=0,b=BASE;
  12.         if (pos==BASE-1) return bit[1];
  13.         for (pos+=b; pos && pos>=b; )
  14.             if (pos%2==0) ret+=bit[pos--];
  15.             else{ pos/=2; b/=2; }
  16.         return ret;
  17.     }
  18.     inline const INT& get(int pos){ return bit[pos+BASE]; }
  19.     inline void add(int pos, INT c){
  20.         for (pos+=BASE; pos; pos/=2) bit[pos]+=c;
  21.     }
  22.     inline const INT& operator[](int pos){ return get(pos); }
  23. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement