Given a 2D matrix matrix, handle multiple queries of the following type:


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


Implement the NumMatrix class:


	NumMatrix(int[][] matrix) Initializes the object with the integer matrix matrix.
	int sumRegion(int row1, int col1, int row2, int col2) Returns the sum of the elements of matrix inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).


You must design an algorithm where sumRegion works on O(1) time complexity.

&nbsp;
Example 1:

Input
["NumMatrix", "sumRegion", "sumRegion", "sumRegion"]
[[[[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]]], [2, 1, 4, 3], [1, 1, 2, 2], [1, 2, 2, 4]]
Output
[null, 8, 11, 12]

Explanation
NumMatrix numMatrix = new NumMatrix([[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]]);
numMatrix.sumRegion(2, 1, 4, 3); // return 8 (i.e sum of the red rectangle)
numMatrix.sumRegion(1, 1, 2, 2); // return 11 (i.e sum of the green rectangle)
numMatrix.sumRegion(1, 2, 2, 4); // return 12 (i.e sum of the blue rectangle)


&nbsp;
Constraints:


	m == matrix.length
	n == matrix[i].length
	1 &lt;= m, n &lt;= 200
	-104 &lt;= matrix[i][j] &lt;= 104
	0 &lt;= row1 &lt;= row2 &lt; m
	0 &lt;= col1 &lt;= col2 &lt; n
	At most 104 calls will be made to sumRegion.


class NumMatrix:

    def __init__(self, matrix: List[List[int]]):
        m, n = len(matrix), len(matrix[0])
        # Understand why we need to create a new matrix with extra one row/column
        self.sum = [[0 for row in range(n + 1)] for column in range(m + 1)]
        for r in range(1, m + 1):
            for c in range(1, n + 1):
                self.sum[r][c] = self.sum[r - 1][c] + self.sum[r][c - 1] - self.sum[r - 1][c - 1] + matrix[r - 1][c - 1]

    def sumRegion(self, row1: int, col1: int, row2: int, col2: int) -> int:
        r1, c1, r2, c2 = row1 + 1, col1 + 1, row2 + 1, col2 + 1
        return self.sum[r2][c2] - self.sum[r1 - 1][c2] - self.sum[r2][c1 - 1] + self.sum[r1 - 1][c1 - 1]

# Your NumMatrix object will be instantiated and called as such:
# obj = NumMatrix(matrix)
# param_1 = obj.sumRegion(row1,col1,row2,col2)
class NumMatrix {

    int mat[][];
    public NumMatrix(int[][] matrix) {
        mat = matrix;
        int rows = mat.length;
        int cols = mat[0].length;
        
        for(int i = 0; i< rows; i++)
        {
            for(int j = 0; j < cols; j++) 
            {
                if(i > 0) mat[i][j] += mat[i-1][j];
                if(j > 0) mat[i][j] += mat[i][j-1];
                if(i>0 && j > 0) mat[i][j] -= mat[i-1][j-1];
            }
        }
        
    }
    
    public int sumRegion(int row1, int col1, int row2, int col2) {
        int res = mat[row2][col2];
        if(row1 > 0) res -= mat[row1-1][col2];
        if(col1 > 0) res -= mat[row2][col1-1];
        if(row1> 0 && col1 > 0) res += mat[row1-1][col1-1];
        
        return res;
    }
}
//Using row prefix sum O(m)
class NumMatrix {
    vector<vector<int>> prefix;
public:
    NumMatrix(vector<vector<int>>& matrix) {
        int m = matrix.size(), n = matrix[0].size();
        for(int i = 0;i<m;i++){
            vector<int> row(n);
            row[0] = matrix[i][0];
            for(int j = 1;j<n;j++){
                row[j] = row[j-1] + matrix[i][j];
            }
            prefix.push_back(row);
        }
    }
    
    int sumRegion(int row1, int col1, int row2, int col2) {
        int sum = 0;
        for(int i = row1;i<=row2;i++){
            sum += prefix[i][col2];
            if(col1>0) sum -= prefix[i][col1-1];
        }
        return sum;
    }
};

//Using mat prefix sum O(1)
class NumMatrix {
    vector<vector<int>> prefix;
public:
    NumMatrix(vector<vector<int>>& matrix) {
        int m = matrix.size(), n = matrix[0].size();
        prefix = vector<vector<int>>(m+1,vector<int>(n+1,0));
        prefix[1][1] = matrix[0][0];
        for(int i = 1;i<=m;i++){
            for(int j = 1;j<=n;j++){
                prefix[i][j] = prefix[i-1][j] + prefix[i][j-1] + matrix[i-1][j-1] - prefix[i-1][j-1];
            }
        }
    }
    
    int sumRegion(int row1, int col1, int row2, int col2) {
        int sum = 0;
        sum += prefix[row2+1][col2+1];
        sum -= prefix[row1][col2+1];
        sum -= prefix[row2+1][col1];
        sum += prefix[row1][col1];
        return sum;
    }
};
/**
 * @param {number[][]} matrix
 */
var NumMatrix = function(matrix) {
    const n = matrix.length, m = matrix[0].length;
    // n * m filled with 0
    this.prefix = Array.from({ length: n}, (_, i) => {
        return new Array(m).fill(0);
    });
    const prefix = this.prefix;
    // precompute
    for(let i = 0; i < m; i++) {
        if(i == 0) prefix[0][i] = matrix[0][i];
        else prefix[0][i] = prefix[0][i-1] + matrix[0][i];
    }
    for(let i = 0; i < n; i++) {
        if(i == 0) continue;
        else prefix[i][0] = prefix[i-1][0] + matrix[i][0];
    }
    
    for(let i = 1; i < n; i++) {
        for(let j = 1; j < m; j++) {
            prefix[i][j] = prefix[i-1][j] + prefix[i][j-1] - prefix[i-1][j-1] + matrix[i][j];
        }
    }
};

/** 
 * @param {number} row1 
 * @param {number} col1 
 * @param {number} row2 
 * @param {number} col2
 * @return {number}
 */
NumMatrix.prototype.sumRegion = function(row1, col1, row2, col2) {
    const prefix = this.prefix;
    const biggerRectSum = prefix[row2][col2];
    if(row1 == col1 && row1 == 0) return biggerRectSum;
    if(row1 == 0 || col1 == 0) {
        let subtractRegion = 0;
        if(row1 == 0) subtractRegion = prefix[row2][col1 - 1];
        else subtractRegion = prefix[row1 - 1][col2];
        return biggerRectSum - subtractRegion;
    }
    return biggerRectSum - prefix[row1-1][col2] - prefix[row2][col1 - 1] + prefix[row1-1][col1-1];
};

/** 
 * Your NumMatrix object will be instantiated and called as such:
 * var obj = new NumMatrix(matrix)
 * var param_1 = obj.sumRegion(row1,col1,row2,col2)
 */
Range Sum Query 2D - Immutable