diff --git a/Data Structures/fenwick_tree.cpp b/Data Structures/fenwick_tree.cpp new file mode 100644 index 0000000..dd04aea --- /dev/null +++ b/Data Structures/fenwick_tree.cpp @@ -0,0 +1,11 @@ +#include +using namespace std; + +class fenwick_tree { +private: vector FT; +public: + fenwick_tree(int N) { FT.assign(N + 1, 0); } + void update(int x, int val) { for (; x < FT.size(); x += x & -x) FT[x] += val; } + int query(int x) { int ret = 0; for (; x > 0; x -= x & -x) ret += FT[x]; return ret; } + int query(int x, int y) { return query(y) - (x == 1 ? 0 : query(x - 1)); } +};