Magic Cube
Time limit1sMemory limit2048 MB
Given an n by n by n cube of numbered small cubes and a list of slice rotations, report which cube ends up at each queried position.
- Level
Medium6 of 10
- Topics
- Simulation, Implementation, Matrix
- Solved
- No attempts yet
Problem
Imagine you are holding an cube, which is split into smaller cubes labeled from 1 to . The axes run left to right for the -axis, back to front for the -axis, and bottom to top for the -axis. For example, a cube is labeled as follows.
Bottom layer ():
1 2
3 4
Top layer ():
5 6
7 8
In this cube, cube 1 is at (1, 1, 1), cube 2 is at (2, 1, 1), cube 3 is at (1, 2, 1), and cube 5 is at (1, 1, 2).
Each time you rotate the cube at slice around one of the -, -, or -axes, the th layer and all layers after in that axis's increasing direction rotate along with it.

Input
The first line contains two integers () and (), the size of the cube and the number of operations.
Each of the next lines describes one operation in one of the following forms.
x, , : Rotate slices through by degrees counterclockwise around the -axis.y, , : Rotate slices through by degrees counterclockwise around the -axis.z, , : Rotate slices through by degrees counterclockwise around the -axis.q x y z: Query. Output the cube at location .
For the first three operations, and are guaranteed. For queries, is the location, with . At least one query is guaranteed. The cube is not reset between operations, so rotations accumulate.
Output
For each query operation, output the number of the cube at the given location, one per line.