Numerical Array Template
Detailed explanation of the principle of tree array: https://zhuanlan.zhihu.com/p/25185969
class BIT {
int n;
int[] tree;
public BIT(int n) {
this.n = n;
this.tree = new int[n + 1];
}
// 更新tree[i]到tree[n]的值
public void update(int i, int delta) {
while(i <= n) {
tree[i] += delta;
i += lowbit(i);
}
}
// 查询tree[1]到tree[i]的区间和
public int query(int i) {
int res = 0;
while(i > 0) {
res += tree[i];
i -= lowbit(i);
}
return res;
}
// 获得最后一位1的位置,而i & (i - 1)是最后一位1置为0,其他位不变
public int lowbit(int i) {
return i & (- i);
}
}