private int[] tree;//树状数组,存储某个区间(左界为其二进制下标去掉最后的1再加上1,右界为本身)的和private int[] nums;//原数组private int N;//元素数量public NumArray(int[] nums) {N&#61;nums.length;this.nums&#61;nums;this.tree&#61;new int[N&#43;1];//0位置不用for (int i &#61; 0; i 0; i -&#61; lowbit(i))ans &#43;&#61; tree[i];return ans;}//在树状数组pos位置中增加值valvoid add(int pos, int val) {//对应的父节点也要加上valfor (int i &#61; pos; i <&#61; N; i &#43;&#61; lowbit(i))tree[i] &#43;&#61; val;}public void update(int idx, int val) {
add(idx&#43;1,val-nums[idx]);nums[idx]&#61;val;} public int sumRange(int left, int right) {
return query(right&#43;1) - query(left);}