Python | 99.88% Faster | Simple Code
We will take note of the sum when the array is initialized.\nWe update the sum when the array is updated.\nWe calculate the sum from left to right either as sum
We will take note of the sum when the array is initialized.\nWe update the sum when the array is updated.\nWe calculate the sum from left to right either as sum between those index or by subtracting the sum of 0 to left and right to end from the total sum, depending on which is smaller in length.\n\n```\nclass NumArray:\n    nums = []\n    s = 0\n    l = 0\n    \n    def __init__(self, nums: List[int]):\n        self.nums = nums\n        self.s = sum(nums)\n        self.l = len(nums)\n\n    def update(self, index: int, val: int) -> None:\n        self.s -= self.nums[index]\n        self.nums[index] = val\n        self.s += self.nums[index]\n\n    def sumRange(self, left: int, right: int) -> int:\n        if right - left > self.l // 2:\n            ans = sum(self.nums[:left]) + sum(self.nums[right + 1:])\n            return self.s - ans\n        else:\n            return sum(self.nums[left: right + 1])\n```
['Python']