Given an integer array nums, handle multiple queries of the following types:
Update the value of an element in nums.
Calculate the sum of the elements of nums between indices left and rightinclusive where left <= right.
Implement the NumArray class:
NumArray(int[] nums) Initializes the object with the integer array nums.
void update(int index, int val)Updates the value of nums[index] to be val.
int sumRange(int left, int right) Returns the sum of the elements of nums between indices left and rightinclusive (i.e. nums[left] + nums[left + 1] + ... + nums[right]).
classBinaryIndexedTree:__slots__=["n","c"]def__init__(self,n):self.n=nself.c=[0]*(n+1)defupdate(self,x:int,delta:int):whilex<=self.n:self.c[x]+=deltax+=x&-xdefquery(self,x:int)->int:s=0whilex>0:s+=self.c[x]x-=x&-xreturnsclassNumArray:__slots__=["tree"]def__init__(self,nums:List[int]):self.tree=BinaryIndexedTree(len(nums))fori,vinenumerate(nums,1):self.tree.update(i,v)defupdate(self,index:int,val:int)->None:prev=self.sumRange(index,index)self.tree.update(index+1,val-prev)defsumRange(self,left:int,right:int)->int:returnself.tree.query(right+1)-self.tree.query(left)# Your NumArray object will be instantiated and called as such:# obj = NumArray(nums)# obj.update(index,val)# param_2 = obj.sumRange(left,right)
classBinaryIndexedTree{privateintn;privateint[]c;publicBinaryIndexedTree(intn){this.n=n;c=newint[n+1];}publicvoidupdate(intx,intdelta){while(x<=n){c[x]+=delta;x+=x&-x;}}publicintquery(intx){ints=0;while(x>0){s+=c[x];x-=x&-x;}returns;}}classNumArray{privateBinaryIndexedTreetree;publicNumArray(int[]nums){intn=nums.length;tree=newBinaryIndexedTree(n);for(inti=0;i<n;++i){tree.update(i+1,nums[i]);}}publicvoidupdate(intindex,intval){intprev=sumRange(index,index);tree.update(index+1,val-prev);}publicintsumRange(intleft,intright){returntree.query(right+1)-tree.query(left);}}/** * Your NumArray object will be instantiated and called as such: * NumArray obj = new NumArray(nums); * obj.update(index,val); * int param_2 = obj.sumRange(left,right); */