#K39852. Swap Rows and Sum
Swap Rows and Sum
Swap Rows and Sum
You are given a grid with n rows and an arbitrary number of columns. Initially, the grid is filled with integers. You will also be provided with m operations, where each operation consists of two row indices r_1 and r_2 (1-indexed). For each operation, swap the two corresponding rows of the grid. After performing all operations, calculate the sum of the integers in each row.
Note: When swapping, the rows are exchanged immediately. If the same row is swapped with itself or swapped back, the grid remains unchanged for that operation.
Input/Output Requirements: The input will be given via stdin
, and the output should be written to stdout
. Make sure your solution reads from standard input and outputs the result to standard output.
The sums should be printed in the order of the rows after all swapping operations are completed, with each sum on a separate line.
Mathematically, if the grid after all operations is represented as \(G = [g_{i,j}]\) for \(1 \le i \le n \), then the answer is a list \(S\) where
[ S_i = \sum_{j} g_{i,j} \quad \text{for } i = 1, 2, \ldots, n. ]
inputFormat
The first line contains two integers n and m, where n is the number of rows in the grid and m is the number of swap operations.
The next n lines each contain a sequence of integers separated by spaces, representing the rows of the grid.
The following m lines each contain two integers r1 and r2 (1-indexed), indicating that row r1 should be swapped with row r2.
outputFormat
Output n lines. Each line contains a single integer which is the sum of the corresponding row of the grid after performing all swap operations.
## sample3 2
3 1 4
2 5 6
7 8 9
1 3
2 3
24
8
13
</p>