Create fenwick_tree.cpp

This commit is contained in:
Anthony Wang 2019-07-16 12:06:14 -07:00 committed by GitHub
parent aeffb40351
commit 1f0268e4f4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,11 @@
#include <vector>
using namespace std;
class fenwick_tree {
private: vector<int> 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)); }
};