#P4041. [AHOI2014/JSOI2014] 奇怪的计算器

[AHOI2014/JSOI2014] 奇怪的计算器

Description

JYY’s calculator can execute NN preset instructions. Each time JYY inputs a positive integer XX, the calculator uses XX as the initial value, then executes the NN preset instructions in order, and finally returns the resulting value to JYY.

Each instruction is one of the following four types (here aa denotes a positive integer):

  1. +a+a: add aa to the current result.
  2. a-a: subtract aa from the current result.
  3. ×a\times a: multiply the current result by aa.
  4. @a@a: add a×Xa \times X to the current result (where XX is the number JYY initially input).

The variable that stores the computation result has a limited range, so overflow may occur after each computation.

In JYY’s calculator, the result variable can only store positive integers between LL and RR. If after executing an instruction the result exceeds RR, the calculator will automatically change it to RR, then continue from RR. Similarly, if the result is less than LL, the calculator will change it to LL, then continue computing.

For example, suppose the calculator can store values from 11 to 66. If the current result is 22, then after executing +5+5, the stored value will be 66. Although the actual result 2+52+5 is 77, since 77 exceeds the upper bound, the result is clamped to the upper bound 66.

JYY plans to input QQ values into the calculator and wants to know what result each input will produce.

Input Format

The first line contains three positive integers NN, LL, and RR.

The next NN lines each contain one instruction. Each instruction, as described above, consists of one symbol and one positive integer, separated by a space.

The (N+2)(N+2)-th line contains an integer QQ, the number of values JYY will input.

The next QQ lines each contain a positive integer. The kk-th positive integer XkX_k is the integer JYY inputs on the kk-th time.

Output Format

Output QQ lines, each with a positive integer. The integer on the kk-th line is the result obtained after inputting XkX_k and then executing the NN instructions in order.

5 1 6
+ 5
- 3
* 2
- 7
@ 2
3
2
1
5
5
3
6

Hint

Sample Explanation

When JYY inputs 22, the calculator performs 55 operations. After each operation, the results are 66 (the actual result is 77 but it exceeds the upper bound), 33, 66, 11 (the actual result is 1-1 but it is below the lower bound), and 55 (since the initial input was 22, this step computes 1+2×21 + 2 \times 2).

Constraints

For all testdata, 1N,Q1051 \le N, Q \le 10^5, 1LXkR1091 \le L \le X_k \le R \le 10^9, 1a1091 \le a \le 10^9.

Translated by ChatGPT 5