結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー H3PO4H3PO4
提出日時 2022-01-07 22:01:12
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,142 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 82,404 KB
実行使用メモリ 87,992 KB
最終ジャッジ日時 2024-04-26 18:41:12
合計ジャッジ時間 9,478 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
51,712 KB
testcase_01 RE -
testcase_02 RE -
testcase_03 AC 567 ms
87,628 KB
testcase_04 AC 543 ms
87,260 KB
testcase_05 AC 560 ms
86,748 KB
testcase_06 AC 552 ms
86,864 KB
testcase_07 AC 563 ms
86,748 KB
testcase_08 AC 542 ms
86,880 KB
testcase_09 AC 568 ms
86,996 KB
testcase_10 RE -
testcase_11 AC 556 ms
86,884 KB
testcase_12 AC 549 ms
87,464 KB
testcase_13 AC 557 ms
87,236 KB
testcase_14 AC 618 ms
86,272 KB
testcase_15 AC 38 ms
51,968 KB
testcase_16 AC 38 ms
52,096 KB
testcase_17 AC 40 ms
51,968 KB
testcase_18 AC 38 ms
51,968 KB
testcase_19 RE -
testcase_20 AC 38 ms
51,840 KB
testcase_21 AC 41 ms
51,968 KB
testcase_22 AC 38 ms
51,712 KB
testcase_23 RE -
testcase_24 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

class Bit:
    """1-indexed"""

    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 sum(self, i, j):
        """閉区間[i, j]"""
        return self._sum(j) - self._sum(i - 1)

    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())
B = Bit(N - 1)
for i in range(N - 1):
    if S[i] == "(" and S[i + 1] == ")":
        B.add(i + 1, 1)
for _ in range(Q):
    query = tuple(map(int, input().split()))
    if query[0] == 1:
        i = query[1] - 1
        if S[i] == "(":
            S[i] = ")"
            if i > 0 and S[i - 1] == "(":
                B.add(i, 1)
            if S[i + 1] == ")":
                B.add(i + 1, -1)
        else:
            S[i] = "("
            if i > 0 and S[i - 1] == "(":
                B.add(i, -1)
            if S[i + 1] == ")":
                B.add(i + 1, 1)
    else:
        _, l, r = query
        print(B.sum(l, r - 1))
0