#P11186. Evaluate Ternary Constant Expression

    ID: 13252 Type: Default 1000ms 256MiB

Evaluate Ternary Constant Expression

Evaluate Ternary Constant Expression

This problem involves evaluating a segmented constant expression that is defined using a ternary operator. The ternary operator in this problem acts similarly to an if statement and has the following syntax:

\texttt{condition ? value_1 : value_2}

When the condition is true, the result of the expression is value_1; otherwise it is value_2. In this problem, the condition is one of the forms x > a or x < a, where a is a positive decimal integer. Furthermore, a segmented constant expression S is defined as follows:

  • A positive decimal integer (e.g. 243) is a segmented constant expression.
  • If a is a positive decimal integer and p, q are segmented constant expressions, then $$\texttt{x > a ? p : q}$$ is a segmented constant expression.
  • If a is a positive decimal integer and p, q are segmented constant expressions, then $$\texttt{x < a ? p : q}$$ is also a segmented constant expression.

For example, the expression x>154?220:x<37?16:10 is a segmented constant expression because both 220 and x<37?16:10 are segmented constant expressions. When evaluating this expression for a given value of x, you need to follow the ternary operator logic recursively.

You are given a segmented constant expression S. It is guaranteed that any positive integer appearing in S does not exceed a given bound m (note: m is only used as a constraint and is not directly provided in the input). You are also given q queries. Each query provides a non-negative integer value for x, and your task is to evaluate the expression S for each query.

inputFormat

The input consists of multiple lines:

  1. The first line contains the segmented constant expression S (a string). The expression does not contain spaces.
  2. The second line contains an integer q, the number of queries.
  3. The following q lines each contain a non-negative integer x.

You can assume that the expression is valid and every positive integer in it is no more than m.

outputFormat

For each query, print the value of the segmented constant expression when evaluated with the given x. Each result should be output on a separate line.

sample

x>5?8:6
2
7
3
8

6

</p>