Range Sum Query - Mutable

Given an integer arraynums, find the sum of the elements between indices i and j(i≤j), inclusive.

Theupdate(i, val)function modifiesnumsby updating the element at indexitoval.

Example:

Given nums = [1, 3, 5]

sumRange(0, 2) ->9
update(1, 2)
sumRange(0, 2) ->8

Note:

  1. The array is only modifiable by the

    update

    function.

  2. You may assume the number of calls to

    update

    and

    sumRange

    function is distributed evenly.

分析

线段树sum

如果是matrix的话,就是左上右下定位。然后每次分成4段算,4个孩子。

binary index tree

初始化时候,或者duplicate 一部分update的code, 建树,但是不改变Nums

或者直接call update,但是更改新的arr[0],不能改原nums

Last updated

Was this helpful?