树状数组数据结构
class BIT{
public:
vector<int> tree;
int n;
BIT(int _n): n(_n), tree(_n+1) {}
// 注意处理x为0时的异常, 计算0的补码的反码会有问题 +0, -0等...., 因此上面tree数组容量加1,起始位置注意从1开始
static int lowBit(int x) {
return x & (-x);
}
int query(int x) {
int ret = 0;
while (x > 0) {
ret += tree[x];
x -= lowBit(x);
}
return ret;
}
void update(int x, int k) {
while (x <= n) {
tree[x] + =k;
x += lowBit(x);
}
}
};