아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Ivan Smirnov

시간 제한2초메모리 제한512 MB

요약
런 렝스 인코딩된 두 괄호 문자열이 주어질 때, 두 문자열을 교차시켜 올바른 괄호 문자열을 만들 수 있는지 판별한다.
난이도

보통10점 중 7점

유형
문자열 매칭, 그리디, 구현
정답자
아직 제출이 없습니다

문제

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. 0≤a_1<a_2<…<a_n<n+m0 \leq a\_1 < a\_2 < \ldots < a\_n < n + m
  2. 0≤b_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_i≠c_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 (1≤n≤3⋅1051 \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_i≠c_i+1,1≤a_i≤1012c\_i \in \\{ (, ) \\}, c\_i \neq c\_{i+1}, 1 \leq a\_i \leq 10^{12}).

The next line contains a single integer mm (1≤n≤3⋅1051 \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 3⋅1053 \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.

예제4

  1. 예제 1

    입력
    4
    ( 1
    ) 3
    ( 3
    ) 1
    1
    2
    ( 1
    ) 1
    
    예상 출력
    0
    
  2. 예제 2

    입력
    2
    ( 2
    ) 1
    4
    1
    ( 1
    1
    ) 1
    2
    ) 2
    ( 1
    2
    ) 4
    ( 3
    
    예상 출력
    0
    1
    1
    0
    
  3. 예제 3

    입력
    4
    ) 2
    ( 3
    ) 5
    ( 100
    3
    1
    ) 96
    2
    ( 132
    ) 228
    4
    ( 2
    ) 3
    ( 5
    ) 100
    
    예상 출력
    0
    1
    1
    
  4. 예제 4

    입력
    1
    ) 1000000000000
    2
    1
    ) 1000000000000
    1
    ( 1000000000000
    
    예상 출력
    0
    1