#P4041. JYY's Calculator Simulation
JYY's Calculator Simulation
JYY's Calculator Simulation
JYY's calculator can execute \(N\) preset instructions. Every time JYY inputs a positive integer \(X\), the calculator uses \(X\) as the initial value and then sequentially executes the \(N\) instructions. After execution, the final result is returned to JYY.
Each instruction is one of the following four types (here \(a\) is a positive integer):
- \(+a\): Add \(a\) to the current result, i.e. \(result \gets result + a\).
- \(-a\): Subtract \(a\) from the current result, i.e. \(result \gets result - a\).
- \(\times a\): Multiply the current result by \(a\), i.e. \(result \gets result \times a\).
- \(@a\): Add \(a \times X\) to the current result (where \(X\) is the input value), i.e. \(result \gets result + a \times X\).
The calculator has a constrained memory that can only store values in the range \([L, R]\) (both inclusive). During computation, if any operation results in a value greater than \(R\), the value is immediately set to \(R\); similarly, if the result is less than \(L\), it is set to \(L\). The updated value is then used in subsequent operations.
Your task is: given \(N\), \(L\), \(R\), a list of \(N\) instructions, and \(Q\) queries, for each query (each query provides a positive integer \(X\)), simulate the execution of the instructions and output the final result.
Note that all formulas are presented in \(\LaTeX\) format.
inputFormat
The first line contains four integers \(N\), \(L\), \(R\), \(Q\) where:
- \(N\) is the number of instructions.
- \(L\) and \(R\) represent the inclusive lower and upper bounds for stored values.
- \(Q\) is the number of queries.
The next \(N\) lines each contain an instruction in one of the following forms: +a
, -a
, *a
(representing \(\times a\)), or @a
.
The following \(Q\) lines each contain a positive integer \(X\), the input to the calculator for that query.
outputFormat
For each query, output a single line containing the final result after sequentially executing the \(N\) instructions starting with \(X\), applying the clamping after each operation.
sample
3 1 6 3
+5
-3
@2
2
5
1
6
6
5
</p>