結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー yudedakoyudedako
提出日時 2022-01-28 17:27:53
言語 Scala(Beta)
(3.4.0)
結果
TLE  
実行時間 -
コード長 868 bytes
コンパイル時間 13,259 ms
コンパイル使用メモリ 258,872 KB
実行使用メモリ 138,196 KB
最終ジャッジ日時 2023-08-28 14:48:40
合計ジャッジ時間 36,078 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.collection.immutable
import scala.io.StdIn.readLine


@main def main =
  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)
    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){immutable.TreeSet[Int]()}
  for i <- 0 until n do
    if i > 0 then
      set(i) = set(i - 1)
    if s(i) == '(' then
      set(i) += pair(i)
    else
      set(i) = set(i - 1)
  for (x, y) <- queries do
    val result = set(x).rangeFrom(y).headOption.map(right => s"${pair(right) + 1} ${right + 1}").getOrElse("-1")
    println(result)

0