The Parentheses Problem

0

0 votes
Problem

You have a string s of length n, consisting of characters "(" and ")". There are two types of query:

  •  1 l r(Type 1) Flip the parentheses (i.e. replace each parenthesis with its opposite) at all positions with indices from l to r, both inclusive: each character '(' is replaced with ')' and each character ')' is replaced with '(' (1lrn);
  • 2 l r(Type 2) Find the length of the maximum correct bracket subsequence of sequence sl,sl+1,....,sr.

A subsequence of length |x| of string s=s1s2...s|s| (where |s| is the length of string s) is string x=sk1sk2...sk|x| (1k1<k2<...<k|x||s|).

A correct bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the characters of the string. For example, bracket sequences "()()", "(())" are correct (the resulting expressions "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.

Input:

The first line contains two integers n and m — the length of the string s and the number of queries correspondingly. The next line contains a sequence of characters s1,s2,...,sn  without any spaces. Each character is either a "(" or a ")". Each of the next m lines contains a number that denotes the type of the query followed by a pair of integers. The ith line contains integers t,l,r — the description of the ith query.

Output:

For each query of type 2 print the required answer on a new line.

Constraints:

  • 1n106
  • 1m3×105
  • 1t2
  • 1lrn
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

2 8 8: This query is of type 2. There is no correct bracket subsequence in "(". Hence the answer is 0.

2 1 3: This query is also of type 2. The correct bracket subsequence in ")()" is "()". Hence the answer is 2.

1 7 8: This query is of type 1. Here we have to invert the parentheses having index between 7 and 8. Hence the new sequence is ")())(())(".

2 2 8: This query is of type 2. The maximum length of correct bracket subsequence in "())(())" is "()(())". Hence the answer is 6.

Editor Image

?