#K1111. Grid Update and Subgrid Sum Query
Grid Update and Subgrid Sum Query
Grid Update and Subgrid Sum Query
You are given a grid with n rows and m columns, initially filled with zeros. You need to perform a series of operations on this grid. There are two types of operations:
- Update (denoted by 2): Update the value at a specific cell.
- Query (denoted by 1): Compute the sum of values in a subgrid, defined by its top-left and bottom-right coordinates.
Note: All grid indices are 1-based. For a query operation, you should sum the values cell by cell.
Mathematical Formulation:
For a query operation with parameters \(x_0, y_0, x_1, y_1\), the sum is given by:
\[ S = \sum_{i=x_0}^{x_1} \sum_{j=y_0}^{y_1} g(i,j) \]where \(g(i, j)\) is the value at cell \((i, j)\) in the grid.
inputFormat
The first line of input contains three space-separated integers: n, m, and Q, where:
- n is the number of rows of the grid.
- m is the number of columns of the grid.
- Q is the number of operations to perform.
The next Q lines each describe an operation.
Each operation is given in one of the following formats:
-
Update Operation:
2 x y v
Update the cell at row x and column y to the value v. -
Query Operation:
1 x0 y0 x1 y1
Output the sum of the subgrid from cell \((x_0,y_0)\) to cell \((x_1,y_1)\), inclusive.
outputFormat
For each query operation, print the resulting sum on a new line.
## sample6 5 5
2 2 1 5
2 3 2 3
1 1 1 3 3
2 1 4 2
1 1 1 4 4
8
10
</p>