#C8730. Matrix Query Operations
Matrix Query Operations
Matrix Query Operations
You are given a matrix of size \(n \times m\) containing integer elements. You need to process \(Q\) queries on this matrix. There are two types of queries:
- Type 1: Update the value of an element in the matrix. The query has the form: \(1\ x\ y\ v\), which means that the element at row \(x\) and column \(y\) should be updated to \(v\) (i.e. \(a_{xy} = v\)).
- Type 2: Calculate the sum of a submatrix. The query has the form: \(2\ x_1\ y_1\ x_2\ y_2\), which means you need to calculate the sum of all elements in the submatrix with the top-left coordinate \((x_1, y_1)\) and bottom-right coordinate \((x_2, y_2)\). The sum is defined as \[ S = \sum_{i=x_1}^{x_2}\sum_{j=y_1}^{y_2}a_{ij} \] .
Process the queries in the order they are given. For each query of type 2, output the result of the sum query on a new line.
Input is read from standard input and output is written to standard output.
inputFormat
The first line of input contains two integers \(n\) and \(m\) separated by a space, representing the number of rows and columns of the matrix, respectively.
The next \(n\) lines each contain \(m\) space-separated integers representing the elements of the matrix.
The following line contains an integer \(Q\) representing the number of queries.
Each of the next \(Q\) lines contains a query. A query is in one of the following formats:
- For an update query:
1 x y v
- For a sum query:
2 x1 y1 x2 y2
outputFormat
For each query of type 2, output the sum of the specified submatrix on a new line.
## sample3 3
1 2 3
4 5 6
7 8 9
3
2 1 1 2 2
1 2 2 10
2 1 1 2 2
12
17
</p>