Skip to content

Instantly share code, notes, and snippets.

@yinyanghu
Created February 11, 2017 22:24
Show Gist options
  • Save yinyanghu/3b282c12584d3f9601c220546ade3b12 to your computer and use it in GitHub Desktop.
Save yinyanghu/3b282c12584d3f9601c220546ade3b12 to your computer and use it in GitHub Desktop.
Binary Indexed Tree (a.k.a. Fenwick tree)
#define MAXBIT 1000000
#define lowbit(x) (x & -x)
struct BIT {
int bit[MAXBIT];
int M;
void clear(int M) {
memset(bit, 0, sizeof(bit));
this->M = M;
}
// require: x > 0
void add(int x, int key) {
for (; x <= M; x += lowbit(x)) bit[x] += key;
}
// require: x > 0
int query(int x) {
int ret = 0;
for (; x; x -= lowbit(x)) ret += bit[x];
return ret;
}
};
int solve() {
Left.clear(); Right.clear();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment