結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-01-09 23:20:02
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,413 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 88,264 KB
最終ジャッジ日時 2024-04-26 19:56:39
合計ジャッジ時間 10,315 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,676 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 38 ms
52,732 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 37 ms
52,708 KB
testcase_21 AC 37 ms
52,336 KB
testcase_22 AC 38 ms
53,620 KB
testcase_23 AC 38 ms
52,400 KB
testcase_24 AC 38 ms
53,012 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