This page is still under construction.

Parts of this page are still being built. What you see may change.

Qarentheziz Sequence

Time limit2sMemory limit1024 MB

Summary
Given a parentheses string with range flips, find for each query the fewest end insertions and adjacent swaps needed to make a substring balanced.
Level

Hard8 of 10

Topics
Segment tree, String, Greedy
Solved
No attempts yet

Problem

Ryan is interested in strings made only of ( and ). He especially loves balanced strings. Any balanced string can be built with these rules:

  • () is balanced.
  • The concatenation of two balanced strings is balanced.
  • If TT is balanced, then (, followed by TT, followed by ) in this order is balanced.

For example, ()() and (()()) are balanced. )(, )()((), and ( are not balanced.

Ryan defines the sadness of a string TT as the minimum number of operations needed to turn TT into a balanced string. The operations can be used in any order and any number of times:

  • Add ) to the beginning of TT.
  • Add ( to the end of TT.
  • Swap two adjacent characters of TT.

Ryan has a string SS of length NN consisting only of ( and ). There are QQ queries, and you must process them in order. There are two kinds of queries.

  • 1 l r: For each character from the ll-th to the rr-th character of SS (inclusive), change ( to ) and ) to (.
  • 2 l r: Output the sadness of the substring of SS from the ll-th to the rr-th character.

Input

The first line contains two integers NN and QQ (2≤N≤150 0002 \le N \le 150\,000, 1≤Q≤150 0001 \le Q \le 150\,000), separated by a space. The second line contains the string SS, which consists only of ( and ) and has length NN. Each of the next QQ lines contains three integers tit_i, lil_i, and rir_i (1≤ti≤21 \le t_i \le 2, 1≤li≤ri≤N1 \le l_i \le r_i \le N), separated by spaces. At least one query has ti=2t_i = 2.

Output

For each query with ti=2t_i = 2, print the sadness of the substring from the lil_i-th to the rir_i-th character on its own line.

Examples2

  1. Example 1

    Input
    6 6
    ())()(
    2 1 6
    1 2 4
    2 1 4
    2 2 5
    1 1 5
    2 1 6
    
    Expected output
    2
    5
    0
    6
    
  2. Example 2

    Input
    7 5
    (((((()
    2 1 7
    1 1 7
    2 1 7
    2 3 3
    2 2 6
    
    Expected output
    20
    26
    2
    20