例题 https://leetcode-cn.com/problems/range-sum-query-2d-mutable/
一般的树状数组tree[i]记录的是以i为右端点,长度为lowbit(i)的区间和,实现单点修改区间查询。
树状数组也可以实现矩阵上的单点修改区间查询
int lowbit(int x) { return x & -x; } void modify(int x0, int y0, int val) { for (int x = x0; x <= n; x += lowbit(x)) { for (int y = y0; y <= n; y += lowbit(y)) { tree[x][y] += val; } } } long query(int row, int col) { long ret = 0; for (int x = row; x > 0; x -= lowbit(x)) { for (int y = col; y > 0; y -= lowbit(y)) { ret += tree[x][y]; } } return ret; }