#P3215. [HNOI2011] 括号修复 / [JSOI2011] 括号序列
[HNOI2011] 括号修复 / [JSOI2011] 括号序列
Description
A valid parenthesis sequence is defined as follows:
- The empty string is valid.
- If string
Sis valid, then(S)is also valid. - If strings
AandBare valid, thenABis also valid.
You are given a string of length consisting of ( and ), with positions numbered from to . The following four operations are supported on this string:
-
Replace a b c: change all parentheses in to . For example, given the original string))())())(, after performingReplace 2 7 (, it becomes)(((((()(. -
Swap a b: reverse the substring in . For example, given the original string))())())(, after performingSwap 3 5, it becomes))))(())(. -
Invert a b: flip(to)and)to(within . For example, given the original string))())())(, after performingInvert 4 8, it becomes))((()(((. -
Query a b: ask for the minimum number of positions in that must be changed to make the substring a valid parenthesis sequence. Changing a position means turning(into)or)into(. Note thatQuerydoes not modify the current sequence. For example, given the original string))())())(, the result ofQuery 3 6is , because you need to change position from)to(and position from(to).
Input Format
The first line contains two space-separated positive integers , denoting the length of the string and the number of operations, respectively.
The second line contains the initial string of length . The next lines each describe one operation to be performed in order. The operation name and its parameters are separated by single spaces.
Output Format
For each Query operation, output a single line with one integer representing the answer. The input is guaranteed to be solvable.
4 5
((((
Replace 1 2 )
Query 1 2
Swap 2 3
Invert 3 4
Query 1 4
1
2
Hint
Sample Explanation:
There are Query operations in the input, so the output has lines.
When executing the first Query, the parenthesis sequence is ))((. By changing position , the substring over becomes a valid parenthesis sequence, so the first output line is 1.
When executing the second Query, the parenthesis sequence is )((). You need to change position and position to make the substring over a valid parenthesis sequence, so the second output line is 2.
Constraints:
For of the points, .
For of the points, .
Translated by ChatGPT 5
京公网安备 11011102002149号