Range Sum Query 2D - Mutable - Problem
Range Sum Query 2D - Mutable
You're given a 2D matrix and need to efficiently handle two types of operations:
๐ Update: Change the value of any cell in the matrix
๐ Query: Calculate the sum of all elements within a rectangular region
The challenge is to design a data structure that can handle multiple updates and queries efficiently. A naive approach would recalculate sums from scratch each time, but with smart preprocessing, we can do much better!
Example:
โข
โข
โข
๐ฏ Goal: Implement the
You're given a 2D matrix and need to efficiently handle two types of operations:
๐ Update: Change the value of any cell in the matrix
๐ Query: Calculate the sum of all elements within a rectangular region
The challenge is to design a data structure that can handle multiple updates and queries efficiently. A naive approach would recalculate sums from scratch each time, but with smart preprocessing, we can do much better!
Example:
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) returns 8 (sum of rectangle from (2,1) to (4,3))โข
update(3,2,2) changes matrix[3][2] from 0 to 2โข
sumRegion(2,1,4,3) now returns 10 (updated sum)๐ฏ Goal: Implement the
NumMatrix class with efficient update and query operations. Input & Output
example_1.py โ Basic Operations
$
Input:
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]]
Operations: sumRegion(2,1,4,3), update(3,2,2), sumRegion(2,1,4,3)
โบ
Output:
8, null, 10
๐ก Note:
First query sums rectangle (2,1) to (4,3): 2+0+1+1+0+1+0+3+0 = 8. After updating (3,2) from 0 to 2, the same rectangle now sums to 10.
example_2.py โ Single Cell Query
$
Input:
matrix = [[1,2],[3,4]]
Operations: sumRegion(0,0,0,0), update(0,0,5), sumRegion(0,0,0,0)
โบ
Output:
1, null, 5
๐ก Note:
Query single cell (0,0) returns 1. After updating to 5, same query returns 5.
example_3.py โ Entire Matrix Query
$
Input:
matrix = [[1,2],[3,4]]
Operations: sumRegion(0,0,1,1), update(1,1,10), sumRegion(0,0,1,1)
โบ
Output:
10, null, 16
๐ก Note:
Entire 2x2 matrix sums to 1+2+3+4=10. After updating (1,1) from 4 to 10, sum becomes 1+2+3+10=16.
Constraints
- m == matrix.length
- n == matrix[i].length
- 1 โค m, n โค 200
- -105 โค matrix[i][j] โค 105
- 0 โค row1 โค row2 < m
- 0 โค col1 โค col2 < n
- -105 โค val โค 105
- At most 104 calls will be made to sumRegion and update
Visualization
Tap to expand
Understanding the Visualization
1
Building Setup
Initialize the smart building monitoring system with sensor data from each room
2
Real-time Updates
When occupancy changes in a room, update affects multiple monitoring stations in logarithmic cascades
3
Zone Queries
Building manager queries any rectangular zone using prefix sum technique with inclusion-exclusion
4
Efficient Response
System responds in O(log m ร log n) time regardless of zone size
Key Takeaway
๐ฏ Key Insight: Binary Indexed Tree provides the perfect balance between update and query performance, making it ideal for real-time applications where both operations are frequent and time-critical.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code