結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー 👑 tamatotamato
提出日時 2022-01-07 22:45:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 319 ms / 2,000 ms
コード長 1,950 bytes
コンパイル時間 1,005 ms
コンパイル使用メモリ 87,336 KB
実行使用メモリ 89,732 KB
最終ジャッジ日時 2023-09-12 14:12:55
合計ジャッジ時間 7,942 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,360 KB
testcase_01 AC 314 ms
89,532 KB
testcase_02 AC 305 ms
88,880 KB
testcase_03 AC 313 ms
89,328 KB
testcase_04 AC 304 ms
89,228 KB
testcase_05 AC 316 ms
89,732 KB
testcase_06 AC 317 ms
89,284 KB
testcase_07 AC 319 ms
89,384 KB
testcase_08 AC 309 ms
89,076 KB
testcase_09 AC 307 ms
89,440 KB
testcase_10 AC 307 ms
89,096 KB
testcase_11 AC 314 ms
89,508 KB
testcase_12 AC 305 ms
89,504 KB
testcase_13 AC 308 ms
89,072 KB
testcase_14 AC 266 ms
89,020 KB
testcase_15 AC 71 ms
71,472 KB
testcase_16 AC 70 ms
71,592 KB
testcase_17 AC 72 ms
71,588 KB
testcase_18 AC 71 ms
71,728 KB
testcase_19 AC 72 ms
71,584 KB
testcase_20 AC 71 ms
71,800 KB
testcase_21 AC 73 ms
71,704 KB
testcase_22 AC 71 ms
71,692 KB
testcase_23 AC 72 ms
71,620 KB
testcase_24 AC 71 ms
71,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 1000000007
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    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

        def lower_bound(self, w):
            if w <= 0:
                return 0
            x = 0
            k = 1 << (self.size.bit_length() - 1)
            while k:
                if x + k <= self.size and self.tree[x + k] < w:
                    w -= self.tree[x + k]
                    x += k
                k >>= 1
            return x + 1

    N, Q = map(int, input().split())
    bit = Bit(N)
    S = input().rstrip('\n')
    S = list(S)
    for i in range(N - 1):
        if S[i] == "(" and S[i+1] == ")":
            bit.add(i + 1, 1)

    for _ in range(Q):
        q = list(map(int, input().split()))
        if q[0] == 1:
            i = q[1]
            if S[i-1] == "(":
                S[i-1] = ")"
                if i != N:
                    if S[i] == ")":
                        bit.add(i, -1)
                if i != 1:
                    if S[i-2] == "(":
                        bit.add(i-1, 1)
            else:
                S[i-1] = "("
                if i != N:
                    if S[i] == ")":
                        bit.add(i, 1)
                if i != 1:
                    if S[i-2] == "(":
                        bit.add(i-1, -1)
        else:
            l, r = q[1:]
            if l == r:
                print(0)
                continue
            if l == 1:
                print(bit.sum(r - 1))
            else:
                print(bit.sum(r - 1) - bit.sum(l - 1))


if __name__ == '__main__':
    main()
0