Skip to content

Instantly share code, notes, and snippets.

@tanitanin
Created October 7, 2014 03:13
Show Gist options
  • Save tanitanin/dac5220fd6997b74fbc7 to your computer and use it in GitHub Desktop.
Save tanitanin/dac5220fd6997b74fbc7 to your computer and use it in GitHub Desktop.
Binary Indexed Tree
#pragma once
#include <vector>
template<class T>
class BIT {
public:
explicit BIT(size_t N=1) : _bit(N) {}
void add(size_t i, T val) {
while (i < _bit.size()){
_bit[i] += val;
i += i & -i;
}
}
T sum(size_t i) {
T ret = 0;
while(i>0){
ret += _bit[i];
i &= (i - 1);
}
return ret;
}
private:
std::vector<T> _bit;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment