eolymp
bolt
Try our new interface for solving problems
Məsələlər

2D-переворот

2D-переворот

You are given a matrix \textbf{A} containing \textbf{N} rows numbered from \textbf{1} to \textbf{N} and \textbf{M} columns numbered from \textbf{1} to \textbf{M}. Initially the element in row \textbf{i}, column \textbf{j} is \textbf{A_\{i,j\} = (i - 1) · M + j - 1}. Someone has done several transformations to the matrix. Each transformation was either a horizontal reverse or a vertical reverse. Each reverse is described by four integers \textbf{X_1}, \textbf{Y_1}, \textbf{X_2} and \textbf{Y_2} such that \textbf{1} ≤ \textbf{X_1} ≤ \textbf{X_2} ≤ \textbf{N }and \textbf{1 }≤\textbf{ Y_1} ≤ \textbf{Y_\{2 \}}≤ \textbf{M}. After the horizontal reverse the element in row \textbf{i}, column \textbf{j} becomes equal to \textbf{A^\{'\}_\{i,j\} = A_\{i,Y2-j+Y1\}} iff \textbf{X_1} ≤ \textbf{i} ≤ \textbf{X_2} and \textbf{Y_1} ≤\textbf{ j} ≤ \textbf{Y_2}, otherwise it remains the same (\textbf{A^\{'\}_\{i,j\} = A_\{i,j\}}). After the vertical reverse the element in row \textbf{i}, column \textbf{j} becomes equal to \textbf{A^\{'\}_\{i,j\} = A_\{X2-i+X1,j\}} iff \textbf{X_1} ≤ \textbf{i }≤ \textbf{X_2} and \textbf{Y_1} ≤ \textbf{j }≤ \textbf{Y_2}, otherwise it remains the same (\textbf{A^\{'\}_\{i,j\} = A_\{i,j\}}). Below you can see an example of applying a horizontal reverse and then a vertical reverse. \includegraphics{https://static.e-olymp.com/content/37/37a4820bebade9fc28e51290070e197b0b9ce3cf.jpg} You should write a program that, given \textbf{N}, \textbf{M} and a sequence of operations, will be able to find the resulting matrix. \InputFile The first line contains two space-separated integers \textbf{N} and \textbf{M} (\textbf{1} ≤ \textbf{N}, \textbf{M} ≤ \textbf{2000}). The next line contains a ninteger \textbf{K}(\textbf{1} ≤ \textbf{K} ≤ \textbf{2000}) the number of operations. Each of the next \textbf{K} lines describes one operation. The first character of the line stands for the type of the operation ('\textbf{H}' for horizontal reverse or '\textbf{V}' for vertical reverse) after which there are four integers \textbf{X_1}, \textbf{Y_1}, \textbf{X_2} and \textbf{Y_2 - }description of the selected rectangular range. Operations are given in order of their applying. \OutputFile The first lines hould contain \textbf{N} space-separated integers corresponding to the sum of the elements in the first, second, ..., \textbf{N}-th row of the matrix after applying all the operations. Similarly, the second line should contain \textbf{M} space-separated integers the sum of the elements in the first, second, ..., \textbf{M}-th column of the resulting matrix.
Zaman məhdudiyyəti 5 saniyə
Yaddaşı istafadə məhdudiyyəti 256 MiB
Giriş verilənləri #1
5 6
2
H 2 2 4 5
V 1 3 5 5
Çıxış verilənləri #1
87 87 87 87 87
60 74 73 72 71 85