Qarentheziz Sequence
Time limit2sMemory limit1024 MB
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 is balanced, then
(, followed by , followed by)in this order is balanced.
For example, ()() and (()()) are balanced. )(, )()((), and ( are not balanced.
Ryan defines the sadness of a string as the minimum number of operations needed to turn into a balanced string. The operations can be used in any order and any number of times:
- Add
)to the beginning of . - Add
(to the end of . - Swap two adjacent characters of .
Ryan has a string of length consisting only of ( and ). There are queries, and you must process them in order. There are two kinds of queries.
1 l r: For each character from the -th to the -th character of (inclusive), change(to)and)to(.2 l r: Output the sadness of the substring of from the -th to the -th character.
Input
The first line contains two integers and (, ), separated by a space. The second line contains the string , which consists only of ( and ) and has length . Each of the next lines contains three integers , , and (, ), separated by spaces. At least one query has .
Output
For each query with , print the sadness of the substring from the -th to the -th character on its own line.