結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー suosuo
提出日時 2022-01-07 22:11:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 682 ms / 2,000 ms
コード長 1,088 bytes
コンパイル時間 273 ms
コンパイル使用メモリ 87,016 KB
実行使用メモリ 90,060 KB
最終ジャッジ日時 2023-09-12 14:12:06
合計ジャッジ時間 11,356 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,156 KB
testcase_01 AC 594 ms
89,996 KB
testcase_02 AC 579 ms
88,352 KB
testcase_03 AC 588 ms
88,156 KB
testcase_04 AC 588 ms
88,324 KB
testcase_05 AC 597 ms
89,744 KB
testcase_06 AC 594 ms
89,992 KB
testcase_07 AC 598 ms
89,904 KB
testcase_08 AC 601 ms
90,060 KB
testcase_09 AC 604 ms
88,440 KB
testcase_10 AC 598 ms
88,432 KB
testcase_11 AC 603 ms
88,148 KB
testcase_12 AC 593 ms
88,368 KB
testcase_13 AC 581 ms
88,372 KB
testcase_14 AC 682 ms
89,948 KB
testcase_15 AC 76 ms
71,236 KB
testcase_16 AC 74 ms
71,356 KB
testcase_17 AC 75 ms
71,160 KB
testcase_18 AC 75 ms
71,184 KB
testcase_19 AC 73 ms
71,284 KB
testcase_20 AC 73 ms
71,276 KB
testcase_21 AC 73 ms
71,272 KB
testcase_22 AC 74 ms
71,284 KB
testcase_23 AC 73 ms
71,004 KB
testcase_24 AC 74 ms
71,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:
  def __init__(self,size):
    self.size = size
    self.tree = [0] * (size+1)
    
  def add(self,i,x):
    while i <= self.size:
      self.tree[i] += x
      i += i & -i
    return
  
  def ac(self,i):
    res = 0
    while i > 0:
      res += self.tree[i]
      i -= i & -i
    return res

n,q = map(int,input().split())
s = input()
l = []
for i in s:
    if i == '(':
        l.append(0)
    else:
        l.append(1)
bit = BIT(n)
for i in range(n-1):
    if l[i] == 0 and l[i+1] == 1:
        bit.add(i+1,1)
for i in range(q):
    t = list(map(int,input().split()))
    if t[0] == 1:
        ind = t[1]-1
        l[ind] = (l[ind]+1)%2
        if ind > 0:
            if l[ind] == 1 and l[ind-1] == 0:
                bit.add(ind,1)
            elif l[ind] == l[ind-1] == 0:
                bit.add(ind,-1)
        if ind < n-1:
            if l[ind] == 0 and l[ind+1] == 1:
                bit.add(ind+1,1)
            elif l[ind] == l[ind+1] == 1:
                bit.add(ind+1,-1)
    else:
        left,right = t[1],t[2]
        print(bit.ac(right-1) - bit.ac(left-1))
0