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.
Note
- 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.
Example
No.1
Input:
1 | NumMatrix( |
sumRegion(2,1,4,3)
update(3,2,2)
sumRegion(2,1,4,3)
Output:
8
10
No.2
Input:
NumMatrix([[1]])
sumRegion(0, 0, 0, 0)
update(0, 0, -1)
sumRegion(0, 0, 0, 0)
Output:
1
-1
Code
1 | public class BinaryIndexedTree { |