Problem Description
You are given a 2D matrix and need to answer multiple queries asking for the sum of a submatrix defined by its upper left and lower right corners. The task is to design a solution where each query runs in O(1) time.
Key Insights
- Precompute a prefix sum matrix where each cell accumulates the sum of elements above and to the left.
- Use the inclusion-exclusion principle to efficiently calculate the sum of any submatrix using precomputed sums.
- Preprocessing the matrix takes O(m * n) time, but each query is answered in constant time (O(1)).
Space and Time Complexity
Time Complexity:
- Preprocessing: O(m * n)
- Each Query: O(1)
Space Complexity: O(m * n) for storing the prefix sum matrix.
Solution
The solution involves building a prefix sum matrix, dp, where dp[i+1][j+1] holds the sum of elements in the submatrix from (0, 0) to (i, j). This allows summing any rectangular region using the inclusion-exclusion principle. Specifically, for a query defined by (row1, col1) to (row2, col2), the region sum can be computed as:
dp[row2+1][col2+1] - dp[row1][col2+1] - dp[row2+1][col1] + dp[row1][col1]
This approach ensures that after an O(m * n) preprocessing step, each query is computed in constant time.
Code Solutions
Below are the code solutions in Python, JavaScript, C++, and Java with line-by-line comments.