結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー yudedakoyudedako
提出日時 2022-01-28 22:36:27
言語 Scala(Beta)
(3.4.0)
結果
TLE  
実行時間 -
コード長 1,024 bytes
コンパイル時間 12,840 ms
コンパイル使用メモリ 256,472 KB
実行使用メモリ 260,856 KB
最終ジャッジ日時 2023-08-28 20:56:42
合計ジャッジ時間 63,498 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 AC 1,956 ms
99,268 KB
testcase_04 TLE -
testcase_05 AC 1,956 ms
94,820 KB
testcase_06 AC 1,758 ms
83,676 KB
testcase_07 AC 1,187 ms
67,176 KB
testcase_08 TLE -
testcase_09 AC 1,428 ms
68,560 KB
testcase_10 AC 1,877 ms
91,800 KB
testcase_11 AC 1,893 ms
85,476 KB
testcase_12 TLE -
testcase_13 TLE -
testcase_14 AC 1,840 ms
86,724 KB
testcase_15 AC 935 ms
63,048 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 AC 958 ms
63,116 KB
testcase_22 AC 1,021 ms
63,372 KB
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.annotation.tailrec
import scala.collection.{immutable, mutable}
import scala.io.StdIn.readLine
import scala.math.*
import scala.annotation.tailrec

@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)
    min(x, y) -> max(x, y)
  }
  val stack = mutable.ArrayDeque[Int]()
  val pair = Array.fill(n){-1}
  for (c, i) <- s.zipWithIndex do
    c match
      case '(' => stack.addOne(i)
      case ')' =>
        val h = stack.removeLast()
        pair(i) = h
        pair(h) = i
  val result = Array.fill(q){"-1"}
  val set = mutable.TreeSet[Int]()
  var last = 0
  for ((x, y), i) <- queries.zipWithIndex.sortBy(_._1) do
    while last <= x do
      if s(last) == '(' then
        set.addOne(pair(last))
      last += 1
    set.minAfter(y) match
      case Some(right) => result(i) = s"${pair(right) + 1} ${right + 1}"
      case None =>
  println(
    result.mkString("\n")
  )


0