結果
| 問題 |
No.1802 Range Score Query for Bracket Sequence
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-01-27 20:09:41 |
| 言語 | Scala(Beta) (3.6.2) |
| 結果 |
AC
|
| 実行時間 | 1,993 ms / 2,000 ms |
| コード長 | 1,560 bytes |
| コンパイル時間 | 11,726 ms |
| コンパイル使用メモリ | 264,644 KB |
| 実行使用メモリ | 87,688 KB |
| 最終ジャッジ日時 | 2024-06-30 02:24:42 |
| 合計ジャッジ時間 | 46,217 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 24 |
ソースコード
import scala.io.StdIn.readLine
import scala.reflect.ClassTag
class BinaryIndexedTree[T: Numeric: ClassTag](val size: Int):
private val array = Array.fill(size){summon[Numeric[T]].zero}
def get(position: Int): T =
var result = summon[Numeric[T]].zero
var pos = position
while pos >= 0 do
result = summon[Numeric[T]].plus(result, array(pos))
pos -= ~pos & (pos + 1)
result
def add(position: Int, value: T) =
var pos = position
while pos < size do
array(pos) = summon[Numeric[T]].plus(array(pos), value)
pos += ~pos & (pos + 1)
@main def main =
val Array(n, q) = readLine().split(' ').map(_.toInt)
val s = readLine().toCharArray
val queries = Array.fill(q){
readLine().split(' ').map(_.toInt)
}
val bit = BinaryIndexedTree[Int](n)
for i <- 1 until n do
if s(i - 1) == '(' && s(i) == ')' then
bit.add(i, 1)
for query <- queries do
query match
case Array(1, _i) =>
val i = _i - 1
s(i) match
case '(' =>
s(i) = ')'
if i > 0 && s(i - 1) == '(' then
bit.add(i, 1)
if i + 1 < n && s(i + 1) == ')' then
bit.add(i + 1, -1)
case ')' =>
s(i) = '('
if i > 0 && s(i - 1) == '(' then
bit.add(i, -1)
if i + 1 < n && s(i + 1) == ')' then
bit.add(i + 1, 1)
case _ => ???
case Array(2, _l, _r) =>
val l = _l - 1
val r = _r - 1
val sum = bit.get(r) - bit.get(l)
println(sum)