二维树状数组:
add: (x,y)位置的元素值加d;
sum:查询(1,1)~(x,y)子矩阵元素和;
注意树状数组的元素,下标都要从1开始。
void add(int x, int y, int d) { int i, j; for(i = x; i < N; i += lowbit(i)) for(j = y; j < N; j += lowbit(j)) mat[i][j] += d; } LL sum(int x, int y) { LL res = 0; int i, j; for(i = x; i > 0; i -= lowbit(i)) for(j = y; j > 0; j -= lowbit(j)) res += mat[i][j]; return res; }