The array is only modifiable by the update function. You may assume the number of calls to update and sumRange function is distributed evenly.
Solution
publicclassNumArray {int[] nums;int[] BIT;int n;publicNumArray(int[] nums) {this.nums= nums; n =nums.length; BIT =newint[n +1];for (int i =0; i <nums.length; i++) {init(i, nums[i]); } }privatevoidinit(int i,int val) { i++;while (i <= n) {BIT[i] += val; i += (i &-i); } }voidupdate(int i,int val) {int diff = val - nums[i]; nums[i] = val;init(i, diff); }privateintsum(int i) { i++;int sum =0;while (i >0) { sum +=BIT[i]; i -= (i &-i); }return sum; }publicintsumRange(int i,int j) {returnsum(j)-sum(i -1); }}// Your NumArray object will be instantiated and called as such:// NumArray numArray = new NumArray(nums);// numArray.sumRange(0, 1);// numArray.update(1, 10);// numArray.sumRange(1, 2);