Range Sum Query 2D - Mutable
Given a 2D matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2). And the elements of the matrix could be changed.
You have to implement three functions:
NumMatrix(matrix)
The constructor.
sumRegion(row1, col1, row2, col2)
Return the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
update(row, col, val)
Update the element at (row, col) to
val
.
Example
Example 1:
Example 2:
Notice
The matrix is only modifiable by
update
.
You may assume the number of calls to update and sumRegion function is distributed evenly.
You may assume that row1 ≤ row2 and col1 ≤ col2.
分析
二维数组 binary index tree
注意最后是以x,y为尾的正方形,注意公式
Last updated