33OJ
登录 注册

#P17894. [ICPC 2026 Nanchang I] The Meaning of the World

    ID: 20630 远端评测题 2000ms 1024MiB 尝试: 0 已通过: 0 显示难度省选/NOI− 上传者: 标签>江西ICPC2026省赛/邀请赛

[ICPC 2026 Nanchang I] The Meaning of the World

Problem Description

:::epigraph[Tractatus Logico-Philosophicus]

The meaning of the world must lie outside the world

All things in the world are as they are and happen as they happen

There is no value in the world :::

Fengmi gives you a string ss of length nn, consisting only of “(” and “)”.

You need to support qq operations:

  1. Flip the kk-th bracket, i.e., change “(” to “)” and “)” to “(”.
  2. Query the length of the longest valid parentheses substring in the interval [l,r][l,r].

Formally, you need to compute:

$$f(l,r) = \max (\left\{ j - i + 1 \mid l \le i \le j \le r \land \text{valid}(i,j) \right\}\cup \{0\})$$

where valid(i,j)\text{valid}(i,j) indicates that the substring s[i..j]s[i..j] is a valid parentheses sequence.

In this problem, a valid parentheses sequence is defined as follows:

  1. The empty sequence is a valid sequence.
  2. If AA is a valid sequence, then (AA) is also a valid sequence.
  3. If AA and BB are both valid sequences, then ABAB is also a valid sequence.

Input Format

The first line contains two integers nn and qq (1≤n,q≤2×1051 \le n, q \le 2 \times 10^5) --- the length of the bracket string and the number of operations.

The second line contains a string ss of length nn, consisting only of the characters “(” and “)”. The given string is not guaranteed to be a valid parentheses sequence.

The next qq lines each describe one of the following two operations:

  • 1 k (1≤k≤n1 \le k \le n): flip the kk-th bracket.
  • 2 l r (1≤l≤r≤n1 \le l \le r \le n): query the length of the longest valid parentheses substring in the interval [l,r][l,r].

Output Format

For each operation of type 22, output a single integer on its own line --- the answer.

8 5
()(()())
2 1 8
1 4
2 1 8
1 7
2 1 8
8
4
4
12 6
(()())(()())
2 1 12
1 3
2 1 12
1 8
2 1 12
2 4 9
12
10
12
2

Hint

Sample 1:

  • Initial string: ()(()()).
  • First query on interval [1,8][1,8]: the longest valid parentheses substring is ()(()()) (positions 1−81-8), length 88.
  • Flip the 44th bracket: ()())()).
  • Second query on interval [1,8][1,8]: the longest valid parentheses substring is ()() (positions 1−41-4), length 44.
  • Flip the 77th bracket: ()())(().
  • Third query on interval [1,8][1,8]: the longest valid parentheses substring is ()() (positions 1−41-4), length 44.

Sample 2:

  • Initial string: (()())(()()).
  • First query on interval [1,12][1,12]: the longest valid parentheses substring is the entire string, length 1212.
  • Flip the 33rd bracket: (((())(()()).
  • Second query on interval [1,12][1,12]: the longest valid parentheses substring is (())(()()) (positions 3−123-12), length 1010.
  • Flip the 88th bracket: (((())())()).
  • Third query on interval [1,12][1,12]: the longest valid parentheses substring is (((())())()) (positions 1−121-12), length 1212.
  • Fourth query on interval [4,9][4,9]: the longest valid parentheses substring is () (positions 4−54-5), length 22.