Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
The update(i, val) function modifies nums by updating the element at index i to val.
Example:
Given nums = [1, 3, 5]
sumRange(0, 2) -> 9
update(1, 2)
sumRange(0, 2) -> 8
Note:
       The array is only modifiable by the update function.
       You may assume the number of calls to update and sumRange function is distributed evenly.
from math import sqrt
 class NumArray(object):
 
     def __init__(self, nums):
         """
         :type nums: List[int]
         """
         if nums:
             k=int(sqrt(len(nums)))
             add,i=[],0
             while i<=len(nums)-k:
                 add.append(sum(nums[i:i+k]))
                 i+=k
             if i!=len(nums):
                 add.append(sum(nums[i:]))
             self.nums,self.add,self.k=nums,add,k
         
 
     def update(self, i, val):
         """
         :type i: int
         :type val: int
         :rtype: void
         """
         self.add[i//self.k]+=val-self.nums[i]
         self.nums[i]=val
         
         
 
     def sumRange(self, i, j):
         """
         :type i: int
         :type j: int
         :rtype: int
         """
         def func(i):
             return sum(self.add[:i//self.k])+sum(self.nums[(i//self.k)*self.k:i+1]) if i>=0 else 0
         return func(j)-func(i-1)
         
 
 
 # Your NumArray object will be instantiated and called as such:
 # obj = NumArray(nums)
 # obj.update(i,val)
 # param_2 = obj.sumRange(i,j)
from math import sqrt
 class NumArray(object):
 
     def __init__(self, nums):
         """
         :type nums: List[int]
         """
         if nums:
             k=int(sqrt(len(nums)))
             add,i=[],0
             while i<=len(nums)-k:
                 add.append(sum(nums[i:i+k]))
                 i+=k
             if i!=len(nums):
                 add.append(sum(nums[i:]))
             self.nums,self.add,self.k=nums,add,k
         
 
     def update(self, i, val):
         """
         :type i: int
         :type val: int
         :rtype: void
         """
         self.add[i//self.k]+=val-self.nums[i]
         self.nums[i]=val
         
         
 
     def sumRange(self, i, j):
         """
         :type i: int
         :type j: int
         :rtype: int
         """
         def func(i):
             return sum(self.add[:i//self.k])+sum(self.nums[(i//self.k)*self.k:i+1]) if i>=0 else 0
         return func(j)-func(i-1)
         
 
 
 # Your NumArray object will be instantiated and called as such:
 # obj = NumArray(nums)
 # obj.update(i,val)
 # param_2 = obj.sumRange(i,j)