結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー tktk_snsntktk_snsn
提出日時 2022-01-07 22:47:53
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 533 ms / 2,000 ms
コード長 2,027 bytes
コンパイル時間 567 ms
コンパイル使用メモリ 87,136 KB
実行使用メモリ 96,072 KB
最終ジャッジ日時 2023-09-12 14:13:22
合計ジャッジ時間 10,872 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,224 KB
testcase_01 AC 533 ms
94,904 KB
testcase_02 AC 499 ms
94,588 KB
testcase_03 AC 505 ms
94,800 KB
testcase_04 AC 498 ms
94,736 KB
testcase_05 AC 495 ms
95,528 KB
testcase_06 AC 505 ms
94,548 KB
testcase_07 AC 510 ms
94,956 KB
testcase_08 AC 488 ms
94,508 KB
testcase_09 AC 510 ms
94,452 KB
testcase_10 AC 513 ms
94,776 KB
testcase_11 AC 469 ms
96,072 KB
testcase_12 AC 451 ms
94,608 KB
testcase_13 AC 475 ms
95,244 KB
testcase_14 AC 319 ms
90,980 KB
testcase_15 AC 73 ms
71,084 KB
testcase_16 AC 73 ms
71,380 KB
testcase_17 AC 73 ms
71,540 KB
testcase_18 AC 73 ms
71,280 KB
testcase_19 AC 73 ms
71,384 KB
testcase_20 AC 73 ms
71,404 KB
testcase_21 AC 72 ms
71,520 KB
testcase_22 AC 72 ms
71,264 KB
testcase_23 AC 72 ms
71,516 KB
testcase_24 AC 72 ms
71,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline


class FenwickTree(object):
    def __init__(self, n):
        self.n = n
        self.log = n.bit_length()
        self.data = [0] * n
        self.raw = [0] * n

    def __sum(self, r):
        s = 0
        while r > 0:
            s += self.data[r - 1]
            r -= r & -r
        return s

    def get(self, i):
        return self.raw[i]

    def add(self, p, x):
        """ a[p] += xを行う"""
        self.raw[p] += x
        p += 1
        while p <= self.n:
            self.data[p - 1] += x
            p += p & -p

    def update(self, p, x):
        self.add(p, x - self.get(p))

    def sum(self, l, r):
        """a[l] + a[l+1] + .. + a[r-1]を返す"""
        return self.__sum(r) - self.__sum(l)

    def lower_bound(self, x):
        """a[0] + a[1] + .. a[i] >= x となる最小のiを返す"""
        if x <= 0:
            return -1
        i = 0
        k = 1 << self.log
        while k:
            if i + k <= self.n and self.data[i + k - 1] < x:
                x -= self.data[i + k - 1]
                i += k
            k >>= 1
        return i

    def __repr__(self):
        res = [self.sum(i, i+1) for i in range(self.n)]
        return " ".join(map(str, res))


N, Q = map(int, input().split())
S = list(input().rstrip())

bit = FenwickTree(N + 1)
for i in range(N-1):
    if S[i] == ")" and S[i+1] == "(":
        bit.add(i, 1)


def swap(i):
    S[i] = (set("()") ^ set(S[i])).pop()
    if i + 1 < N:
        if S[i] == ")" and S[i+1] == "(":
            bit.update(i, 1)
        else:
            bit.update(i, 0)
    if i > 0:
        if S[i-1] == ")" and S[i] == "(":
            bit.update(i-1, 1)
        else:
            bit.update(i-1, 0)


for _ in range(Q):
    flag, *arg = map(int, input().split())
    if flag == 1:
        swap(arg[0]-1)
    else:
        l, r = arg
        l -= 1
        r -= 1
        V = bit.sum(l, r)
        if S[l] == "(":
            V += 1
        if S[r] == ")":
            V += 1
        print(V - 1)
0