Magic Cube

시간 제한1초메모리 제한2048 MB

요약
x, y, z축을 기준으로 일부 층을 누적해서 회전시키면서 n x n x n 큐브의 각 칸에 있는 번호를 관리하고, 질의한 위치의 번호를 출력한다.
난이도

어려움10점 중 8점

유형
구현, 시뮬레이션, 수학, 행렬
정답자
아직 제출이 없습니다

문제

Imagine you are holding an n×n×nn \times n \times n cube, which is split up into n3n^3 smaller cubes labeled from 1 to n3n^3. The orientation of the axes is left-to-right for the xx-axis, back-to-front for the yy-axis, and bottom-to-top for the zz-axis. For example, a 2×2×22 \times 2 \times 2 cube is labeled as such:

Bottom layer (z=1z=1):

1 2 
3 4

Top layer (z=2z=2):

5 6 
7 8

In the context of a 2×2×22 \times 2 \times 2 cube:

  • Cube 1 is at (1, 1, 1).
  • Cube 2 is at (2, 1, 1).
  • Cube 3 is at (1, 2, 1).
  • Cube 5 is at (1, 1, 2).

Each time you rotate the cube at slice kk along one of the xx-, yy-, and zz- axes, you are rotating the (k+1)(k+1)th layer along the corresponding axis, as well as all the layers after kk in the increasing direction of that axis.

입력

The first line contains two integers, nn (2≤n≤1,0002 \leq n \leq 1\\,000) and mm (1≤m≤2,0001 \leq m \leq 2\\,000), the size of the cube and the number of operations.

Each of the next mm lines contains the information regarding an operation, and will be one of the following:

  • x, θ\theta, kk: Rotate slices k+1k+1 through slice nn by θ\theta degrees counterclockwise around the xx-axis.
  • y, θ\theta, kk: Rotate slices k+1k+1 through slice nn by θ\theta degrees counterclockwise around the yy-axis.
  • z, θ\theta, kk: Rotate slices k+1k+1 through slice nn by θ\theta degrees counterclockwise around the zz-axis.
  • q x y z: This is a query operation. Output which cube is at location (x,y,z)(x, y, z).

For the first three operations, it is guaranteed that 0≤k≤n−10 \leq k \leq n-1 and θ∈90,180,270,360\theta \in \\{90, 180, 270, 360\\}. For queries, (x,y,z)(x, y, z) denotes the query location and 1≤x,y,z≤n1 \leq x, y, z \leq n. It is guaranteed there will be at least one query. The cube does not reset between operations. That is, rotations are cumulative.

출력

For each query operation, output which cube is at the given location.

예제3

  1. 예제 1

    입력
    2 8
    x 360 1
    y 360 1
    q 1 1 2
    z 90 1
    x 360 1
    q 1 2 1
    q 2 1 1
    q 2 2 2
    
    예상 출력
    5
    3
    2
    7
    
  2. 예제 2

    입력
    2 7
    x 180 1
    q 1 1 1
    q 1 1 2
    y 270 1
    q 2 1 1
    q 2 1 2
    q 2 2 1
    
    예상 출력
    1
    5
    8
    4
    2
    
  3. 예제 3

    입력
    3 7
    y 270 1
    q 1 1 1
    q 1 2 3
    z 360 2
    q 3 2 1
    q 2 2 2
    q 3 3 3
    
    예상 출력
    1
    4
    24
    14
    25