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.
The initial data is in a form of a matrix of N×M. At first, the data were numbered from 1 to N*M and sorted from top-left to the bottom-right.
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

Input

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.

Output

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 command.

Example

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 .


Added by:hanstan
Date:2016-08-10
Time limit:0.5s-1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 GOSU
Resource:Self

hide comments
2017-04-21 16:07:19
Thank you for your attention @hanstan I now have a correct answer with the exact first code previously submitted.
2017-03-24 20:06:31
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.
Re: I'm sorry for the inconvenience. It seems that there is a corrupted output test case when I was uploading it(since each file is almost 10MB TBH), and somehow my solutions still got an AC. I compared all of my test cases with your outputs manually and it works fine. I suggest you to resubmit it :)

Last edit: 2017-04-06 16:28:12
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.