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:
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
๐Ÿข Smart Building Occupancy ManagementBuilding Floor Plan (Matrix)3๐Ÿ‘ฅ0๐Ÿ‘ฅ1๐Ÿ‘ฅ4๐Ÿ‘ฅ2๐Ÿ‘ฅ5๐Ÿ‘ฅ6๐Ÿ‘ฅ3๐Ÿ‘ฅ2๐Ÿ‘ฅ1๐Ÿ‘ฅ1๐Ÿ‘ฅ2๐Ÿ‘ฅ0๐Ÿ‘ฅ1๐Ÿ‘ฅ5๐Ÿ‘ฅ๐Ÿ“Š Query Zone: Rooms (2,1) to (2,3) = 2+0+1 = 3 peopleBIT Monitoring Network๐Ÿ“ก3๐Ÿ“ก3๐Ÿ“ก1๐Ÿ“ก4๐Ÿ“ก8๐Ÿ“ก14๐Ÿ“ก4๐Ÿ“ก18๐Ÿ”„ Each monitor tracks cumulative dataโšก Updates propagate through network๐ŸŽฏ Key Insight: Binary Indexed Tree Magic๐Ÿ’ก Smart Updates: When occupancy changes, only O(log m ร— log n) monitors need updates๐Ÿ” Quick Queries: Any zone query uses inclusion-exclusion with 4 prefix sumsโšก Efficiency: Both operations run in O(log m ร— log n) time - perfect for real-time systems!๐Ÿ† Result: Optimal performance for both updates and queries in smart building management
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.
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 24 Apple 18
78.5K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen