Answer rectangle sum queries on a matrix that receives point updates
Company: Google
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Other
You are given an `m x n` integer matrix `matrix` and a sequence of operations to run against it in order. There are two kinds of operation:
- `update(row, col, val)` sets `matrix[row][col]` to `val`.
- `sumRegion(row1, col1, row2, col2)` returns the sum of the elements inside the rectangle whose upper-left corner is `(row1, col1)` and whose lower-right corner is `(row2, col2)`, both corners included.
The operations arrive as a list of names and a matching list of argument lists. Return one result per operation: `None` for each `update` and the computed sum for each `sumRegion`.
### Function Signature
```python
def process_operations(matrix: list[list[int]], operations: list[str], arguments: list[list[int]]) -> list[int | None]:
```
### Rules
- `operations[k]` is either `"update"` or `"sumRegion"`, and `arguments[k]` holds its arguments: `[row, col, val]` for `update`, `[row1, col1, row2, col2]` for `sumRegion`.
- Operations run in list order, and every `sumRegion` sees the effect of all earlier updates.
- `update` is an assignment: after it runs, `matrix[row][col]` equals `val`.
- Indices are 0-based. Rows run top to bottom and columns left to right.
- The returned list has the same length as `operations`, with `None` at the position of every `update`.
### Constraints
- `m == len(matrix)` and `n == len(matrix[i])`
- `1 <= m, n <= 200`
- `-1000 <= matrix[i][j] <= 1000`
- `0 <= row < m` and `0 <= col < n`
- `-1000 <= val <= 1000`
- `0 <= row1 <= row2 < m`
- `0 <= col1 <= col2 < n`
- `len(operations) == len(arguments)`, with at most 5000 operations in total.
### Examples
```text
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","update","sumRegion"], arguments = [[2,1,4,3],[3,2,2],[2,1,4,3]]
Output: [8, None, 10]
```
The rectangle covers rows 2 to 4 and columns 1 to 3, whose values are 2, 0, 1, 1, 0, 1, 0, 3 and 0, for a sum of 8. The update changes `matrix[3][2]` from 0 to 2, a cell inside the rectangle, so the second query returns 10.
```text
Input: matrix = [[1,2],[3,4]], operations = ["sumRegion","update","sumRegion","update","sumRegion"], arguments = [[0,0,1,1],[0,0,5],[0,0,1,1],[1,1,10],[0,0,1,1]]
Output: [10, None, 14, None, 20]
```
The whole matrix sums to 10. Setting the top-left cell from 1 to 5 raises the total by 4 to 14, and setting the bottom-right cell from 4 to 10 raises it by 6 to 20.
```text
Input: matrix = [[-1,2],[3,-4]], operations = ["sumRegion","update","sumRegion","sumRegion"], arguments = [[1,0,1,1],[1,0,-5],[0,0,1,0],[1,1,1,1]]
Output: [-1, None, -6, -4]
```
The bottom row sums to 3 + (-4) = -1. After `matrix[1][0]` becomes -5, the left column sums to -1 + (-5) = -6, and the single cell `(1, 1)` is still -4.
Overview: Process a sequence of point updates and rectangle sum queries on an integer matrix, returning the sum for each query and None for each update. It tests picking a data structure that keeps both operations fast, along with careful index handling when combining prefix sums.