Under construction

Still being built. Things may change

Array Play

Interview

Time limit2sMemory limit512 MB

Summary
Given an N by N grid and M rectangle-add operations, output the final sum of each row and each column.
Level

Medium4 of 10

Topics
Prefix sum, Array, Implementation, Simulation
Solved
No attempts yet

Problem

There is a two-dimensional integer array AA with NN rows and NN columns. A[r,c]A[r, c] denotes the value of the element in row rr and column cc.

Consider a game on this array that applies a total of MM operations.

Each operation is given by five integers (r1,c1,r2,c2,v)(r_1, c_1, r_2, c_2, v) satisfying 1≤r1≤r2≤N1 \le r_1 \le r_2 \le N, 1≤c1≤c2≤N1 \le c_1 \le c_2 \le N, and −1,000≤v≤1,000-1{,}000 \le v \le 1{,}000. The operation adds vv to every A[r,c]A[r, c] inside the rectangle from (r1,c1)(r_1, c_1) to (r2,c2)(r_2, c_2).

For example, take N=3N = 3 and A=[[1,2,3],[4,5,6],[7,8,9]]A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], and apply the following three operations in order.

  • Operation 1: (r1=1,c1=1,r2=2,c2=3,v=3)(r_1 = 1, c_1 = 1, r_2 = 2, c_2 = 3, v = 3)
  • Operation 2: (r1=2,c1=2,r2=3,c2=2,v=−5)(r_1 = 2, c_1 = 2, r_2 = 3, c_2 = 2, v = -5)
  • Operation 3: (r1=1,c1=1,r2=3,c2=2,v=1)(r_1 = 1, c_1 = 1, r_2 = 3, c_2 = 2, v = 1)

Before any operation, AA is as follows.

A = [ [ 1 2 3 ]
      [ 4 5 6 ]
      [ 7 8 9 ] ].

Applying operation 1 changes the six elements in the first two rows:

A = [ [ 4 5 6 ]
      [ 7 8 9 ]
      [ 7 8 9 ] ].

After applying operation 2:

A = [ [ 4 5 6 ]
      [ 7 3 9 ]
      [ 7 3 9 ] ].

After applying operation 3:

A = [ [ 5 6 6 ]
      [ 8 4 9 ]
      [ 8 4 9 ] ].

After all three operations, you want the sum of the elements of each row and the sum of the elements of each column of the resulting array. In the example above, the row sums are [17,21,21][17, 21, 21] (row 1 to row 3) and the column sums are [21,14,24][21, 14, 24] (column 1 to column 3).

Given NN, MM, a two-dimensional array AA, and MM operations, write a program that computes the row sums and column sums after applying all operations to the array.

Input

The first line contains the number of test cases TT (1≤T≤101 \le T \le 10).

For each test case, the first line contains two integers NN and MM separated by a space (1≤N≤1,0001 \le N \le 1{,}000, 1≤M≤1,0001 \le M \le 1{,}000).

The next NN lines give the two-dimensional array AA, where the ii-th line is the ii-th row. The jj-th integer in each line is the value of the element in the jj-th column. Each element of AA is an integer between 11 and 1,0001{,}000 inclusive.

The next MM lines each contain five integers r1,c1,r2,c2,vr_1, c_1, r_2, c_2, v separated by spaces. It always holds that 1≤r1≤r2≤N1 \le r_1 \le r_2 \le N, 1≤c1≤c2≤N1 \le c_1 \le c_2 \le N, and −1,000≤v≤1,000-1{,}000 \le v \le 1{,}000.

Output

For each test case, print the answer on two lines.

The first line contains the NN row sums separated by spaces (row 1 to row NN).

The second line contains the NN column sums separated by spaces (column 1 to column NN).

Hint

The first test case is described in the problem statement.

For the second test case, applying the operation changes the array to the following.

[ [ -20 -10 ]
  [   0  10 ] ].

Thus the row sums are [−30,10][-30, 10] and the column sums are [−20,0][-20, 0].

For the third test case, applying all operations leaves a 1×11 \times 1 array whose element is 2000.

PyPy is faster than Python in most cases, so if your Python solution exceeds the time limit, it is better to use PyPy.

Examples1

  1. Example 1

    Input
    3
    3 3
    1 2 3
    4 5 6
    7 8 9
    1 1 2 3 3
    2 2 3 2 -5
    1 1 3 2 1
    2 1
    10 20
    30 40
    1 1 2 2 -30
    1 3
    1000
    1 1 1 1 1000
    1 1 1 1 -1000
    1 1 1 1 1000
    
    Expected output
    17 21 21
    21 14 24
    -30 10
    -20 0
    2000
    2000