We are given a problem: Artem has an element that stores a matrix of size n x m. There are n+m inputs: each row and each column can receive a signal. When a signal comes to a row input (type 1), that row cyclically shifts left: so row r: (a_{r,1}, a_{r,2}, ..., a_{r,m}) becomes (a_{r,2}, ..., a_{r,m}, a_{r,1}). When a signal comes to a column input (type 2), that column cyclically shifts up: column c: (a_{1,c}, a_{2,c}, ..., a_{n,c}) becomes (a_{2,c}, ..., a_{n,c}, a_{1,c}). Initially, there is some unknown matrix A (initial). Then we have a sequence of q operations (turns). Each operation either applies a shift (type 1 or 2) or checks a cell value (type 3) and records that at that moment, the cell (r,c) must have value x.
We are told the experiment data is consistent, meaning there exists at least one initial matrix that satisfies all constraints. We need to output any valid initial matrix.
Constraints: n,m up to 100; q up to 10000. Shifts are cumulative. We need to compute initial values such that after applying operations in order, the observed values match. Since the operations are only row left shifts and column up shifts, these are permutations of the matrix entries. For each cell (i,j), its content moves according to the sequence of row and column shifts.
Alternatively, we can think of the matrix evolving over time via row and column permutations. The state after a sequence of operations can be described by mapping each original cell (i,j) to its current position (r,c), or mapping each current position to the original cell that currently occupies it. Since operations are reversible (cyclic shifts are permutations), we could invert the sequence to determine what initial value must be in a given original position for an observed value at a later position.
Let’s denote the initial matrix A[i][j]. After a sequence of operations, the value at a certain position (r,c) is some A[original_i][original_j] where original indices depend on the operations applied so far. So constraints are of the form: For a type 3 operation at time t (after applying all operations before it), we have that A[original_i][original_j] = x.
Thus, each type 3 gives a linear equation (an assignment) linking an initial cell to a value. However, the same original cell might be referenced multiple times, requiring consistency (same value). Because the experiment is consistent, there should be no contradiction.
Thus, we need to compute, for each type 3 operation, the mapping from the observed (r,c) back to the original indices, given the sequence of operations before that point. Then assign values to initial cells accordingly. For cells not referenced, we can assign any value (e.g., 0). However, we must also respect that the mapping might assign multiple constraints to the same original cell; they must be equal, which is guaranteed consistent.
So the core challenge is: simulate the sequence of operations, but we need to know for each time step the inverse mapping of positions. Since n,m <= 100 and q <= 10000, we can simulate by maintaining the current matrix? But that would be up to 10k * 100*100 = 1e8, maybe borderline but perhaps acceptable in C++? But we need to compute initial matrix, not current. Actually we could simulate forward with unknown values using symbolic representation. But we need to output initial values. The typical solution: maintain two arrays: row_offset and col_offset? But careful: Because both row and column shifts affect positions. However, note that row shifts only affect columns ordering within the row, and column shifts affect rows ordering within the column. But these transformations do not commute exactly? But we can treat the effect as a permutation on the positions.
Let’s consider representation: Let’s denote that the current matrix entry at position (i, j) originally came from some (r0, c0). We could maintain two arrays: row_shift[i] indicating how many left shifts have been applied to row i (cumulative mod m). Similarly col_shift[j] for column j (up shifts mod n). However, applying a column shift will affect all rows? Actually column shifts shift the elements within each column independently; but the rows themselves are not shifted; they are just the rows of the matrix. The row shifts and column shifts are applied sequentially. After a series of operations, the net effect on each cell can be expressed as: For a cell originally at (i, j), its current row index = (i + total column shifts applied to column j? Actually careful: Column shift up: the element at row 1 moves to row n? Wait description: "first element of the column becomes last element, second element becomes first and so on." That is a cyclic shift to the top. Typically "shift cyclically top" means each element moves up one: row1 becomes last, row2 becomes row1, etc. Actually they say: "When signal comes to the input corresponding to some column, that column shifts cyclically to the top, that is first element of the column becomes last element, second element becomes first and so on." So if we have column c, its elements are (a_{1,c}, a_{2,c}, ..., a_{n,c}) initially. After one column shift up, the new column is (a_{2,c}, a_{3,c}, ..., a_{n,c}, a_{1,c}). So element originally at row i moves to row i-1 (with wrap) after one column up shift. So the row index after column shift decreases by 1 (mod n). So if we think about the movement of a specific element: Row shifts move an element within its row, i.e., column index changes. Column shifts move an element within its column, i.e., row index changes.
If we apply row shifts and column shifts, we can track offsets for rows and columns separately. However, they interact because row shifts affect columns but not rows; column shifts affect rows but not columns. If we treat the position of an element as (r, c) where r is row, c is column. Initially element from (i, j) is at (i, j). When we apply a row shift on row r, all elements currently in row r will have their column index shifted left by 1 mod m. If we apply a column shift on column c, all elements currently in column c will have their row index shifted up by 1 mod n.
If we maintain offsets per row for column index and offsets per column for row index, we might need to combine them because after column shifts, the row offsets for elements might be affected. But note: The row shifts are only applied to specific rows; column shifts to specific columns. The effect on a particular element can be computed by simulating the series of operations backward.
Given q up to 10000 and n,m <= 100, we can simply simulate the whole sequence of operations in terms of mapping from original to current, but we can store a "permutation" matrix of size nm? That would be 10000 cells, which is fine. Actually nm <= 10000. So we could represent each cell's current coordinates relative to original. Or we could maintain two arrays: row_map[i] and col_map[j] that tell where the element that originated in row i currently is? Actually, we can think of the matrix after a series of operations as being obtained from the original by applying a permutation to rows and columns? However, because row shifts are per row, they don't mix rows, and column shifts are per column, they don't mix columns. But overall the mapping is: for an original cell (i, j), its current coordinates (r, c) are determined by:
- Initially: (r = i, c = j).
- Each row shift on row r' (current row index) will affect elements currently in that row, shifting their column index. So to track, we need to apply operations in order.
Alternatively, we could track the inverse mapping: For a current position (r, c), which original cell is there? This mapping is easier to update upon operations. Suppose we have an array orig[r][c] storing (i,j) of the original cell currently at (r,c). Initially orig[r][c] = (r,c). When we apply a row shift left on row r: the row's columns cycle left. That means for each column c from 1 to m-1, orig[r][c] becomes what was orig[r][c+1]; and orig[r][m] becomes what was orig[r][1]. This is a permutation on the orig values in that row. Similarly, for a column shift up on column c: for each row r from 1 to n-1, orig[r][c] becomes what was orig[r+1][c]; and orig[n][c] becomes what was orig[1][c]. This updates accordingly.
Since n,m <= 100, orig is a 100x100 array of pairs (int, int). q up to 10000, each operation O(n) or O(m) for updating the entire row or column. In worst case, each operation might shift a row (cost m) or column (cost n). So worst-case total cost: q * max(n,m) = 10000 * 100 = 1e6, which is fine. So we can simulate forward, updating the mapping of which original cell is at each current position. For each type 3 query (ri, ci, xi), we look at the current orig[ri][ci] = (i0, j0), and record that A[i0][j0] must equal xi. Because consistency guaranteed, we can just assign that value, and if later we get another assignment for same original cell, we must check consistency (but if consistent we accept). However, if we assign on the fly, we may have contradictions? Since guaranteed, we can just assign later value to same cell; but if there's a conflict we could overwrite? But to be safe, we can collect constraints and then assign at end. Or we can maintain an array initVal[i][j] initialized to some sentinel (like None). For each type 3, we compute (i0,j0) from orig, and if initVal[i0][j0] is unset, set it to xi; else check it equals xi; if not, it would be inconsistent but guaranteed not to happen.
After processing all operations, we have some original cells assigned. For unassigned ones, we can set any value (say 0). But careful: The output must be the initial matrix. So we output A with those assigned values and zeros for others. That's it.
But wait: Is the forward simulation with orig mapping sufficient? Let's test on the example.
Example 1:
n=2,m=2.
Initialize orig[1][1]=(1,1), orig[1][2]=(1,2), orig[2][1]=(2,1), orig[2][2]=(2,2).
Operations:
- type2 c=1: column 1 shift up.
So column1: orig[1][1] becomes orig[2][1] = (2,1); orig[2][1] becomes orig[1][1] (old) = (1,1). So after: orig[1][1]=(2,1), orig[2][1]=(1,1). Others unchanged.
- type2 c=2: column2 shift up.
Column2: orig[1][2] becomes orig[2][2]=(2,2); orig[2][2] becomes orig[1][2]=(1,2). So after: orig[1][2]=(2,2), orig[2][2]=(1,2).
Now we have queries:
- type3 1 1 1: orig[1][1] is (2,1) -> A[2][1]=1.
- type3 2 2 2: orig[2][2] is (1,2) -> A[1][2]=2.
- type3 1 2 8: orig[1][2] is (2,2) -> A[2][2]=8.
- type3 2 1 8: orig[2][1] is (1,1) -> A[1][1]=8.
So initial matrix: A[1][1]=8, A[1][2]=2, A[2][1]=1, A[2][2]=8. That matches output (8 2; 1 8). Yes.
Example 2:
n=3,m=3.
Initial orig as identity.
Operations:
- type1 r=2: row2 shift left.
Row2: orig[2][1] becomes orig[2][2]=(2,2); orig[2][2] becomes orig[2][3]=(2,3); orig[2][3] becomes orig[2][1]=(2,1). So after: row2: (2,2),(2,3),(2,1).
- type3 2 2 5: orig[2][2] is (2,3) currently? Wait after shift: row2 indices: col1->(2,2), col2->(2,3), col3->(2,1). So orig[2][2] = (2,3). So assign A[2][3]=5. So initial matrix: all 0 except A[2][3]=5. That matches output: all zeros except row2 col3 =5.
So this method works.
Complexity: O(q * max(n,m) + nm). Memory O(nm). Good.
But we need to be careful: The mapping orig is from current (r,c) to original (i,j). But when we apply row shift on row r, we need to shift the orig values in that row. Similarly for column shift. Implementation straightforward.
One nuance: Since we are only reading operations sequentially and updating orig, we need to be careful about the order: For row shift, we need to temporarily store the first value and then assign. In column shift, similarly.
Edge cases: n=1 or m=1: still works. Shifts: row shift left on a row with m=1 does nothing (cyclic shift of 1 element). Similarly column shift up with n=1 does nothing. Our code should handle that.
We also need to store initial values: init[n+1][m+1] maybe using long long because x can be up to 1e9. Use vector of vectors.
Implementation steps:
-
Read n,m,q.
-
Initialize orig as a 2D vector of pairs: vector<vector<pair<int,int>>> orig(n+1, vector<pair<int,int>>(m+1));
For i=1..n, j=1..m: orig[i][j] = {i,j}.
-
Initialize ans as a 2D vector of long long (or int) with default 0 (or maybe -inf sentinel). We'll use vector<vector> init(n+1, vector(m+1, 0)). But we need to differentiate unset. Since 0 could be a valid value (including negative?), but it's allowed to output any values for unconstrained cells, so setting to 0 is fine. But we should note that constraints might require some cell to be something else; but if a cell is constrained to a value, we set that. So we don't need a sentinel; we can just set to 0 initially and then override with constrained values. However, what if a cell is constrained to 0? That's fine; we set it to 0 anyway. But we need to avoid conflict if later another constraint says a different value for the same cell; but that is guaranteed consistent. So we can simply for each type3, compute (i0,j0) and set init[i0][j0] = xi. If later another type3 for same (i0,j0) gives a different xi, it would contradict consistency; but since guarantee, we can ignore. But to be safe, we could check and maybe throw, but not needed.
But careful: If we set init[i0][j0] = xi each time, but if two constraints with same original cell have same xi, fine; if different, we would override; the output would be the last one, which might be correct if both are same? Actually if guarantee says at least one initial matrix exists, it means all constraints for the same original cell must have the same x. So if we set to the last seen, it should equal the previous ones (if not, then the guarantee would be violated). So we can just set without check, but it's better to check for debugging.
Thus algorithm:
For each query:
read ti.
if ti == 1:
read r.
// row shift left on row r
// Save orig[r][1] into tmp
tmp = orig[r][1];
for j=1 to m-1: orig[r][j] = orig[r][j+1];
orig[r][m] = tmp;
else if ti == 2:
read c.
// column shift up on column c
// Save orig[1][c]
tmp = orig[1][c];
for i=1 to n-1: orig[i][c] = orig[i+1][c];
orig[n][c] = tmp;
else if ti == 3:
read r, c, x.
pair<int,int> p = orig[r][c];
init[p.first][p.second] = x;
After processing all queries, output init[i][j] for i=1..n, j=1..m.
That should produce a valid initial matrix.
We must ensure we handle indices correctly: rows 1..n, cols 1..m.
Now let's test with some more scenarios to verify consistency.
Consider if there are both row and column shifts after queries. Example: n=3,m=3.
Sequence: type1 r=1, type2 c=1, type3 (1,1,x). We need to see if mapping works.
Initial orig identity.
After row shift on row1: row1 left shift: orig[1][1]=(1,2), orig[1][2]=(1,3), orig[1][3]=(1,1).
After column shift on col1: column1 up shift: we need to shift the current column1. Current column1 values: orig[1][1]=(1,2), orig[2][1]=(2,1), orig[3][1]=(3,1). Up shift: tmp = orig[1][1] = (1,2); then orig[1][1] = orig[2][1] = (2,1); orig[2][1] = orig[3][1] = (3,1); orig[3][1] = tmp = (1,2). So now orig[1][1] = (2,1). Query (1,1) gives original (2,1). So init[2][1] = x. That seems plausible.
Now check if we could compute initial matrix from forward simulation. This method works.
Now think about whether there is any subtlety: The operations are applied sequentially. Our orig mapping accurately reflects permutation of cells if we simulate updates as described. Because each operation is a permutation on the set of positions. Maintaining orig as mapping from current positions to original is correct if we apply the permutation to the orig matrix accordingly. When we shift a row left, we are effectively moving the contents of that row; the orig values move with the content. So updating orig as described matches.
Thus the solution is straightforward.
Potential pitfalls: For row shift, we need to be careful about the direction. The problem says: row shift left: first becomes last, second becomes first. That is: new row: old col2 -> col1, old col3 -> col2, ..., old col1 -> colm. That's what we did: we saved first, then shift left, assign last to saved first. However, some implementations might do shift right, but we need to ensure correct direction. Our update: we saved orig[r][1] as tmp, then for j=1..m-1: orig[r][j] = orig[r][j+1], then orig[r][m] = tmp. This yields: after shift, what was at column 2 goes to column 1; column 3 to column 2; ...; column m to column m-1; column 1 to column m. That's exactly a left cyclic shift? Let's test with a simple row of three elements: [A,B,C]. After left shift, should become [B,C,A]. Our algorithm: save tmp=A. then j=1: orig[1]=orig[2]=B; j=2: orig[2]=orig[3]=C; then orig[3]=tmp=A. So result: [B,C,A] correct.
For column shift up: first becomes last, second becomes first. That is up shift: new column: old row2->row1, row3->row2,..., row1->rown. Our algorithm: save orig[1][c] (old row1), then for i=1..n-1: orig[i][c] = orig[i+1][c]; then orig[n][c] = saved. That yields: after shift, orig[1][c] gets old row2; orig[2][c] gets old row3; ...; orig[n][c] gets old row1. That matches description: shift up (top element goes to bottom). Good.
Now about output: The values can be any, as long as they match constraints. We can set unconstrained cells to 0. That is fine.
Now complexity: q up to 10000, n,m up to 100. Each row shift O(m) at most 100; each column shift O(n) at most 100; total ops ~ 1e6, fine. Memory small.
Now we should consider if there is any alternative approach, but this is fine.
Edge Cases: If the same original cell gets multiple constraints, they are consistent, so we can assign either one. For unconstrained cells, we output 0 (or any integer within bounds). However, we should ensure that the output integers do not exceed 10^9 in absolute value. Since constraints x are within -1e9 to 1e9, and we set unconstrained to 0, it's fine.
Implementation details in C++:
- Use vector<vector<pair<int,int>>> orig(n, vector<pair<int,int>>(m)) if using 0-index. Simpler: 0-index. But input uses 1-index. We can keep 0-index and adjust. Let's decide: Keep 0-index internally for arrays. For row and column indices from input, subtract 1 when accessing array.
Initialize orig: for i in 0..n-1, for j in 0..m-1, orig[i][j] = {i, j}. Or we could use struct? pair is fine.
When reading ti:
if ti == 1: read r (1-index), convert to zero: r-1. Then perform shift: tmp = orig[r][0]; for j=0..m-2: orig[r][j] = orig[r][j+1]; orig[r][m-1] = tmp.
if ti == 2: read c (1-index), convert to c-1. Then shift column: tmp = orig[0][c]; for i=0..n-2: orig[i][c] = orig[i+1][c]; orig[n-1][c] = tmp.
if ti == 3: read r, c, x; convert to zero: r-1, c-1. pair p = orig[r][c]; init[p.first][p.second] = x. Note: p.first, p.second are 0-index? Because orig[i][j] stores original indices as we set 0-index. So set init[p.first][p.second] = x.
After processing all, output init matrix: for i=0..n-1, for j=0..m-1, print init[i][j] followed by space or not? Typically print with a space after each except maybe last in row? Just follow example: each line has numbers separated by space and maybe trailing space; it's okay.
Now consider that we might need to use long long for x, but int is sufficient (1e9 fits in int). But x could be negative, so int is fine. Use int.
Now confirm with examples.
Example 1: n=2,m=2. Our code will produce same.
Now check a random test: n=2,m=2. Sequence: type1 r=1, then type3 (1,1,x). Let's simulate manually: initial orig: (0,0),(0,1) row0; row1: (1,0),(1,1). After row shift left on row0: tmp = orig[0][0]=(0,0); then orig[0][0]=orig[0][1]=(0,1); orig[0][1]=tmp=(0,0). So now orig[0][0]=(0,1). Query (1,1) => (0,1) => set init[0][1]=x. So output initial row0 col2 = x. Others 0. That seems correct? Let's think: After row shift left, the original value at (0,0) moved to col2? Actually after row shift left, row0 originally [a,b] becomes [b,a]. So at (1,1) (row1? wait r=1, c=1) after row shift, the value is b, which originally at (1,2). So yes, init[0][1] should be x. Good.
Now another: column shift then query. Already covered.
Now what about multiple shifts mixing? It should work.
Potential Issue: The matrix size is up to 100x100, and we are storing orig as pair<int,int>. For each row shift we copy pairs (two ints) m times. That's fine.
Now confirm that the forward simulation is indeed correct: we are treating the orig mapping as the current permutation. However, there is a subtlety: Because we update orig for each operation, we must ensure that we correctly propagate the permutation. Since operations are permutations, and we apply them to the mapping, it's correct.
Now, is there any need to maintain both forward and inverse? Not needed.
Now, will this always produce a valid initial matrix? Yes, because for each observation we record the required original cell value. The mapping orig tells us exactly which original cell is at that position at that time, based on the sequence of operations we simulated. Since our simulation exactly matches the transformations described, the recorded constraints correspond to the necessary values in the initial matrix. After processing all constraints, we fill the rest arbitrarily, giving a consistent initial matrix.
But wait: The mapping orig we maintain is based on the assumption that the initial matrix's cells move as per operations. That is indeed what happens. So if we assign the initial values as recorded, then when we simulate the whole sequence with those initial values, the observations would match. However, does our simulation of orig exactly mirror the movement of actual values? Yes, because we treat each cell as a "token" that carries its original coordinates. So if we later built the actual matrix using the assigned initial values and applied the same operations, the observed values would be correct.
Thus solution is sound.
Now discuss complexity and memory: O(nm + qmax(n,m)). For q=10000, max(n,m)=100, that's 1e6 operations, plus small overhead.
Now possible alternative: Using offsets. But this is simpler.
Edge Cases: n=100,m=100, q=10000 all operations type1 on random rows. Each row shift cost O(m)=100, total 1e6, fine.
Now implement in C++.
Implementation steps in code:
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, q;
cin >> n >> m >> q;
// orig: n rows, m columns, each element is pair<int,int>
vector<vector<pair<int,int>>> orig(n, vector<pair<int,int>>(m));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
orig[i][j] = {i, j};
// answer matrix initial values, initialized to 0
vector<vector<int>> init(n, vector<int>(m, 0));
for (int t = 0; t < q; ++t) {
int type;
cin >> type;
if (type == 1) {
int r;
cin >> r;
r--;
// row shift left
pair<int,int> tmp = orig[r][0];
for (int j = 0; j < m-1; ++j)
orig[r][j] = orig[r][j+1];
orig[r][m-1] = tmp;
} else if (type == 2) {
int c;
cin >> c;
c--;
// column shift up
pair<int,int> tmp = orig[0][c];
for (int i = 0; i < n-1; ++i)
orig[i][c] = orig[i+1][c];
orig[n-1][c] = tmp;
} else if (type == 3) {
int r, c, x;
cin >> r >> c >> x;
r--; c--;
pair<int,int> p = orig[r][c];
init[p.first][p.second] = x;
}
}
// output
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (j) cout << " ";
cout << init[i][j];
}
cout << endl;
}
return 0;
}
That seems correct.
Now double-check directions again:
-
Row shift left: They say: "when signal comes to the input corresponding to some row, this row cyclically shifts to the left, that is the first element of the row becomes last element, second element becomes first and so on." So first becomes last, second becomes first. So new row: [second, third, ..., last, first]. That's left shift. Our implementation: shift left: we move each element one to left, and the leftmost wraps to rightmost. That yields [second, third, ..., last, first] correct.
-
Column shift up: "first element of the column becomes last element, second element becomes first and so on." So first becomes last, second becomes first => up shift: element at row 2 goes to row1, row3 to row2, ..., row1 to bottom. That's exactly an upward rotation: each element moves up one, top wraps to bottom. Our implementation: save top, move each up, assign saved to bottom. That yields: new col: [second, third, ..., last, first] (top to bottom). That's correct.
Now potential off-by-one: When we saved top and moved others up, we essentially performed a shift up (like in a queue). Verified.
Thus solution is done.
Now let's consider if there might be any tricky part: Because we are using orig to map current positions to original indices. But we update orig in place. In column shift, we use orig[i][c] = orig[i+1][c] for i from 0 to n-2. This overwrites orig[0][c] with orig[1][c] before we have used orig[0][c] elsewhere. That's fine because we saved orig[0][c] first.
All good.
Now confirm that uninitialized init values set to 0 are acceptable. The problem statement: "All output integers should not exceed 109 by their absolute value." 0 is fine. It doesn't require them to be within any other constraints.
Now we should also consider that the initial matrix might need to satisfy some constraints that are not simply assignments, e.g., if a type3 occurs, it constraints a specific original cell. Could there be constraints that indirectly impose values on other cells? No, because each observation only relates to one original cell directly via the permutation. There's no constraint linking different cells. So independent assignments.
Thus solution is trivial after simulation.
Now the only possible pitfall is if the same original cell gets two different values; we might set init to last one; if they were different, that would produce an invalid matrix, but the problem guarantees consistency, so it won't happen in test data.
Alternatively, we could store constraints in a map and check; not necessary.
Now let's think about correctness proof: Since we maintain the exact permutation of cells, each type3 gives an equation A[orig] = x. At the end, we assign values to A accordingly. If we simulate the operations from this A using the same permutation (or actually apply the operations to A), the values at observation times will match because we explicitly satisfied each equation. This is because the movement of cells is independent of their values; it's just a permutation. So it's correct.
Thus final answer: implement as described.
Now some may think about using row/col offsets, but this is simpler.
Now let's test with some custom cases.
Case 1: n=1,m=1. Only one cell. Any operation? q=2: type1 1 (row shift does nothing), type3 1 1 42. Sim: orig[0][0] initially (0,0). After row shift: no change. Query: orig[0][0]=(0,0) so init[0][0]=42. Output 42. That's fine.
Case 2: n=1,m=3. Row shift left moves elements. Column shift up doesn't exist because n=1? Actually column shift up on column c would shift the column of size 1, so no effect. So only row shifts matter. Sim: we can test.
Now code complexity is fine.
Now produce final answer: I'll output the code.
But the problem statement: "The first line of the input contains three integers n, m and q (1 ≤ n, m ≤ 100, 1 ≤ q ≤ 10 000) — dimensions of the matrix and the number of turns in the experiment, respectively." Then each operation line. Output any valid initial matrix.
So final code.
One more nuance: In row shift, we used for j from 0 to m-2. If m=1, loop does nothing, we set orig[r][0] = tmp which was orig[r][0] (saved), so no change. That's fine.
Similarly for column shift with n=1.
Now output: each line with m integers separated by spaces. The examples have trailing space but it's okay.
Now final answer: Provide solution explanation and code.
Given that the user might want an explanation, I'll provide a thorough reasoning.
Thus the final answer. We can simulate the sequence of operations while tracking which original cell currently occupies each position. For each query of type 3, we note the required value for the corresponding original cell. At the end, we output an initial matrix that assigns these values (and zeros elsewhere). The simulation is efficient because the matrix size is at most 100×100 and there are at most 10 000 operations.
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, m, q;
cin >> n >> m >> q;
// orig[i][j] = (original row, original column) of the element currently at (i,j)
vector<vector<pair<int,int>>> orig(n, vector<pair<int,int>>(m));
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
orig[i][j] = {i, j};
// initial matrix (unconstrained values will stay 0)
vector<vector<int>> init(n, vector<int>(m, 0));
for (int t = 0; t < q; ++t) {
int type;
cin >> type;
if (type == 1) {
int r;
cin >> r;
r--;
// cyclic left shift of row r
auto tmp = orig[r][0];
for (int j = 0; j < m-1; ++j)
orig[r][j] = orig[r][j+1];
orig[r][m-1] = tmp;
} else if (type == 2) {
int c;
cin >> c;
c--;
// cyclic up shift of column c
auto tmp = orig[0][c];
for (int i = 0; i < n-1; ++i)
orig[i][c] = orig[i+1][c];
orig[n-1][c] = tmp;
} else { // type == 3
int r, c, x;
cin >> r >> c >> x;
r--; c--;
auto [orig_r, orig_c] = orig[r][c];
init[orig_r][orig_c] = x; // the experiment forces this value
}
}
// output any valid initial matrix
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (j) cout << ' ';
cout << init[i][j];
}
cout << '\n';
}
return 0;
}