結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー yudedakoyudedako
提出日時 2022-01-28 23:07:31
言語 Scala(Beta)
(3.4.0)
結果
TLE  
実行時間 -
コード長 1,726 bytes
コンパイル時間 12,930 ms
コンパイル使用メモリ 262,104 KB
実行使用メモリ 139,932 KB
最終ジャッジ日時 2023-08-28 21:45:06
合計ジャッジ時間 21,882 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 AC 1,949 ms
107,812 KB
testcase_04 TLE -
testcase_05 TLE -
testcase_06 AC 1,931 ms
87,364 KB
testcase_07 AC 1,171 ms
63,784 KB
testcase_08 TLE -
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 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()
class Bit(val size: Int):
  private val array = Array.fill(size){Int.MinValue}
  def get(position: Int): Int =
    var pos = position
    var result = Int.MinValue
    while pos>= 0 do
      result = max(result, array(pos))
      pos -= ~pos & (pos + 1)
    result
  def changeMax(position: Int, value: Int) =
    var pos = position
    while pos < size do
      array(pos) = max(array(pos), value)
      pos += ~pos & (pos + 1)

@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)
    x -> y
  }
  val stack = mutable.Stack[Int]()
  val pair = Array.fill(n){-1}
  for (c, i) <- s.zipWithIndex do
    c match
      case '(' => stack.push(i)
      case ')' =>
        val h = stack.pop()
        pair(i) = h
        pair(h) = i
  val bit = Bit(n)
  var last = n - 1
  val result = Array.fill(q){"-1"}
  for ((x, y), i) <- queries.map((x, y) => min(min(x, pair(x)), min(y, pair(y))) -> max(max(x, pair(x)), max(y, pair(y)))).zipWithIndex.sortBy(-_._1._2) do
    while 0 <= last && y <= last do
      if s(last) == ')' then
        bit.changeMax(pair(last), pair(last))
      last -= 1
    val left = bit.get(x)
    if left >= 0 then
      result(i) = s"${left + 1} ${pair(left) + 1}"
  println(result.mkString("\n"))



0