Simple Operations in Matrix
InterviewTime limit1sMemory limit512 MB
Apply row and column addition queries to a small matrix, then print the total sum, minimum, and maximum of the final values.
- Level
Easy2 of 10
- Topics
- Array, Simulation, Implementation, Matrix
- Solved
- No attempts yet
Problem
A matrix is a mathematical object that arranges data into a rectangular array of N rows and M columns. The rows are indexed from 1 to N, and the columns are indexed from 1 to M. Matrices are very powerful and useful in many applications. In this problem, we focus on two simple operations on a matrix: row addition and column addition.
You are given a matrix of integers with N rows and M columns, and Q queries of the following forms:
rowk val: add val to every element on the k-th row,colk val: add val to every element on the k-th column.
After all queries have been performed, output the following three numbers:
- sum: the sum of all elements in the matrix,
- min: the value of the smallest element in the matrix,
- max: the value of the largest element in the matrix.
See the sample input for clarity.
Input
The first line contains two integers N M (1 ≤ N, M ≤ 50), the size of the matrix (the number of rows and columns, respectively). The next N lines each contain M integers Ai,j (-100 ≤ Ai,j ≤ 100), the matrix element at the i-th row and j-th column, for 1 ≤ i ≤ N and 1 ≤ j ≤ M. The next line contains an integer Q (0 ≤ Q ≤ 100), the number of queries. The next Q lines each contain a query in one of the following forms:
rowk val (1 ≤ k ≤ N; -100 ≤ val ≤ 100)colk val (1 ≤ k ≤ M; -100 ≤ val ≤ 100)
Output
Output the three integers sum min max on a single line, separated by single spaces, as described in the problem statement.