結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-01-09 23:52:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 691 ms / 2,000 ms
コード長 1,411 bytes
コンパイル時間 851 ms
コンパイル使用メモリ 86,748 KB
実行使用メモリ 89,204 KB
最終ジャッジ日時 2023-09-12 14:17:08
合計ジャッジ時間 11,759 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,148 KB
testcase_01 AC 606 ms
89,012 KB
testcase_02 AC 611 ms
89,148 KB
testcase_03 AC 613 ms
89,132 KB
testcase_04 AC 616 ms
88,120 KB
testcase_05 AC 618 ms
88,432 KB
testcase_06 AC 613 ms
88,944 KB
testcase_07 AC 602 ms
88,508 KB
testcase_08 AC 621 ms
88,916 KB
testcase_09 AC 604 ms
88,548 KB
testcase_10 AC 608 ms
89,140 KB
testcase_11 AC 599 ms
88,724 KB
testcase_12 AC 592 ms
89,204 KB
testcase_13 AC 601 ms
89,204 KB
testcase_14 AC 691 ms
88,248 KB
testcase_15 AC 73 ms
71,364 KB
testcase_16 AC 72 ms
71,216 KB
testcase_17 AC 72 ms
71,368 KB
testcase_18 AC 73 ms
71,448 KB
testcase_19 AC 73 ms
71,228 KB
testcase_20 AC 72 ms
71,272 KB
testcase_21 AC 76 ms
71,236 KB
testcase_22 AC 74 ms
71,220 KB
testcase_23 AC 75 ms
71,392 KB
testcase_24 AC 74 ms
71,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT:
    def __init__(self, n):
        self.size = n
        self.tree = [0]*(n+1)
 
    def build(self, list):
        self.tree[1:] = list.copy()
        for i in range(self.size+1):
            j = i + (i & (-i))
            if j < self.size+1:
                self.tree[j] += self.tree[i]

    def sum(self, i):
        # [0, i) の要素の総和を返す
        s = 0
        while i>0:
            s += self.tree[i]
            i -= i & -i
        return s
    # 0 index を 1 index に変更  転倒数を求めるなら1を足していく
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i



n,q = map(int,input().split())
S = list(input())
bit = BIT(n+5)
for i in range(n-1):
    if S[i] == "(" and S[i+1] == ")":
        bit.add(i+1,1)

for i in range(q):
    l = list(map(int,input().split()))
    if l[0] == 1:
        x = l[1]-1
        if S[x] == "(":
            S[x] = ")"
            if x != n-1 and S[x+1] == ")":
                bit.add(x+1,-1)
            
            if x != 0 and S[x-1] == "(":
                bit.add(x,1)

        elif S[x] == ")":
            S[x] = "("
            if x != n-1 and S[x+1] == ")":
                bit.add(x+1,1)
            
            if x != 0 and S[x-1] == "(":
                bit.add(x,-1)

    
    else:
        _,l,r = l

        print(bit.sum(r)-bit.sum(l))

0