PRINTMOVE2 - Move it, please! Part 2
After helping Lius, your best friend, he survived the trial from the company (see SPOJ PRINTMOVE for clarity). Now he has to deal with the real job. His boss now asked him to handle even more data. Lius was also given more commands than before. The commands available now are:
- "up" : Shift the data 1 row up, the data in top row is moved to the bottom row.
- "down" : Shift the data 1 row below, the data in bottom row is moved to the top row.
- "left" : Shift the data 1 columns to the left, the data in the first column is moved to the last column.
- "right" : Shift the data 1 columns to the right, the data in the last column is moved to the first column.
- "print x y": Print the value on the yth (1 ≤ y ≤ N) row and xth (1 ≤ x ≤ M) column.
- "modify x y a": Modify the value on the yth (1 ≤ y ≤ N) row and xth (1 ≤ x ≤ M) column with a(a will fit an 64-bit unsigned data type).
- "reset x y": Set the value on the yth row and xth column to 0.
Knowing that he is already incapable of handling the first task, he pleads you to help him create a program to help him finish his work. You would help him, wouldn't you?
Warning: large Input/Output data, be careful with certain languages!
Both input and output file of every testcase will not exceed 25MB
PS: For speed addict, my best solution is 2.11s for all test files
First line of input consists of integer N and M (1 ≤ N,M ≤ 10^9), denoting the number of rows and columns of the matrix. The next line of input is an integer C (0 ≤ C ≤ 10^6), denoting the number of commands that should be done, followed by C lines, the orders that should be done by the program.
For each time he is asked to print the value, print"Command #X: A",where X (1 ≤ X ≤ C) is the number of times he is asked to print the value, and A is the value that is asked by his boss. There must be no trailing spaces at the end of printed lines, neither empty characters. Print a newline after each Commands.
Input: 3 3 10 print 2 2 up left print 1 1 modify 2 2 12345 down right print 3 3 reset 2 2 print 2 2 Output: Command #1: 5 Command #2: 5 Command #3: 12345 Command #4: 0
PS: The answer for the date and month in my problem GUESSBDAY is the date I created this problem :), which is also a week before the Independence Day of my country .
Thank you for your attention @hanstan I now have a correct answer with the exact first code previously submitted.
I am missing something here, don't know what. I moved from a simple solution that would have taken too long, and tried two different more efficient versions, but wrong answers. I do feel I understand the problem having halved the previous best time in Part I at the first attempt. Since no-one has answered this problem correctly I wonder if @hanstan can check his own solution.