結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-01-09 23:37:37
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,541 bytes
コンパイル時間 261 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 102,528 KB
最終ジャッジ日時 2024-04-26 19:57:17
合計ジャッジ時間 10,295 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,224 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 39 ms
51,968 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 39 ms
52,096 KB
testcase_21 AC 39 ms
51,712 KB
testcase_22 AC 40 ms
51,840 KB
testcase_23 AC 39 ms
51,968 KB
testcase_24 AC 39 ms
52,224 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 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)
            
            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