Library/Data Structures/fenwick_tree.cpp
2020-04-20 22:22:35 -05:00

8 lines
366 B
C++

template<typename T> class fenwick_tree {
private: vector<T> FT;
public:
fenwick_tree(int N) { FT.assign(N + 1, 0); }
void update(int x, T val) { if (x > 0) for (; x < FT.size(); x += x & -x) FT[x] += val; }
T query(int x) { T ret = 0; if (x > 0) for (; x > 0; x -= x & -x) ret += FT[x]; return ret; }
T query(int x, int y) { return query(y) - query(x - 1); }
};