#P8765. [蓝桥杯 2021 国 AB] 翻转括号序列

[蓝桥杯 2021 国 AB] 翻转括号序列

Problem Description

Given a parentheses sequence of length nn, you need to support two types of operations:

  1. Flip all parentheses in the interval [Li,Ri]\left[L_{i}, R_{i}\right] (from the LiL_{i}-th character to the RiR_{i}-th character in the sequence). That is, left parentheses become right parentheses, and right parentheses become left parentheses.

  2. Given LiL_{i} as the left endpoint, find the RiR_{i} of the longest valid parentheses sequence (i.e., find the maximum RiR_{i} such that [Li,Ri]\left[L_{i}, R_{i}\right] is a valid parentheses sequence).

Input Format

The first line contains two integers n,mn, m, representing the length of the parentheses sequence and the number of operations.

The second line contains the given parentheses sequence, which only consists of left parentheses and right parentheses.

The next mm lines each describe an operation. If the line is 1 L R, it means the first type of operation with interval [L,R]\left[L, R\right]; if the line is 2 L, it means the second type of operation with left endpoint LL.

Output Format

For each operation of the second type, output one line with the corresponding RiR_{i}. If no such RiR_{i} exists, output 00.

7 5
((())()
2 3
2 2
1 3 5
2 3
2 1
4
7
0
0

Hint

For 20%20\% of the testdata, n,m5000n, m \leq 5000.

For 40%40\% of the testdata, n,m30000n, m \leq 30000.

For 60%60\% of the testdata, n,m100000n, m \leq 100000.

For all testdata, $1 \leq n \leq 10^{6}, 1 \leq m \leq 2 \times 10^{5}$.

Lanqiao Cup 2021 National Contest Group A Problem H (Group B Problem I).

Translated by ChatGPT 5