結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー kona0001kona0001
提出日時 2023-03-14 04:40:39
言語 Ruby
(3.3.0)
結果
AC  
実行時間 1,130 ms / 2,000 ms
コード長 1,003 bytes
コンパイル時間 63 ms
コンパイル使用メモリ 12,004 KB
実行使用メモリ 32,644 KB
最終ジャッジ日時 2023-10-18 11:28:56
合計ジャッジ時間 20,807 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
16,096 KB
testcase_01 AC 1,088 ms
32,636 KB
testcase_02 AC 1,094 ms
32,636 KB
testcase_03 AC 1,122 ms
32,636 KB
testcase_04 AC 1,116 ms
32,636 KB
testcase_05 AC 1,117 ms
32,636 KB
testcase_06 AC 1,130 ms
32,636 KB
testcase_07 AC 1,126 ms
32,636 KB
testcase_08 AC 1,121 ms
32,636 KB
testcase_09 AC 1,119 ms
32,636 KB
testcase_10 AC 1,120 ms
32,636 KB
testcase_11 AC 1,054 ms
32,636 KB
testcase_12 AC 1,042 ms
32,644 KB
testcase_13 AC 1,060 ms
32,636 KB
testcase_14 AC 820 ms
32,560 KB
testcase_15 AC 83 ms
16,096 KB
testcase_16 AC 84 ms
16,096 KB
testcase_17 AC 84 ms
16,096 KB
testcase_18 AC 84 ms
16,096 KB
testcase_19 AC 83 ms
16,096 KB
testcase_20 AC 86 ms
16,092 KB
testcase_21 AC 85 ms
16,092 KB
testcase_22 AC 84 ms
16,096 KB
testcase_23 AC 83 ms
16,096 KB
testcase_24 AC 86 ms
16,096 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Syntax OK

ソースコード

diff #

class BIT
  attr_accessor(:arr, :size)
  def initialize(n)
    @size = n
    @arr = Array.new(@size+1,0)
  end
  def add(i,x)
    i += 1
    while i <= @size
      @arr[i] += x
      i += i & -i
    end
  end
  def sum(i)
    i += 1
    sum = 0
    while i > 0
      sum += @arr[i]
      i &= i - 1
    end
    sum
  end
end

n,q = gets.split.map(&:to_i)
s = gets.chomp.chars
bit = BIT.new(n)
n.times do |i|
  next if i==0
  if s[i] == ")" && s[i-1] == "("
    bit.add(i,1)
  end
end
ans = []
q.times do
  query = gets.split.map(&:to_i)
  if query[0]==1
    i = query[1]-1
    if s[i] == "("
      if 0 <= i-1 && s[i-1] == "("
        bit.add(i,1)
      end
      if i+1 < n && s[i+1] == ")"
        bit.add(i+1,-1)
      end
      s[i] = ")"
    else
      if 0 <= i-1 && s[i-1] == "("
        bit.add(i,-1)
      end
      if i+1 < n && s[i+1] == ")"
        bit.add(i+1,1)
      end
      s[i] = "("
    end
  else
    l,r = query[1]-1,query[2]-1
    ans << bit.sum(r) - bit.sum(l)
  end
end
puts ans
0