Under construction

Still being built. Things may change

Colorful Snake

Interview

Time limit32sMemory limit1024 MB

Summary
Simulate a snake that moves on an m by m board, eats colored snacks, and report the color of the segment in a queried cell after each query.
Level

Medium4 of 10

Topics
Simulation, Hash map, Queue
Solved
No attempts yet

Problem

Years ago, Bajtazar played the Snake game on his old mobile phone. Today he works as a programmer, and out of nostalgia he wants to write his own version. It is 2022, so he wants to add some color and move the game to a much larger computer screen. Your task is to help him write one module of the game.

The game is played on a square board with mm rows and mm columns, so the board has m2m^2 cells. Rows are numbered 1 to mm from top to bottom, and columns are numbered 1 to mm from left to right. All colors are numbered from 0 to m2−1m^2-1.

At the start, a snake made only of its head is in the cell at row 1, column 1. The head has color 0. There are pp snacks on the board. Each snack occupies one cell and has a color. Snack colors may repeat.

In each time unit, the player moves the head one cell up, down, left, or right. The rest of the snake follows the head. The cell the head occupied before the move is now occupied by the second segment, the cell of the second segment is taken by the third segment, and so on. The head never moves into the cell of the first segment behind it, so the snake never turns back. The head may not leave the board or enter a cell that holds another segment of the snake. If it does, the player loses.

When the head moves onto a cell with a snack, the snake eats it. The snack disappears from the board and the snake grows by one segment. The head takes the color of the eaten snack and ends the move on that cell. The other segments do not move during that move.

You may assume that the snake never leaves the board and never enters a cell occupied by its own segments.

Input

The first line contains three integers mm, pp, and nn. They are the side length of the board, the number of snacks on the board, and the number of commands to process.

Each of the next pp lines contains three integers wiw_i, kik_i, and cic_i. This means there is a snack of color cic_i in row wiw_i, column kik_i. Here 1≤wi,ki≤m1 \le w_i, k_i \le m and 0≤ci≤m2−10 \le c_i \le m^2-1. All pairs (wi,ki)(w_i, k_i) are distinct and none of them is the starting cell (1, 1).

Each of the next nn lines is a command. A command is one of the letters G, D, L, or P, or the letter Z followed by two integers wj′w'_j and kj′k'_j. G means the head moved up, D means it moved down, L means it moved left, and P means it moved right. The command Z w' k' asks for the color of the snake segment in row w′w', column k′k' at the current moment. You may assume there is at least one such query.

Output

For each query, print one line with one integer. Print the color of the snake segment occupying the queried cell at that moment, as an integer in the range [0,m2−1][0, m^2-1]. If no segment of the snake occupies that cell at that moment, print -1.

Examples1

  1. Example 1

    Input
    6 5 14
    1 3 1
    5 1 5
    2 3 2
    3 4 1
    3 5 3
    Z 1 1
    Z 1 2
    P
    P
    D
    D
    P
    Z 3 5
    P
    Z 3 5
    D
    Z 3 5
    L
    Z 3 5
    
    Expected output
    0
    -1
    -1
    3
    1
    2