結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
コンテスト
ユーザー yudedako
提出日時 2022-01-28 22:53:36
言語 Scala(Beta)
(3.8.1)
コンパイル:
scalac _filename_
実行:
java -cp .:/home/linuxbrew/.linuxbrew/Cellar/scala/3.8.1/libexec/maven2/org/scala-lang/scala3-library_3/3.8.1/scala3-library_3-3.8.1.jar:/home/linuxbrew/.linuxbrew/Cellar/scala/3.8.1/libexec/maven2/org/scala-lang/scala-library/3.8.1/scala-library-3.8.1.jar _class_
結果
TLE  
実行時間 -
コード長 1,155 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 12,350 ms
コンパイル使用メモリ 289,868 KB
実行使用メモリ 172,464 KB
最終ジャッジ日時 2026-03-09 19:46:01
合計ジャッジ時間 69,085 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 13 TLE * 14
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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){immutable.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)



0