Bracket String and Queries

Time limit0.5sMemory limit512 MB

Summary
Flip one character per query and count how many prefixes of the query sequence leave the string as a correct bracket sequence.
Level

Medium6 of 10

Topics
String, Prefix sum, Implementation, Math
Solved
No attempts yet

Problem

A bracket string is a string made of '(' and ')'. A correct bracket string is defined as follows.

  1. The empty string is a correct bracket string.
  2. If S is a correct bracket string, then (S) is also a correct bracket string.
  3. If S and T are correct bracket strings, then ST is also a correct bracket string.
  4. Every correct bracket string can be built using only the three rules above.

You are given a bracket string S = s1s2...sN made of '(' and ')', along with M queries. Each query consists of a single integer index, and it means the following.

  • If the index-th character of S is '(', change it to ')'; if it is ')', change it to '('.

Queries are applied cumulatively: the i-th query must be applied to the result of applying the (i-1)-th query. Count how many times the result of applying a query is a correct bracket string.

Input

The first line gives the string S. The second line gives the number of queries M. The following M lines give the query index, one per line.

Output

On the first line, print the number of times the result of applying a query was a correct bracket string.

Constraints

  • 1 ≤ |S| ≤ 100,000
  • 1 ≤ M ≤ 100,000
  • 1 ≤ index ≤ |S|

Hint

For S = "()()()()", the results of applying the queries of sample 1 are as follows.

  • ((()()()
  • ((()()))
  • ((((()))
  • (((())))
  • (()())))
  • (()())()
  • )()())()
  • )))())()

Examples1

  1. Example 1

    Input
    ()()()()
    8
    2
    7
    4
    5
    3
    7
    1
    2
    
    Expected output
    3