publicBinaryIndexedTree(int[] nums){ int n = nums.length; bits = newint[n+1]; System.arraycopy(nums, 0, bits, 1, n); for (int i=1; i < n; i++) { int nxt = i + (i & (-i)); if (nxt <= n) bits[nxt] += bits[i]; } }
publicvoidupdate(int index, int delta){ int i = index + 1; while (i < bits.length) { bits[i] += delta; i += (i & -i); } }
// [0, index) publicintprefixSum(int index){ int i = index; int res = 0; while (i > 0) { res += bits[i]; i -= (i & -i); } return res; }