

一维
public static class indexTree {//辅助数组private int[] tree;private int N;public indexTree(int size) {N = size;tree = new int[N + 1];}// 0 ~ index 范围上的累加和public int sum(int index) {int res = 0;while (index > 0) {res += tree[index];index -= index & -index;}return res;}// index位置的数,想加上d,还有哪些位置也要都加dpublic void add(int index, int d) {while (index <= N) {tree[index] += d;index += index & -index;}}}
