roboti — COCI CONTEST_#4
Task type: batch | Time limit: 2 s | Memory limit: 512 MB | Difficulty: 16
Algorithms: data structures, bfs, sparse grid, graph theory

# Task Roboti

Kile, a board games enthusiast, recently discovered the game Robots. The game consists of a board with *n* rows and *m* columns and one robot. The field (1, 1) is the top-left field of the board, while the field (*n*, *m*) is the bottom-right.

At the beginning, the robot is positioned on some field (*x*, *y*) (*x*-th row, *y*-th column), and the player can direct it in one of the four directions: up, down, left, or right. Depending on the chosen direction, it will move in that direction until it encounters its goal or a special field on the board. If at any point it wants to exit
![alt text](_page_0_Figure_5.jpeg)
the board, it wraps around to the other side. For example, if it is located at the field (*n*, 3) and wants to move down, it will arrive at the field (1, 3).

The board has three types of fields:

- Empty field - the robot continues moving in the same direction.
- Left turn field - when the robot steps on this field, it will turn left by 90° and continue moving.
- Right turn field - when the robot steps on this field, it will turn right by 90° and continue moving.

Most fields on the board are empty, only *k* of them are left or right turn fields.

The game consists of *q* rounds. In the *i*-th round of the game, the robot will be placed on the field (*a<sub>i</sub>*, *b<sub>i</sub>*). The goal is to reach the field (*c<sub>i</sub>*, *d<sub>i</sub>*) using the minimum number of turns, or determine that it is impossible.

After playing this game several times, Kile realized that it is more challenging than it initially seemed. That is why he needs your help now. Help him determine the minimum number of turns required for each round of the game!

*Note*: If the robot starts or finishes its path on a left or right turn field, that turn is not counted.

## Input

The first line contains integers *n*, *m*, and *k* (1 ≤ *n*, *m* ≤ 10<sup>6</sup>, 0 ≤ *k* ≤ 10<sup>5</sup>), the number of rows, columns, and non-empty fields on the board.

The *i*-th of the following *k* lines contains integers *x<sub>i</sub>*, *y<sub>i</sub>*, and character *s<sub>i</sub>* (1 ≤ *x<sub>i</sub>* ≤ *n*, 1 ≤ *y<sub>i</sub>* ≤ *m*, *s<sub>i</sub>* = 'L' or *s<sub>i</sub>* = 'R'), the row and column of the *i*-th turn field and the type of turn. If *s<sub>i</sub>* = 'L', then it is a left turn field. If *s<sub>i</sub>* = 'R', then it is a right turn field.

The next line contains integer *q* (1 ≤ *q* ≤ 3 ⋅ 10<sup>5</sup>), the number of rounds.

The *i*-th of the following *q* lines contains integers *a<sub>i</sub>*, *b<sub>i</sub>*, *c<sub>i</sub>*, *d<sub>i</sub>* (1 ≤ *a<sub>i</sub>*, *c<sub>i</sub>* ≤ *n*, 1 ≤ *b<sub>i</sub>*, *d<sub>i</sub>* ≤ *m*), the starting position and the goal.

## Output

In the *i*-th of the following *q* lines, output the minimal number of turns for the *i*-th round of the game or '-1' if it is impossible to reach the goal.

![alt text](_page_1_Picture_0.jpeg)

## Scoring

| Subtask | Points | Constraints                |
|---------|--------|----------------------------|
| 1       | 10     | k = 0                      |
| 2       | 13     | *n*, *m* ≤ 300, *q* ≤ 10  |
| 3       | 49     | *n*, *m* ≤ 300            |
| 4       | 38     | No additional constraints. |

## Examples

| Input   | Input   | Input   |
|---------|---------|---------|
| 2 2 2   | 3 3 4   | 4 4 8   |
| 1 1 L   | 1 1 L   | 1 1 R   |
| 2 2 R   | 1 3 L   | 1 3 L   |
| 5       | 2 1 L   | 2 2 R   |
| 1 1 2 2 | 3 3 L   | 2 4 L   |
| 2 1 1 2 | 7       | 3 1 L   |
| 1 1 1 2 | 1 1 3 3 | 3 3 L   |
| 2 1 1 1 | 3 3 2 1 | 4 2 L   |
| 2 2 2 1 | 3 1 2 2 | 4 4 L   |
|         | 2 3 1 2 | 7       |
| Output  | 2 3 3 1 | 1 2 1 4 |
|         | 1 2 3 2 | 2 2 3 4 |
| -1      | 3 3 2 2 | 4 4 3 2 |
| 1       |         | 4 1 4 4 |
| 0       | Output  | 4 2 3 1 |
| 0       | 1       | 1 2 2 3 |
| 0       | 2       | 2 4 2 3 |
|         | 1       |         |
|         | 1       | Output  |
|         | 1       |         |
|         | 0       | 2       |
|         | 3       | 1       |
|         |         | 1       |
|         |         | 0       |
|         |         | -1      |
|         |         | 5       |
|         |         | 0       |

## Clarification of the second example:

First round: We start at (1, 1). If we direct the robot to the left, it will arrive at (1, 3) in the next step because it wanted to exit the board, so it wraps around to the other side. Field (1, 3) is a left turn field, so the robot is now directed downwards. After two more steps, it will be at the desired goal (3, 3).

Second round: We start at (3, 3). If we direct the robot upwards, it will arrive at (1, 3) in two steps, where it will be directed to the left due to the left turn field. After two steps, it will be at the field (1, 1), which is also a left turn field, so it will be directed downwards. In the next step, it will be at the desired goal (2, 1).

Starter code: