Range Sum Query 2D Mutable

You run a grid of weather monitoring stations, each reporting the rainfall captured at one lattice cell of a continentspan field. Readings shift whenever a sensor is recalibrated, and researchers constantly ask for the total rainfall across a rectangular patch. Because the values change frequently, a one-time prefix table goes stale the moment any sensor is retuned. Design a system that handles both kinds of requests efficiently: refreshing a single cell to a new reading, and summing every cell inside an axis-aligned rectangle. The two operations interleave arbitrarily, so an update can re-run a sensor immediately after a query reports on it.

Implement the Solution class:

  • Solution(matrix) initializes the object with the integer matrix.
  • sumRegion(row1, col1, row2, col2) returns the sum of the elements of matrix inside the rectangle whose upper-left corner is (row1, col1) and whose lower-right corner is (row2, col2).
  • update(row, col, val) makes matrix[row][col] equal to val, and records the change for every subsequent query.

Every update rewrites the cell value for all later calls. Rectangle corners are inclusive on all four sides.

Examples
Input: [["Solution","update","update","sumRegion","sumRegion","sumRegion","update","sumRegion","sumRegion"],[[[[0,-5],[-42,9],[-29,18],[5,-34],[-22,35],[-20,-4],[36,12]]],[2,0,8],[5,1,-32],[5,0,6,0],[4,0,5,0],[5,1,5,1],[0,1,-48],[2,0,4,0],[3,0,5,0]]]
Output: [null,null,null,16,-42,-32,null,-9,-37]
Hints

Range Sum Query 2D Mutable

You run a grid of weather monitoring stations, each reporting the rainfall captured at one lattice cell of a continentspan field. Readings shift whenever a sensor is recalibrated, and researchers constantly ask for the total rainfall across a rectangular patch. Because the values change frequently, a one-time prefix table goes stale the moment any sensor is retuned. Design a system that handles both kinds of requests efficiently: refreshing a single cell to a new reading, and summing every cell inside an axis-aligned rectangle. The two operations interleave arbitrarily, so an update can re-run a sensor immediately after a query reports on it.