#d1806. 合法括号串
合法括号串
Valid Parentheses String
Problem Description
Given a parentheses string of length , there are queries. For each query, find the length of the longest subsequence within the interval that is a valid parentheses string.
A parentheses string is a string consisting only of ( and ).
A valid parentheses string is defined as follows:
- The empty string is a valid parentheses string.
- If is a valid parentheses string, then is a valid parentheses string.
- If and are valid parentheses strings, then is a valid parentheses string.
Input Format
The first line contains two integers as described ().
The second line contains a string , guaranteed to contain only ( and ).
The following lines each contain two integers as described ().
Output Format
Output lines in total; the -th line is the answer to the -th query.
Sample Input
10 5
))()()()))
6 10
7 8
7 7
7 8
4 9
Sample Output
2
2
0
2
4