結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー mkawa2mkawa2
提出日時 2023-08-10 21:37:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 459 ms / 2,000 ms
コード長 2,147 bytes
コンパイル時間 338 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 87,992 KB
最終ジャッジ日時 2024-04-28 14:41:47
合計ジャッジ時間 9,565 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
52,736 KB
testcase_01 AC 442 ms
87,380 KB
testcase_02 AC 443 ms
87,224 KB
testcase_03 AC 448 ms
86,976 KB
testcase_04 AC 441 ms
87,552 KB
testcase_05 AC 457 ms
87,212 KB
testcase_06 AC 458 ms
87,328 KB
testcase_07 AC 450 ms
87,356 KB
testcase_08 AC 456 ms
87,240 KB
testcase_09 AC 444 ms
87,992 KB
testcase_10 AC 435 ms
87,528 KB
testcase_11 AC 459 ms
87,636 KB
testcase_12 AC 440 ms
87,976 KB
testcase_13 AC 435 ms
87,352 KB
testcase_14 AC 232 ms
83,456 KB
testcase_15 AC 38 ms
52,480 KB
testcase_16 AC 38 ms
52,480 KB
testcase_17 AC 38 ms
52,736 KB
testcase_18 AC 37 ms
52,608 KB
testcase_19 AC 38 ms
52,864 KB
testcase_20 AC 37 ms
52,736 KB
testcase_21 AC 35 ms
52,992 KB
testcase_22 AC 34 ms
52,480 KB
testcase_23 AC 35 ms
52,736 KB
testcase_24 AC 35 ms
52,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(1000005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
inf = -1-(-1 << 63)
md = 10**9+7
# md = 998244353

class BitSum:
    def __init__(self, n):
        self._n = n+1
        self._table = [0]*self._n
        self._origin = [0]*n

    def __getitem__(self, item):
        return self._origin[item]

    def add(self, i, x):
        self._origin[i] += x
        i += 1
        while i < self._n:
            self._table[i] += x
            i += i & -i

    def sum(self, i):
        i += 1
        res = 0
        while i > 0:
            res += self._table[i]
            i -= i & -i
        return res

    def sumlr(self, l, r):
        if l >= r: return 0
        if l == 0: return self.sum(r-1)
        return self.sum(r-1)-self.sum(l-1)

    # return "min(i) of sum(i)>=x" or "N"
    def bisect(self, x):
        idx = 0
        d = 1 << (self._n-1).bit_length()-1
        while d:
            if idx+d < self._n and self._table[idx+d] < x:
                x -= self._table[idx+d]
                idx |= d
            d >>= 1
        return idx

n,q=LI()
s=[c==")" for c in SI()]

bit=BitSum(n)
for i in range(n-1):
    if s[i]==0 and s[i+1]:bit.add(i,1)

for _ in range(q):
    t,*lr=LI1()
    if t==0:
        i=lr[0]
        if i and s[i-1]==0 and s[i]:bit.add(i-1,-1)
        if i+1<n and s[i]==0 and s[i+1]:bit.add(i,-1)
        if i and s[i-1]==0 and s[i]==0:bit.add(i-1,1)
        if i+1<n and s[i] and s[i+1]:bit.add(i,1)
        s[i]^=1
    else:
        l,r=lr
        print(bit.sumlr(l,r))
0