#d1806. 合法括号串

合法括号串

Valid Parentheses String

Problem Description

Given a parentheses string SS of length nn, there are mm queries. For each query, find the length of the longest subsequence within the interval [l,r][l, r] that is a valid parentheses string.

A parentheses string is a string consisting only of ( and ).

A valid parentheses string is defined as follows:

  1. The empty string is a valid parentheses string.
  2. If SS is a valid parentheses string, then (S)(S) is a valid parentheses string.
  3. If SS and TT are valid parentheses strings, then STST is a valid parentheses string.

Input Format

The first line contains two integers n,mn, m as described (1≤n,m≤1051 \le n, m \le 10^5).

The second line contains a string SS, guaranteed to contain only ( and ).

The following mm lines each contain two integers l,rl, r as described (1≤l≤r≤n1 \le l \le r \le n).

Output Format

Output mm lines in total; the ii-th line is the answer to the ii-th query.

Sample Input

10 5
))()()()))
6 10
7 8
7 7
7 8
4 9

Sample Output

2
2
0
2
4