ccf#CSPJ2020C. 表达式 (Expression)
表达式 (Expression)
Description
Little C is eager to learn mathematical logic. One day, he discovered a special logical expression. In this kind of logical expression, all operands are variables and can only take the values and . The calculation proceeds from left to right. If there are parentheses in the expression, the value of the sub-expression inside the parentheses is calculated first. In particular, this expression has only the following operations:
- AND operation:
a & b
. If and only if the values of both and are , the value of the expression is . Otherwise, the value of the expression is . - OR operation:
a | b
. If and only if the values of both and are , the value of the expression is . Otherwise, the value of the expression is . - Negation operation:
!a
. If and only if the value of is , the value of the expression is . Otherwise, the value of the expression is .
Given a logical expression and the initial value of each operand, Little C wants to know the value of the original expression when the value of an operand is inverted.
In order to simplify the processing of expressions, we have the following conventions:
The expression will be given in the form of a postfix expression.
A postfix expression is defined as follows:
- If is an operand, the postfix expression of is itself.
- If is an expression of the form , where is any binary operator with precedence no higher than the precedences of the operators outside the parentheses in and , then the postfix expression of is , where and are the postfix expressions of and respectively.
- If is an expression of the form , the postfix expression of is the postfix expression of .
For convenience while taking input, there is a space around the AND operator (&
), the OR operator (|
), and the negation operator (!
), but there is no space at the end of the input expression.
An operand is formed by concatenating the lowercase letter x
with a positive integer which represents the subscript of this variable. For example, x10
represents the variable . It is guaranteed that each variable appears exactly once in the expression.
Input Format
The first line contains a string denoting the expression as described above.
The second line contains a positive integer denoting the number of variables in the expression. The subscripts of the variables in the expression are .
The third line contains integers, where the -th integer denotes the initial value of the variable .
The fourth line contains a positive integer denoting the number of queries.
Each of the next lines contains a positive integer denoting the subscript of the variable that needs to be inverted. Note that the modification done in each query is temporary, i.e., the modification done in the previous query doesn't affect the subsequent query.
It is guaranteed that the given expression is valid and the initial value of each variable is or .
Output Format
Print lines, one per query, containing the value of the expression ( or ) for that query.
x1 x2 & x3 |
3
1 0 1
3
1
2
3
1
1
0
The infix expression equivalent to the given postfix expression is .
In the first query, the value of is inverted. Now the values of the three variables are . The value of the original expression is .
In the second query, the value of is inverted. Now the values of the three variables are . The value of the original expression is .
In the third query, the value of is inverted. Now the values of the three variables are . The value of the original expression is .
x1 ! x2 x4 | x3 x5 ! & & ! &
5
0 1 0 1 1
3
1
3
5
0
1
1
The infix expression equivalent to the given postfix expression is .
Constraints
For of the data, the expression contains only the AND operation (&
) or only the OR operation (|
).
For another of the data, .
For another of the data, the initial values of the variables are all or all .
For of the data, $1 \le |s| \le 10^6, 1 \le q \le 10^5, 2 \le n \le 10^5$.
represents the length of the string .