Created
February 11, 2017 22:24
-
-
Save yinyanghu/3b282c12584d3f9601c220546ade3b12 to your computer and use it in GitHub Desktop.
Binary Indexed Tree (a.k.a. Fenwick tree)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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