Ivan Smirnov

아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

https://stackoverflow.com/questions/45658828/number-of-ways-to-merge-2-parenthesis-sequences. Now you can solve a strictly harder problem in about 101810^{18} years. This almost fits in the time limit. All you have to do is some minor optimizations.

Two sequences of parentheses ss and tt mergeable if they can be interleaved to form a balanced parentheses sequence. Formally, if ss has length nn and tt has length mm they are mergeable if and only if there exists a balanced parentheses sequence pp of length n+mn + m, and two disjoint sequences of indices aa and bb of length nn and mm respectively, such that:

  1. 0a_1<a_2<<a_n<n+m0 \leq a\_1 < a\_2 < \ldots < a\_n < n + m
  2. 0b_1<b_2<<b_m<n+m0 \leq b\_1 < b\_2 < \ldots < b\_m < n + m
  3. For all ii p_a_i=s_ip\_{a\_i} = s\_i
  4. For all ii p_b_i=t_ip\_{b\_i} = t\_i

The RLE (Run Length Encoding) of some string is a list of pairs (c_i,a_i)(c\_i, a\_i) where c_ic\_i is a character and a_ia\_i is a positive integer and _ic_ic_i+1\forall\_i c\_i \neq c\_{i+1}, the length of the encoding (i.e. number of pairs in the list) is called the run length of the string. The original string of RLE \[(c_1,a_1),(c_2,a_2),,(c_n,a_n)]\[(c\_1, a\_1), (c\_2, a\_2), \ldots, (c\_n, a\_n)] consists of a_1a\_1 repetitions of character c_1c\_1, followed by a_2a\_2 repetitions of character c_2c\_2, and so on, and ends with a_na\_n repetitions of character c_nc\_n. It can be shown that RLE is unique.

You are given an RLE of a parentheses sequence ss and RLEs of mm other parentheses sequences candidates t_it\_i.

For each ii find out if t_it\_i and ss are mergeable.

입력

The input starts with a description of the sequence ss. The first line contains a single integer nn (1n31051 \leq n \leq 3 \cdot 10^5), the run length of ss.

nn lines follow. ii-th of them describes a single pair in the RLE of ss and contains a character c_ic\_i and an integer a_ia\_i separated by a single space (c_i(,),c_ic_i+1,1a_i1012c\_i \in \\{ (, ) \\}, c\_i \neq c\_{i+1}, 1 \leq a\_i \leq 10^{12}).

The next line contains a single integer mm (1n31051 \leq n \leq 3 \cdot 10^5), the number of sequences tt to check.

The remaining lines describe those sequences one by one, using the same format as the description of ss. If this isn't clear you should take a look at the samples.

It is guaranteed that the sum of run lengths of those mm sequences doesn't exceed 31053 \cdot 10^5.

출력

Print mm lines.

The ii-th of those lines should contain 1 if t_it\_i and ss are mergeable and 0 if they are not.