問題一覧 > 通常問題

No.1778 Bracketed Sequence Query

レベル : / AI推定難易度 : 1500 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 61
作問者 : null / テスター : CuriousFairy315
お気に入りにしたユーザー ProblemId : 4516 / 自分の提出
問題文最終更新日: 2022-04-25 23:42:24
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
Advent Calendar Contest 2021 (順位表) の他の問題:

Problem Statement

A well-formed bracket sequence is a string from which the empty string can be obtained by repeating, $0$ or more times, the operation of choosing a contiguous substring () and deleting it. Also, the two characters deleted in a single such operation are said to match each other.

Let $S_a$ be the $a$-th character of a bracket sequence $S$, and let $\overline{S_a}$ be the character that matches $S_a$. When $\overline{S_a} = S_b$, we define $\overline{a} = b$.

For example, $(())()$ is well-formed, and for instance $\overline{S_1} = S_4$.

You are given a well-formed bracket sequence $S$ of length $N$.

In the $i(1 \le i \le Q)$-th query, output the index of the innermost pair of matching characters that contains $S_{x_i}, \overline{S_{x_i}}, S_{y_i}, \overline{S_{y_i}}$ inside it. More formally, find the maximum $ans_i$ such that $ans_i \le x_i, \overline{x_i}, y_i, \overline{y_i} \le \overline{ans_i}$. Note that equality is allowed. If no such pair exists, output $-1$.

Constraints

  • $1 \le N, Q \le 2 \times 10^5$
  • $|S| = N$ (where $|S|$ is the length of $S$)
  • $S$ is a well-formed bracket sequence
  • $1 \le x_i, y_i \le N$
  • $N, Q, x_i, y_i$ are all integers

Input

$N\ Q$
$S$
$x_1\ y_1$
$x_2\ y_2$
$\dots$
$x_Q\ y_Q$

Output

For the $i$-th query, output $ans_i$ and $\overline {ans_i}$ in ascending order, separated by a space, on $1$ line. If no such pair exists, output $-1$. Output $Q$ lines in total. Print a newline at the end.

Sample

Sample 1
Input
10 7
(()(()))()
2 4
2 3
2 5
5 7
1 7
1 8
1 10
Output
1 8
2 3
1 8
4 7
1 8
1 8
-1

For the $1$-st query, the answer is shown in red and $S_{x_1}, \overline{S_{x_1}}, S_{y_1}, \overline{S_{y_1}}$ are shown in blue below.
$\color{red}{(} \color{blue}{(} \color{blue}{)} \color{blue}{(} \color{black}{()} \color{blue}{)} \color{red}{)} \color{black}{()}$

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。