Under construction

Still being built. Things may change

Boolean Expression

Time limit3sMemory limit512 MB

Summary
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 mm 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 SS with at most 800 000800\,000 characters. The second line contains an integer mm (1≤m≤400 0001 \le m \le 400\,000), the number of queries. Each of the next mm lines contains an integer pip_i and a character cic_i separated by a space, meaning that the character at position pip_i is changed to cic_i. The expression is guaranteed to remain correct after every query.

Output

Print m+1m+1 characters 0 or 1 on a single line. The first is the value of the original expression, and the next mm characters are the values after each query, in order.

Examples1

  1. Example 1

    Input
    ((1&1)^(0|1))
    3
    11 0
    7 |
    5 0
    
    Expected output
    0110