結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー kohei2019kohei2019
提出日時 2023-06-19 23:53:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 427 ms / 2,000 ms
コード長 3,155 bytes
コンパイル時間 455 ms
コンパイル使用メモリ 87,004 KB
実行使用メモリ 105,908 KB
最終ジャッジ日時 2023-09-09 18:44:23
合計ジャッジ時間 9,094 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,448 KB
testcase_01 AC 427 ms
96,788 KB
testcase_02 AC 411 ms
96,552 KB
testcase_03 AC 405 ms
96,556 KB
testcase_04 AC 407 ms
96,648 KB
testcase_05 AC 403 ms
96,960 KB
testcase_06 AC 403 ms
97,120 KB
testcase_07 AC 403 ms
96,872 KB
testcase_08 AC 406 ms
96,900 KB
testcase_09 AC 405 ms
96,824 KB
testcase_10 AC 408 ms
97,072 KB
testcase_11 AC 390 ms
96,680 KB
testcase_12 AC 394 ms
96,828 KB
testcase_13 AC 395 ms
96,660 KB
testcase_14 AC 373 ms
105,908 KB
testcase_15 AC 75 ms
71,160 KB
testcase_16 AC 70 ms
71,148 KB
testcase_17 AC 71 ms
71,064 KB
testcase_18 AC 70 ms
71,256 KB
testcase_19 AC 69 ms
71,036 KB
testcase_20 AC 71 ms
71,264 KB
testcase_21 AC 70 ms
71,416 KB
testcase_22 AC 70 ms
71,252 KB
testcase_23 AC 71 ms
71,104 KB
testcase_24 AC 70 ms
71,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

class SegTree:
    DEFAULT = {
        'min': 1 << 60,
        'max': -(1 << 60),
        'sum': 0,
        'prd': 1,
        'gcd': 0,
        'lmc': 1,
        '^': 0,
        '&': (1 << 60) - 1,
        '|': 0,
    }

    FUNC = {
        'min': min,
        'max': max,
        'sum': (lambda x, y: x + y),
        'prd': (lambda x, y: x * y),
        'gcd': math.gcd,
        'lmc': (lambda x, y: (x * y) // math.gcd(x, y)),
        '^': (lambda x, y: x ^ y),
        '&': (lambda x, y: x & y),
        '|': (lambda x, y: x | y),
    }

    def __init__(self, ls, mode='min', func=None, default=None):
        """
        要素ls, 関数mode (min,max,sum,prd(product),gcd,lmc,^,&,|)
        func,defaultを指定すれば任意の関数、単位元での計算が可能
        """
        N = len(ls)
        if default == None:
            self.default = self.DEFAULT[mode]
        else:
            self.default = default
        if func == None:
            self.func = self.FUNC[mode]
        else:
            self.func = func
        self.N = N
        self.K = (N - 1).bit_length()
        self.N2 = 1 << self.K
        self.dat = [self.default] * (2**(self.K + 1))
        for i in range(self.N):  # 葉の構築
            self.dat[self.N2 + i] = ls[i]
        self.build()

    def build(self):
        for j in range(self.N2 - 1, -1, -1):
            self.dat[j] = self.func(self.dat[j << 1], self.dat[j << 1 | 1])  # 親が持つ条件

    def leafvalue(self, x):  # リストのx番目の値
        return self.dat[x + self.N2]

    def update(self, x, y):  # index(x)をyに変更
        i = x + self.N2
        self.dat[i] = y
        while i > 0:  # 親の値を変更
            i >>= 1
            self.dat[i] = self.func(self.dat[i << 1], self.dat[i << 1 | 1])
        return

    def query(self, L, R):  # [L,R)の区間取得
        L += self.N2
        R += self.N2
        vL = self.default
        vR = self.default
        while L < R:
            if L & 1:
                vL = self.func(vL, self.dat[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.func(self.dat[R], vR)
            L >>= 1
            R >>= 1
        return self.func(vL, vR)

    def __iter__(self):
        for i in range(self.N):
            yield self[i]

    def __getitem__(self, x): return self.leafvalue(x)
    def __setitem__(self, x, val): return self.update(x, val)

N,Q = map(int,input().split())
S = ')'+input()+'('
lsS = []
for i in range(N+2):
    if S[i] == '(':
        lsS.append(1)
    else:
        lsS.append(0)

score = [0]*(N+1)
for i in range(N+1):
    if lsS[i] == 1 and lsS[i+1] == 0:
        score[i] = 1
ans = []
SG = SegTree(score,mode='sum')
for i in range(Q):
    ls = list(map(int, input().split()))
    if ls[0] == 1:
        x = ls[1]
        lsS[x] ^= 1
        if lsS[x] == 1 and lsS[x+1] == 0:
            SG[x] = 1
        else:
            SG[x] = 0
        if lsS[x-1] == 1 and lsS[x] == 0:
            SG[x-1] = 1
        else:
            SG[x-1] = 0

    else:
        l,r = ls[1],ls[2]
        ans.append(SG.query(l, r))
print(*ans,sep='\n')
0