結果
| 問題 | No.1778 括弧列クエリ / Bracketed Sequence Query |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-01-28 23:07:31 |
| 言語 | Scala(Beta) (3.6.2) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,726 bytes |
| 記録 | |
| コンパイル時間 | 15,610 ms |
| コンパイル使用メモリ | 264,880 KB |
| 実行使用メモリ | 240,104 KB |
| 最終ジャッジ日時 | 2024-12-30 11:05:32 |
| 合計ジャッジ時間 | 89,172 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 4 TLE * 23 |
ソースコード
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"))