結果
問題 |
No.1778 括弧列クエリ / Bracketed Sequence Query
|
ユーザー |
|
提出日時 | 2022-01-28 22:52:18 |
言語 | Scala(Beta) (3.6.2) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,153 bytes |
コンパイル時間 | 12,977 ms |
コンパイル使用メモリ | 269,864 KB |
実行使用メモリ | 340,648 KB |
最終ジャッジ日時 | 2024-12-30 09:46:17 |
合計ジャッジ時間 | 103,841 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 2 TLE * 25 |
ソースコード
import java.io.BufferedReader import scala.annotation.tailrec import scala.collection.{immutable, mutable} import scala.io.StdIn.readLine import scala.math.* import scala.annotation.tailrec import scala.io.{BufferedSource, Source, StdIn} def readLine()(using reader: BufferedReader): String = reader.readLine() @main def main = given reader: BufferedReader = Source.stdin.bufferedReader() val Array(n, q) = readLine().split(' ').map(_.toInt) val s = readLine() val queries = Array.fill(q){ val Array(x, y) = readLine().split(' ').map(_.toInt - 1) min(x, y) -> max(x, y) } var stack = Nil: List[Int] val pair = Array.fill(n){-1} for (c, i) <- s.zipWithIndex do c match case '(' => stack ::= i case ')' => val h::t = stack pair(i) = h pair(h) = i stack = t val set = Array.fill(n){mutable.TreeSet[Int]()} for i <- 0 until n do if i > 0 then set(i) = set(i - 1) if s(i) == '(' then set(i) = set(i) + pair(i) for (x, y) <- queries do val result = set(x).minAfter(y).map(right => s"${pair(right) + 1} ${right + 1}").getOrElse("-1") println(result)