#P3215. [HNOI2011] 括号修复 / [JSOI2011] 括号序列

    ID: 2264 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>字符串2011各省省选平衡树湖南

[HNOI2011] 括号修复 / [JSOI2011] 括号序列

Description

A valid parenthesis sequence is defined as follows:

  1. The empty string is valid.
  2. If string S is valid, then (S) is also valid.
  3. If strings A and B are valid, then AB is also valid.

You are given a string of length nn consisting of ( and ), with positions numbered from 11 to nn. The following four operations are supported on this string:

  • Replace a b c: change all parentheses in [a,b][a, b] to cc. For example, given the original string ))())())(, after performing Replace 2 7 (, it becomes )(((((()(.

  • Swap a b: reverse the substring in [a,b][a, b]. For example, given the original string ))())())(, after performing Swap 3 5, it becomes ))))(())(.

  • Invert a b: flip ( to ) and ) to ( within [a,b][a, b]. For example, given the original string ))())())(, after performing Invert 4 8, it becomes ))((()(((.

  • Query a b: ask for the minimum number of positions in [a,b][a, b] that must be changed to make the substring a valid parenthesis sequence. Changing a position means turning ( into ) or ) into (. Note that Query does not modify the current sequence. For example, given the original string ))())())(, the result of Query 3 6 is 22, because you need to change position 55 from ) to ( and position 66 from ( to ).

Input Format

The first line contains two space-separated positive integers n,qn, q, denoting the length of the string and the number of operations, respectively.
The second line contains the initial string SS of length nn. The next qq 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 22 Query operations in the input, so the output has 22 lines.
When executing the first Query, the parenthesis sequence is ))((. By changing position 11, the substring over [1,2][1, 2] 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 11 and position 22 to make the substring over [1,4][1, 4] a valid parenthesis sequence, so the second output line is 2.

Constraints:

For 30%30\% of the points, 1n,q30001 \le n, q \le 3000.
For 100%100\% of the points, 1n,q1051 \le n, q \le 10^5.

Translated by ChatGPT 5