# Question

Formatted question description: https://leetcode.ca/all/308.html

 308. Range Sum Query 2D - Mutable

Given a 2D matrix matrix, find the sum of the elements inside the rectangle
defined by its upper left corner (row1, col1) and lower right corner (row2, col2).

The above rectangle (with the red border) is defined by (row1, col1) = (2, 1)
and (row2, col2) = (4, 3), which contains sum = 8.

Example:

Given matrix =
[
[3, 0, 1, 4, 2],
[5, 6, 3, 2, 1],
[1, 2, 0, 1, 5],
[4, 1, 0, 1, 7],
[1, 0, 3, 0, 5]
]

sumRegion(2, 1, 4, 3) -> 8
update(3, 2, 2)
sumRegion(2, 1, 4, 3) -> 10

Note:

The matrix is only modifiable by the update function.
You may assume the number of calls to update and sumRegion function is distributed evenly.
You may assume that row1 ≤ row2 and col1 ≤ col2.



# Algorithm

In the class NumMatrix, maintain the original matrix matrix and maintain another 2D array rowSums that has the same size as matrix and stores the sums of the elements to the left in the same row. Concretely, rowSums[i][j] is the sum of all elements from matrix[i][0] to matrix[i][j]. Also maintain rows and columns of matrix as well.

For the constructor, initialize the original matrix matrix, the 2D array rowSums, and rows and columns.

For update, calculate difference = val - matrix[row][column], update matrix[row][col] = val, and for col <= i < columns, update rowSums[row][i] += difference.

For sumRegion, if row1 > row2 or col1 > col2, then the region is invalid, so return 0. Otherwise, for each row from row1 to row2, calculate the sum of elements from column col1 to column col2. The sum in row row and columns range [col1, col2] is calculated as rowSums[row][col2] for col1 == 0, or rowSums[row][col2] - rowSums[row][col1 - 1] for col1 > 0.

class NumMatrix {
int rows;
int columns;
int[][] matrix;
int[][] rowSums;

public NumMatrix(int[][] matrix) {
this.matrix = matrix;
if (matrix == null || matrix.length == 0)
return;
rows = matrix.length;
columns = matrix[0].length;
rowSums = new int[rows][columns];
for (int i = 0; i < rows; i++) {
rowSums[i][0] = matrix[i][0];
for (int j = 1; j < columns; j++)
rowSums[i][j] = rowSums[i][j - 1] + matrix[i][j];
}
}

public void update(int row, int col, int val) {
int difference = val - matrix[row][col];
matrix[row][col] = val;
for (int i = col; i < columns; i++)
rowSums[row][i] += difference;
}

public int sumRegion(int row1, int col1, int row2, int col2) {
if (row1 > row2 || col1 > col2)
return 0;
int regionSum = 0;
for (int i = row1; i <= row2; i++)
regionSum += sumRow(i, col1, col2);
return regionSum;
}

private int sumRow(int row, int col1, int col2) {
int rowSum = rowSums[row][col2];
if (col1 > 0)
rowSum -= rowSums[row][col1 - 1];
return rowSum;
}
}

/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* obj.update(row,col,val);
* int param_2 = obj.sumRegion(row1,col1,row2,col2);
*/


The tree array Binary Indexed Tree, the complexity of query and modification is O(logn)