Java || Segment Tree Solution
\tclass NumArray {\n\t\tint[] st, arr;\n\t\tint len;\n\n\t\tpublic NumArray(int[] nums) {\n\t\t\tlen = nums.length;\n\t\t\tif(len == 0) return;\n\n\t\t\tarr = n
\tclass NumArray {\n\t\tint[] st, arr;\n\t\tint len;\n\n\t\tpublic NumArray(int[] nums) {\n\t\t\tlen = nums.length;\n\t\t\tif(len == 0) return;\n\n\t\t\tarr = nums.clone();\n\n\t\t\tst = new int[2*2*len];\n\t\t\tbuildTree(nums, 0, len-1, 0);\n\t\t}\n\n\t\tpublic int buildTree(int[] nums, int s, int e, int pos){\n\t\t\tif(s == e){\n\t\t\t\tst[pos] = nums[s];\n\t\t\t\treturn nums[s];\n\t\t\t}\n\n\t\t\tint mid = (s+e)/2;\n\t\t\tst[pos] = buildTree(nums, s, mid, 2*pos+1) + buildTree(nums, mid+1, e, 2*pos+2);\n\n\t\t\treturn st[pos];\n\t\t}\n\n\t\tpublic void update(int i, int val) {\n\t\t\tint diff = val - arr[i];\n\t\t\tarr[i] = val;\n\t\t\tupdateTree(i, diff, 0, len-1, 0);\n\t\t}\n\n\t\tpublic void updateTree(int index, int val, int ss, int se, int pos){\n\t\t\tif(index<ss || index>se) return;\n\n\t\t\tst[pos] = st[pos] + val;\n\n\t\t\tif(se != ss) {\n\t\t\t\tint mid = (ss+se)/2;\n\t\t\t\tupdateTree(index, val, ss, mid, 2*pos+1);\n\t\t\t\tupdateTree(index, val, mid+1, se, 2*pos+2);\n\t\t\t}\n\t\t}\n\n\t\tpublic int sumRange(int i, int j) {\n\t\t\treturn calculateSum(i, j, 0, len-1, 0);\n\t\t}\n\n\t\tpublic int calculateSum(int left, int right, int ss, int se, int pos){\n\t\t\tif(left<=ss && right>=se) return st[pos];\n\t\t\telse if(right<ss || left>se) return 0;\n\n\t\t\tint mid = (ss+se)/2;\n\t\t\treturn calculateSum(left, right, ss, mid, 2*pos+1) + calculateSum(left, right, mid+1, se, 2*pos+2);\n\t\t}\n\t}
[]