結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー yudedakoyudedako
提出日時 2022-01-28 22:34:53
言語 Scala(Beta)
(3.6.2)
結果
TLE  
実行時間 -
コード長 1,017 bytes
コンパイル時間 16,011 ms
コンパイル使用メモリ 259,376 KB
実行使用メモリ 219,916 KB
最終ジャッジ日時 2024-12-30 08:45:13
合計ジャッジ時間 94,072 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 4 TLE * 23
権限があれば一括ダウンロードができます

ソースコード

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)
  }
  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 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