Range Sum Query - Mutable
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 right inclusive where left <= right. Implement the NumArray class with update(index, val) and sumRange(left, right) methods.

30:00

Range Sum Query - Mutable
medium
Topics
Companies

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 right inclusive where left <= right. Implement the NumArray class with update(index, val) and sumRange(left, right) methods.

Example 1:
Input: {"operations":["NumArray","sumRange","update","sumRange"],"values":[[[1,3,5]],[0,2],[1,2],[0,2]]}
Output: [null,9,null,8]
Constraints:
  • 1nums.length3×1041 \leq \text{nums.length} \leq 3 \times 10^4

  • 100nums[i]100-100 \leq \text{nums}[i] \leq 100

  • 0index<nums.length0 \leq \text{index} < \text{nums.length}

  • 100val100-100 \leq \text{val} \leq 100

  • 0leftright<nums.length0 \leq \text{left} \leq \text{right} < \text{nums.length}

  • At most 3×1043 \times 10^4 calls in total to update and sumRange.

Input
arr ={"operations":["NumArray","sumRange","update","sumRange"],"values":[[[1,3,5]],[0,2],[1,2],[0,2]]}

Built segment tree from array [1, 3, 5]

Array
[0]

1

[1]

3

[2]

5

Operation: construct
Segment Tree
9
8
1
3
5
Variables
No variables to display
DepthFunction Call
Stack empty
0/3