problems / 171

Alice and Bob's Matrix Game

easy matrix simulation games 1000 ms · 256 MB

Alice and Bob's Matrix Game

Alice and Bob play a game with an n×m matrix of integers. On each turn:

  • On Alice's turn, she chooses a row and increments every value in that row by 1
  • On Bob's turn, he chooses a column and decrements every value in that column by 1

They alternate turns starting with Alice, and they play for exactly k turns total (so Alice gets ⌈k/2⌉ turns and Bob gets ⌊k/2⌋ turns). You must simulate this game and output the final state of the matrix.

Input

The first line contains three integers n, m, k (1 ≤ n, m ≤ 1000; 1 ≤ k ≤ 10^6) — the number of rows, columns, and total number of turns.

The next n lines contain the initial matrix: row i contains m space-separated integers a_{i,j} (-10^9 ≤ a_{i,j} ≤ 10^9).

The next k lines describe the moves: each line contains either:

  • R i — Alice chooses row i (1-indexed)
  • C j — Bob chooses column j (1-indexed)

The moves are already in order, alternating between Alice and Bob, starting with Alice.

Output

Print the final n×m matrix after all k turns, one row per line, space-separated integers.

Notes

The matrix values may become negative and their absolute value may exceed 10^9 in the final output. Use 64-bit integers to avoid overflow.

Example 1

Input

2 2 3
1 2
3 4
R 1
C 1
R 2

Output

1 3
3 5

Example 2

Input

1 1 2
5
R 1
C 1

Output

5

Hints · reveal one at a time if you're stuck

Hint 1

Each move changes a whole row or a whole column, and the order of the moves does not matter for the final values. What determines the final value of cell (i, j)?

Hint 2

Count how many times each row was chosen by Alice and how many times each column was chosen by Bob, then combine the initial value with those two counts. Use 64-bit integers for the output.

report an issue Prefer your own editor? sooks prep 171 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.