Update fenwick_tree.cpp

This commit is contained in:
Anthony Wang 2020-09-08 16:42:40 -05:00
parent 105787a837
commit 2c24b07ac6

View file

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