結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー tktk_snsntktk_snsn
提出日時 2022-01-07 22:36:06
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,075 bytes
コンパイル時間 251 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 115,044 KB
最終ジャッジ日時 2024-04-26 18:48:00
合計ジャッジ時間 7,474 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,480 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 37 ms
52,480 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 38 ms
52,352 KB
testcase_20 AC 39 ms
52,480 KB
testcase_21 AC 37 ms
52,608 KB
testcase_22 AC 38 ms
52,352 KB
testcase_23 WA -
testcase_24 AC 36 ms
52,480 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 = ["()".index(s) for s in input().rstrip()]
query = tuple(tuple(map(int, input().split())) for _ in range(Q))

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

for flag, *arg in query:
    if flag == 1:
        i = arg[0] - 1
        S[i] = 1 - S[i]
        if i > 0:
            if S[i-1] == 1 and S[i] == 0:
                bit.update(i, 1)
            else:
                bit.update(i, 0)
        if i + 1 < N:
            if S[i] == 1 and S[i+1] == 0:
                bit.update(i+1, 1)
            else:
                bit.update(i+1, 0)
    else:
        l, r = arg
        l -= 1
        r -= 1
        tmp = bit.sum(l, r + 1)
        if S[l] == 0:
            tmp += 1
        if S[r] == 1:
            tmp += 1
        print(tmp - 1)
0