nasarouf

Range tree

Mar 14th, 2015
164
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.  
  3. #define BASE (1<<17)
  4. #define MAX ((BASE)*2)
  5. template<class INT>
  6. class BIT{
  7.     INT bit[MAX];
  8. public:
  9.     inline INT sum(int pos){
  10.         INT ret=0,b=BASE;
  11.         if (pos==BASE-1) return bit[1];
  12.         for (pos+=b; pos && pos>=b; )
  13.             if (pos%2==0) ret+=bit[pos--];
  14.             else{ pos/=2; b/=2; }
  15.         return ret;
  16.     }
  17.     inline const INT& get(int pos){ return bit[pos+BASE]; }
  18.     inline void add(int pos, INT c){
  19.         for (pos+=BASE; pos; pos/=2) bit[pos]+=c;
  20.     }
  21.     inline const INT& operator[](int pos){ return get(pos); }
  22. };
Advertisement
Add Comment
Please, Sign In to add comment