結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー yudedako
提出日時 2022-01-28 22:48:56
言語 Scala(Beta)
(3.6.2)
結果
TLE  
実行時間 -
コード長 1,239 bytes
コンパイル時間 17,730 ms
コンパイル使用メモリ 265,344 KB
実行使用メモリ 231,484 KB
最終ジャッジ日時 2024-12-30 09:39:00
合計ジャッジ時間 88,571 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 4 TLE * 23
権限があれば一括ダウンロードができます

ソースコード

diff #

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, StdIn}

def readLine()(using reader: BufferedReader): String = reader.readLine()
@main def main =
  given reader: BufferedReader = scala.io.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)
  }
  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