#P15280. [MCO 2024] Dragon Attack

    ID: 15302 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>线段树2024MCC/MCO(马来西亚)

[MCO 2024] Dragon Attack

Description

Evirir the dragon has yet again laid siege on MCO land. To defend against Evirir's attack, the inhabitants of MCO land have prepared anti-aircraft guns equipped with dragon scale-piercing ammunition.

MCO land can be modeled as an nn by mm grid with the rows labeled 11 to nn from top to bottom and columns labeled 11 to mm from left to right. The cell in the ithi^{\text{th}} row, and the jthj^{\text{th}} column is denoted by (i,j)(i,j). There is an AA gun in each cell. To better shoot down Evirir the dragon, the guns will follow Evirir the dragon as he flies around MCO land.

Duckmoon, the former president of MCO Land, will issue a series of TT commands to the inhabitants, directing them to move guns. These directions are specified in a string SS of length TT, which is composed of the characters N\tt{N}, S\tt{S}, E\tt{E}, and W\tt{W}. At the ithi^{\text{th}} command, represented by SiS_i, all the guns in MCO land will move in the following manner:

  • If Si=NS_i = \tt{N}, guns are directed northward. A gun at position (i,j)(i,j) moves to (i1,j)(i-1,j) if i>1i > 1; otherwise, it remains stationary.
  • If Si=SS_i = \tt{S}, guns are directed southward. A gun at position (i,j)(i,j) moves to (i+1,j)(i+1,j) if i<ni < n; otherwise, it remains stationary.
  • If Si=ES_i = \tt{E}, guns are directed eastward. A gun at position (i,j)(i,j) moves to (i,j+1)(i,j+1) if j<mj < m; otherwise, it remains stationary.
  • If Si=WS_i = \tt{W}, guns are directed westward. A gun at position (i,j)(i,j) moves to (i,j1)(i,j-1) if j>1j > 1; otherwise, it remains stationary.

Note that a cell can have multiple guns at the same time.

After Evirir the dragon is shot down, the inhabitants of MCO land will need to store away the AA guns. For each pair (i,j)(i,j) with 1in1 \leq i \leq n, 1jm1 \leq j \leq m, the inhabitants of MCO land want to know which cell the gun originally in (i,j)(i,j) will end up in after all TT commands.

An integer is used to describe the final position of each gun. If a gun ends up in cell (x,y)(x, y), its final position is represented by the integer (x1)×m+(y1)(x - 1) \times m + (y - 1).

Input Format

The first line consists of three space-separated integers n,mn,m and TT denoting the number of rows, number of columns, and number of commands given by Duckmoon respectively. It is guaranteed that 1n,m,T1061 \leq n,m,T \leq 10^6 and nm106nm \leq 10^6.

The second line contains a string SS of length TT consisting of the characters N,S,E,andW\tt{N}, \tt{S}, \tt{E}, and \tt{W}.

Output Format

Let (xi,j,yi,j)(x_{i,j},y_{i,j}) be the cell the gun originally in cell (i,j)(i,j) ends up in.

Output nn lines. For the ithi^{\text{th}} line, output mm space-separated integers qi,1,qi,2,...,qimq_{i,1},q_{i,2},...,q_{i_m} where qi,j=(xi,j1)×m+(yi,j1)q_{i,j} = (x_{i,j}-1) \times m + (y_{i,j} - 1).

3 4 5
NEESW
5 6 6 6
5 6 6 6
9 10 10 10
7 5 20
WSSWNSENSNWENESSNSEW
12 12 12 13 13
17 17 17 18 18
22 22 22 23 23
27 27 27 28 28
32 32 32 33 33
32 32 32 33 33
32 32 32 33 33

Hint

Note

Here is the visualization for Example 1, showing the coordinates of the original AA gun and how they are directed based on the commands.

$$\begin{array}{|c|c|c|c|c|} \hline & \text{Column 1} & \text{Column 2} & \text{Column 3} & \text{Column 4} \\ \hline \text{Row 1} & (1,1) & (1,2) & (1,3) & (1,4) \\ \hline \text{Row 2} & (2,1) & (2,2) & (2,3) & (2,4) \\ \hline \text{Row 3} & (3,1) & (3,2) & (3,3) & (3,4) \\ \hline \end{array}$$

After the command N, they move northward.

$$\begin{array}{|c|c|c|c|c|} \hline & \text{Column 1} & \text{Column 2} & \text{Column 3} & \text{Column 4} \\ \hline \text{Row 1} & (1,1), (2,1) & (1,2), (2,2) & (1,3), (2,3) & (1,4), (2,4) \\ \hline \text{Row 2} & (3,1) & (3,2) & (3,3) & (3,4) \\ \hline \text{Row 3} & & & & \\ \hline \end{array}$$

After the command E, they move eastward.

$$\begin{array}{|c|c|c|c|c|} \hline & \text{Column 1} & \text{Column 2} & \text{Column 3} & \text{Column 4} \\ \hline \text{Row 1} & & (1,1), (2,1) & (1,2), (2,2) & (1,3), (1,4), (2,3), (2,4) \\ \hline \text{Row 2} & & (3,1) & (3,2) & (3,3), (3,4) \\ \hline \text{Row 3} & & & & \\ \hline \end{array}$$

After the command E, they move eastward again.

$$\begin{array}{|c|c|c|c|c|} \hline & \text{Column 1} & \text{Column 2} & \text{Column 3} & \text{Column 4} \\ \hline \text{Row 1} & & & (1,1), (2,1) & (1,2), (1,3), (1,4), (2,2), (2,3), (2,4) \\ \hline \text{Row 2} & & & (3,1) & (3,2), (3,3), (3,4) \\ \hline \text{Row 3} & & & & \\ \hline \end{array}$$

After the command S, they move southward again.

$$\begin{array}{|c|c|c|c|c|} \hline & \text{Column 1} & \text{Column 2} & \text{Column 3} & \text{Column 4} \\ \hline \text{Row 1} & & & & \\ \hline \text{Row 2} & & & (1,1), (2,1) & (1,2), (1,3), (1,4), (2,2), (2,3), (2,4) \\ \hline \text{Row 3} & & & (3,1) & (3,2), (3,3), (3,4) \\ \hline \end{array}$$

After the command W, they move westward again.

$$\begin{array}{|c|c|c|c|c|} \hline & \text{Column 1} & \text{Column 2} & \text{Column 3} & \text{Column 4} \\ \hline \text{Row 1} & & & & \\ \hline \text{Row 2} & & (1,1), (2,1) & (1,2), (1,3), (1,4), (2,2), (2,3), (2,4) & \\ \hline \text{Row 3} & & (3,1) & (3,2), (3,3), (3,4) & \\ \hline \end{array}$$

The final position of AA gun which originally placed at coordinate (1,1)(1,1) is at cell (2,2)(2,2), which is 1×4+1=51 \times 4 + 1=5.

Scoring

Subtask 1 (10 points): nmT106nmT \leq 10^6

Subtask 2 (22 points): n=1n=1

Subtask 3 (23 points): SS only consists of the characters N\tt{N} and E\tt{E}.

Subtask 4 (45 points): No additional constraints.