Under construction

Still being built. Things may change

Cube

Time limit1.5sMemory limit1024 MB

Summary
Given point recolorings in an N x N x N cube, print the first colored cube seen along each ray after rotating a chosen face to the front.
Level

Medium5 of 10

Topics
Array, Implementation
Solved
No attempts yet

Statement

Imagine a cube of size NN made of N×N×NN \times N \times N unit cubes. Each unit cube starts colorless, so it is transparent and you can see through it. On your request, each unit cube can change color over time.

The cube stands on a glass rotating stand. It has six faces: front, back, left, right, top, and bottom. At the start, the front face faces you at eye level. Looking through the transparent cubes, you see as far as the first colored unit cube.

You get QQ queries. The ii-th query has one of the following two forms:

  • 1 X_i Y_i Z_i C_i: the unit cube in row XiX_i (counting from top to bottom), column YiY_i (counting from left to right), and depth ZiZ_i (counting from front to back) changes its color to CiC_i. The cube is in its initial orientation, with the front face toward you.
  • 2 S_i: rotate the cube so that face SiS_i comes to the front, in front of your eyes, where the front face was before. Then print the N×NN \times N table of colors you now see. If a position is see-through all the way, print 00 for it. Then return the cube to its initial position, so the front face faces you again.

Input

The first line contains natural numbers NN and QQ (2≤N≤1002 \le N \le 100, 1≤Q≤2 0001 \le Q \le 2\,000).

Each of the next QQ lines contains one query of the first or second kind.

  • If the first number of a query is 1, then four more natural numbers XiX_i, YiY_i, ZiZ_i, CiC_i follow (1≤Xi≤N1 \le X_i \le N, 1≤Yi≤N1 \le Y_i \le N, 1≤Zi≤N1 \le Z_i \le N, 1≤Ci≤1 000 000 0001 \le C_i \le 1\,000\,000\,000).
  • If the first number of a query is 2, then a string SiS_i follows. SiS_i is one of prednja, straznja, lijeva, desna, and it names the face to rotate to the front.

Output

For each query of the second kind, print an N×NN \times N table, which is the answer to that query.

Hint

In the first sample, all unit cubes of the 2×2×22 \times 2 \times 2 cube start colorless. The first query asks for the front face. No rotation is needed because that face already faces you. The cube is fully transparent, so the answer is a 2×22 \times 2 table of zeros.

The next two queries change the top-left-front corner cube to color 11 and the bottom-right-back corner cube to color 55. The following two queries ask for the view after rotating the back face to the front, and after rotating the right face to the front. In the first case, the cube of color 11 is in the top-right corner and the cube of color 55 is in the bottom-left corner. In the second case, the view is the same as the view from the front.

In the second sample, the 3×3×33 \times 3 \times 3 cube has its unit cubes in row 22 and depth 22 recolored. Looking from the left, you see only the cube of color 11. Looking from the right, you see only the cube of color 33.

Examples3

  1. Example 1

    Input
    2 5
    2 prednja
    1 1 1 1 1
    1 2 2 2 5
    2 straznja
    2 desna
    
    Expected output
    0 0
    0 0
    0 1
    5 0
    1 0
    0 5
    
  2. Example 2

    Input
    3 5
    1 2 1 2 1
    1 2 2 2 2
    1 2 3 2 3
    2 lijeva
    2 desna
    
    Expected output
    0 0 0
    0 1 0
    0 0 0
    0 0 0
    0 3 0
    0 0 0
    
  3. Example 3

    Input
    2 6
    1 2 1 2 1
    1 1 1 1 3
    1 2 1 2 2
    1 1 1 2 4
    2 prednja
    2 straznja
    
    Expected output
    3 0
    2 0
    0 4
    0 2