This page is still under construction.

Parts of this page are still being built. What you see may change.

Simple Operations in Matrix

Interview

Time limit1sMemory limit512 MB

Summary
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:

  • row k val: add val to every element on the k-th row,
  • col k 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:

  • row k val (1 ≤ k ≤ N; -100 ≤ val ≤ 100)
  • col k 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.

Examples3

  1. Example 1

    Input
    3 4
    1 1 1 1
    1 1 1 1
    1 1 1 1
    2
    row 1 3
    col 4 -2
    
    Expected output
    18 -1 4
    
  2. Example 2

    Input
    4 3
    10 10 10
    10 10 10
    10 10 10
    10 10 10
    5
    row 2 -5
    col 3 6
    col 1 -10
    row 4 7
    col 1 3
    
    Expected output
    122 -2 23
    
  3. Example 3

    Input
    2 3
    15 7 8
    31 1 14
    3
    row 2 -15
    col 1 10
    row 1 2
    
    Expected output
    57 -14 27