QOJ.ac

QOJ

実行時間制限: 4.0 s メモリ制限: 1024 MB 満点: 10

#17400. Parentheses entanglement [A]

統計

A shuffle of words $s$ and $t$ is any word formed by interleaving the letters of $s$ and $t$. In other words, the letters in the shuffle can be colored with two colors such that reading only the letters of one color yields exactly the word $s$, and reading only the letters of the other color yields exactly the word $t$.

A word $w$ consisting of opening '(' and closing ')' parentheses is called a valid parenthesis expression if the number of opening parentheses in $w$ is equal to the number of closing parentheses, and in every prefix of $w$, the number of opening parentheses is not less than the number of closing parentheses.

Given two words $s$ and $t$ composed of parentheses, calculate how many pairs $1 \le i \le j \le |t|$ exist such that there is a valid parenthesis expression $w$ which is a shuffle of the word $s$ and the substring $t[i \dots j]$ (i.e., a non-empty fragment of the word $t$ from position $i$ to position $j$).

Input

The first line of input contains the representation of word $s$, and the second line contains the representation of word $t$.

Each of these lines starts with an integer $n$ ($1 \le n \le 100\,000$), followed by a character $c$ which is one of the parentheses '(' or ')', and then a sequence of $n$ integers $a_1, \dots, a_n$ ($1 \le a_i \le 1\,000\,000$). The word encoded in this way starts with the character $c$ repeated $a_1$ times, followed by the other type of parenthesis repeated $a_2$ times, then the character $c$ repeated $a_3$ times, and so on.

Output

Output a single integer — the number of pairs $(i, j)$ for which some shuffle of the word $s$ and the substring $t[i \dots j]$ is a valid parenthesis expression.

Examples

Input 1

3 ( 1 3 1
3 ) 1 3 2

Output 1

3

Note 1

The words described by this example are ()))( and )((()). From the second word, we can take the fragment )((() or ((()), or ((). In the first case, a valid shuffle of the word ()))( and the fragment )((() is ( )((( )))( ).

Input 2

2 ( 1 1
4 ) 2 1 1 2

Output 2

4

Note 2

The words described by this example are () and ))()((. Note that although the fragments from the second to the third and from the fourth to the fifth letters result in the same substring )(, we count them twice. Even though the word () itself is a valid parenthesis expression, we do not count empty fragments of the second word.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.