P18105 [COCI 2026/2027 #2] Robot

Description

Robi the robot works in a warehouse shaped like a **strip of $L$ cells**, labelled $1$ to $L$ from left to right. Initially, Robi is on cell $p$, and each of $n$ other cells contains **exactly one box**. The boxes are labelled $1$ to $n$ from left to right, and box $i$ is initially on cell $x_i$. ![](https://cdn.luogu.com.cn/upload/image_hosting/qvtzavde.webp) Robi moves **step by step**. In one step, he tries to move to the adjacent cell, to the left or to the right of the cell he is on. If that cell contains a box, Robi **pushes it in front of him**, together with all the boxes that are directly behind it in the direction of movement, with no empty cell between them. Robi and all the boxes he is pushing then move by one cell in the direction of movement. There is a **wall** to the left of cell $1$ and to the right of cell $L$. If, in some step, Robi or one of the boxes he is pushing would have to move past the end of the strip, that step is **not performed** and nothing moves. Robi will receive $q$ commands, one after another, each in one of the following three forms: - `L d` – Robi tries $d$ times in a row to make a step **to the left**, - `D d` – Robi tries $d$ times in a row to make a step **to the right**, - `? j` – Robi wants to know on which cell box $j$ is currently located. Help Robi and answer all of his questions.

Input Format

The first line contains positive integers $L$, $n$ and $q$ ($2 \le L \le 10^{18}$, $1 \le n, q \le 200\,000$, $n < L$), the number of cells, the number of boxes and the number of commands. The second line contains a positive integer $p$ ($1 \le p \le L$), the cell on which Robi is initially located. The third line contains $n$ positive integers $x_1, x_2, \ldots, x_n$ ($1 \le x_1 < x_2 < \cdots < x_n \le L$, $x_i \ne p$), the cells on which the boxes are initially located. Each of the following $q$ lines contains the description of one command in one of the forms given above. For commands of the form `L d` and `D d` it holds that $1 \le d \le 10^{18}$, and for commands of the form `? j` it holds that $1 \le j \le n$. There is **at least one** command of the form `? j`.

Output Format

For each command of the form `? j`, in the order in which they are given, output in a separate line the cell on which box $j$ is located at that moment.

Explanation/Hint

### Explanation of example 1 Robi is on cell $3$, and the only box is on cell $5$. After the first command, Robi is on cell $4$, right next to the box, which has not moved yet. With the second command, Robi moves to cell $5$ and pushes the box to cell $6$. The box is now next to the wall, so Robi cannot make any step with the third command. ### Explanation of example 2 With the first command, Robi reaches box $1$ after one step and pushes it to cell $1$ in the next step. The box is then next to the wall, so Robi cannot make the remaining three steps and stays on cell $2$. With the command `D 3`, Robi moves to cell $5$, right next to box $2$, and with the command `D 1` he pushes it to cell $7$. With the command `L 2`, Robi returns to cell $4$, while the boxes stay where they are. ### Scoring | Subtask | Points | Constraints | | --- | ---: | --- | | 1 | 9 | $n = 1$ | | 2 | 14 | $n, q \le 2000$ | | 3 | 16 | Initially, all boxes are to the right of Robi, i.e. $p < x_1$. | | 4 | 31 | No additional constraints. |