結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-01-09 23:51:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 681 ms / 2,000 ms
コード長 1,538 bytes
コンパイル時間 690 ms
コンパイル使用メモリ 86,924 KB
実行使用メモリ 103,372 KB
最終ジャッジ日時 2023-09-12 14:16:18
合計ジャッジ時間 12,232 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,100 KB
testcase_01 AC 670 ms
102,904 KB
testcase_02 AC 633 ms
103,048 KB
testcase_03 AC 614 ms
103,268 KB
testcase_04 AC 611 ms
103,144 KB
testcase_05 AC 623 ms
103,092 KB
testcase_06 AC 622 ms
103,092 KB
testcase_07 AC 623 ms
102,976 KB
testcase_08 AC 626 ms
102,928 KB
testcase_09 AC 611 ms
102,916 KB
testcase_10 AC 605 ms
103,184 KB
testcase_11 AC 604 ms
103,372 KB
testcase_12 AC 615 ms
103,116 KB
testcase_13 AC 627 ms
103,140 KB
testcase_14 AC 681 ms
103,276 KB
testcase_15 AC 73 ms
71,420 KB
testcase_16 AC 72 ms
71,320 KB
testcase_17 AC 75 ms
71,224 KB
testcase_18 AC 74 ms
71,308 KB
testcase_19 AC 71 ms
71,288 KB
testcase_20 AC 72 ms
71,160 KB
testcase_21 AC 71 ms
71,336 KB
testcase_22 AC 72 ms
71,272 KB
testcase_23 AC 73 ms
71,284 KB
testcase_24 AC 74 ms
71,152 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

    def sum_range(self,l,r):
        return self.sum(r)-self.sum(l)

    # 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())
L = []
bit = BIT(n)
for i in range(n):
    if i != n-1 and S[i] == "(" and S[i+1] == ")":
        L.append(1)
    else:
        L.append(0)

bit.build(L)

for _ 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)
            
            if x != 0 and S[x-1] == "(":
                bit.add(x-1,1)

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

    
    else:
        _,l,r = L

        print(bit.sum_range(l-1,r-1))
0