#K68297. Grid Operations: Update and Subgrid Sum
Grid Operations: Update and Subgrid Sum
Grid Operations: Update and Subgrid Sum
You are given a square grid of size \(n \times n\) containing integers. You need to support two types of operations on the grid:
- update i j x: Update the element at row i and column j to the value x.
- sum r1 c1 r2 c2: Compute the sum of the elements in the subgrid defined by the top-left cell (\(r_1, c_1\)) and bottom-right cell (\(r_2, c_2\)) (inclusive).
The grid indices are 0-indexed. For example, given a 3x3 grid:
1 2 3 4 5 6 7 8 9A query
sum 0 0 1 1
yields \(1+2+4+5=12\). An update update 1 1 10
will change the grid to:
1 2 3 4 10 6 7 8 9and a subsequent
sum 0 0 2 2
will yield \(1+2+3+4+10+6+7+8+9=50\>.
inputFormat
The input is given via standard input. The first line contains an integer \(n\) specifying the size of the grid. The next \(n\) lines each contain \(n\) space-separated integers representing the grid.
The next line contains an integer \(q\), the number of operations. Each of the following \(q\) lines is in one of the two formats:
update i j x
- update the cell at row i and column j to value x.sum r1 c1 r2 c2
- output the sum of values in the subgrid defined from row r1, column c1 to row r2, column c2 (inclusive).
All output should be printed to standard output.
outputFormat
For every sum
operation in the input, output the computed sum on a new line to standard output.
3
1 2 3
4 5 6
7 8 9
3
sum 0 0 2 2
update 1 1 10
sum 0 0 2 2
45
50
</p>