結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー hir355hir355
提出日時 2022-01-07 22:14:49
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,007 bytes
コンパイル時間 340 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 92,544 KB
最終ジャッジ日時 2024-04-26 18:43:12
合計ジャッジ時間 10,787 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
51,712 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 41 ms
51,712 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 41 ms
52,352 KB
testcase_21 AC 41 ms
51,712 KB
testcase_22 AC 40 ms
51,840 KB
testcase_23 AC 41 ms
51,840 KB
testcase_24 AC 39 ms
51,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Bit:
    def __init__(self, n):
        self.size = n
        self.tree = [0] * (n + 1)
 
    def sum(self, i):
        s = 0
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
 
    def add(self, i, x):
        while i <= self.size:
            self.tree[i] += x
            i += i & -i

n, q = map(int, input().split())
s = [' '] + list(input()) + [' ']
fw = Bit(n + 2)
for i in range(n + 2):
    if s[i] == '(' and s[i + 1] == ')':
        fw.add(i + 1, 1)
for _ in range(q):
    t, *query = map(int, input().split())
    if t == 1:
        i, = query
        if s[i] == '(':
            if s[i + 1] == ')':
                fw.add(i + 1, -1)
            s[i] = ')'
            if s[i - 1] == '(':
                fw.add(i, 1)
        else:
            if s[i + 1] == '(':
                fw.add(i, -1)
            s[i] = '('
            if s[i + 1] == ')':
                fw.add(i + 1, 1)
    else:
        l, r = query
        print(fw.sum(r) - fw.sum(l))
0