Boolean Expression
Time limit3sMemory limit512 MB
Given a fully parenthesized boolean expression of 0, 1, &, |, and ^, print its value and its value after each of m single-character replacements.
- Level
Medium7 of 10
- Topics
- Segment tree, Tree, String
- Solved
- No attempts yet
Problem
You are given a boolean expression made of 0, 1, the operators & (boolean AND), | (boolean OR), ^ (XOR, exclusive OR), and parentheses. A correct boolean expression is defined recursively. It is either a single character 0 or 1, or a boolean operation applied to two correct boolean expressions. For simplicity, every operation is wrapped in parentheses. The expression contains no spaces or other characters. For example, ((0|1)|0), (0&1), and 0 are correct, while 0|1, 0|1&1, and (0) are not. Compute the value of the expression. The expression changes over time. You are also given queries, and each query replaces the character at some position. After each query, compute the value of the expression again.
Input
The first line contains a correct boolean expression with at most characters. The second line contains an integer (), the number of queries. Each of the next lines contains an integer and a character separated by a space, meaning that the character at position is changed to . The expression is guaranteed to remain correct after every query.
Output
Print characters 0 or 1 on a single line. The first is the value of the original expression, and the next characters are the values after each query, in order.