結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー H3PO4H3PO4
提出日時 2022-01-07 22:02:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 692 ms / 2,000 ms
コード長 1,170 bytes
コンパイル時間 1,131 ms
コンパイル使用メモリ 87,132 KB
実行使用メモリ 89,448 KB
最終ジャッジ日時 2023-09-12 14:11:39
合計ジャッジ時間 12,811 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,596 KB
testcase_01 AC 615 ms
88,640 KB
testcase_02 AC 613 ms
88,500 KB
testcase_03 AC 617 ms
88,408 KB
testcase_04 AC 597 ms
89,116 KB
testcase_05 AC 637 ms
89,360 KB
testcase_06 AC 625 ms
89,112 KB
testcase_07 AC 630 ms
89,244 KB
testcase_08 AC 640 ms
89,312 KB
testcase_09 AC 634 ms
88,920 KB
testcase_10 AC 637 ms
88,984 KB
testcase_11 AC 618 ms
89,408 KB
testcase_12 AC 610 ms
89,448 KB
testcase_13 AC 599 ms
88,544 KB
testcase_14 AC 692 ms
88,064 KB
testcase_15 AC 74 ms
71,260 KB
testcase_16 AC 75 ms
71,380 KB
testcase_17 AC 76 ms
70,988 KB
testcase_18 AC 75 ms
71,340 KB
testcase_19 AC 73 ms
71,340 KB
testcase_20 AC 73 ms
71,492 KB
testcase_21 AC 71 ms
71,324 KB
testcase_22 AC 72 ms
71,260 KB
testcase_23 AC 71 ms
71,372 KB
testcase_24 AC 72 ms
71,384 KB
権限があれば一括ダウンロードができます

ソースコード

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 i < N - 1 and S[i + 1] == ")":
                B.add(i + 1, -1)
        else:
            S[i] = "("
            if i > 0 and S[i - 1] == "(":
                B.add(i, -1)
            if i < N - 1 and S[i + 1] == ")":
                B.add(i + 1, 1)
    else:
        _, l, r = query
        print(B.sum(l, r - 1))
0