#P9356. 「SiR-1」Bracket

「SiR-1」Bracket

Background

Everything that kills me makes me feel alive.

Problem Description

Mirika has a bracket sequence ss of length nn.

For a bracket sequence SS, Mirika can perform two operations:

  • Rotation: choose a position ii satisfying 1iS1 \leq i \leq \lvert S \rvert, and change SS into $S_iS_{i+1}\cdots S_{\lvert S\rvert}S_1S_2\cdots S_{i-2}S_{i-1}$.
  • Insertion: insert one bracket (either a left bracket or a right bracket) at any position in the sequence.

Mirika defines the weight f(S)f(S) of a bracket sequence SS as the minimum number of operations needed to turn this bracket sequence into a valid bracket sequence.

The definition of a valid bracket sequence is:

  • The empty string is a valid bracket sequence.
  • If A\texttt A is a valid bracket sequence, then (A)\texttt{(A)} is also a valid bracket sequence.
  • If both A\texttt A and B\texttt B are valid bracket sequences, then AB\texttt{AB} is also a valid bracket sequence.

Now Mirika wants to compute:

l=1nr=lnf(s[l,r])\sum_{l=1}^n \sum_{r=l}^n f(s[l,r])

where s[l,r]s[l,r] denotes the contiguous subsequence formed by sl,sl+1,,srs_l,s_{l+1},\cdots,s_r.

However, Mirika is too weak to compute it, so she has to ask you for help.

Input Format

There are multiple test cases in each test point.

The first line contains a positive integer TT, denoting the number of test cases.

For each test case, the first line contains a positive integer nn.

The second line contains a bracket sequence ss of length nn.

Output Format

Output a total of TT lines. The ii-th line contains one integer, denoting the answer for the ii-th test case.

5
2
((
4
())(
5
()(()
5
()()(
15
()())(())))()()
4
11
16
12
241

Hint

Sample Explanation

For s=())(s = \texttt{())(}:

  • Consider s[1,4]=())(s[1,4]=\texttt{())(}. Perform a rotation operation with i=4i=4, then ())((())\texttt{())(} \Rightarrow \texttt{(())}. Since (())\texttt{(())} is a valid bracket sequence, f(s[1,4])=1f(s[1, 4]) = 1. It can be proven that there is no better strategy.
  • Consider s[2,4]=))(s[2,4]=\texttt{))(}. Perform a rotation operation with i=2i=2, then insert a left bracket at the beginning of the sequence, then $\texttt{))(} \Rightarrow \texttt{)()} \Rightarrow \texttt{()()}$. Since ()()\texttt{()()} is a valid bracket sequence, f(s[2,4])=2f(s[2, 4]) = 2. It can be proven that there is no better strategy.

Constraints and Notes

This problem uses bundled test.

  • Subtask 0 (15 pts): n400n \leq 400, n800\sum n \leq 800.
  • Subtask 1 (20 pts): n2×103n \leq 2\times 10^3, n4×103\sum n \leq 4\times 10^3.
  • Subtask 2 (5 pts): ss contains no right brackets.
  • Subtask 3 (10 pts): for all integers 1i<n1\le i < n, sisi+1s_i \neq s_{i+1}.
  • Subtask 4 (30 pts): n2×105n \leq 2\times 10^5, n5×105\sum n \leq 5\times 10^5.
  • Subtask 5 (20 pts): no special constraints.

For all testdata, 1T100001 \leq T \leq 10000, 1n2×1061 \leq n \leq 2 \times 10^6, 1n2×1071 \leq \sum n \leq 2 \times 10^7.

Translated by ChatGPT 5