結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー yudedakoyudedako
提出日時 2022-01-27 20:09:41
言語 Scala(Beta)
(3.4.0)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,560 bytes
コンパイル時間 17,199 ms
コンパイル使用メモリ 249,496 KB
実行使用メモリ 85,812 KB
最終ジャッジ日時 2023-09-12 14:20:46
合計ジャッジ時間 55,333 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 938 ms
62,656 KB
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 AC 925 ms
62,488 KB
testcase_16 AC 924 ms
62,132 KB
testcase_17 AC 933 ms
62,352 KB
testcase_18 AC 927 ms
62,504 KB
testcase_19 AC 935 ms
62,212 KB
testcase_20 AC 939 ms
62,396 KB
testcase_21 AC 954 ms
62,148 KB
testcase_22 AC 934 ms
62,236 KB
testcase_23 AC 944 ms
62,208 KB
testcase_24 AC 941 ms
62,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0