結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2022-01-07 23:29:47
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,879 bytes
コンパイル時間 224 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 103,444 KB
最終ジャッジ日時 2024-04-26 19:05:35
合計ジャッジ時間 6,599 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
51,840 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 41 ms
51,968 KB
testcase_17 AC 41 ms
52,224 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 41 ms
51,968 KB
testcase_21 AC 39 ms
52,096 KB
testcase_22 AC 41 ms
52,096 KB
testcase_23 WA -
testcase_24 AC 40 ms
51,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from sys import stdin

#0-indexed , 半開区間[a,b)
#calc変更で演算変更
class SegTree:

    def __init__(self,N,first):
        self.NO = 2**(N-1).bit_length()
        self.First = first
        self.data = [first] * (2*self.NO)

    def calc(self,l,r):
        return l+r

    def update(self,ind,x):
        ind += self.NO - 1
        self.data[ind] = x
        while ind >= 0:
            ind = (ind - 1)//2
            self.data[ind] = self.calc(self.data[2*ind+1],self.data[2*ind+2])

    def query(self,l,r):
        L = l + self.NO
        R = r + self.NO
        s = self.First
        while L < R:
            if R & 1:
                R -= 1
                s = self.calc(s , self.data[R-1])
            if L & 1:
                s = self.calc(s , self.data[L-1])
                L += 1
            L >>= 1
            R >>= 1
        return s

    def get(self , ind):
        ind += self.NO - 1
        return self.data[ind]

N,Q = map(int,stdin.readline().split())
S = list(stdin.readline()[:-1])
ST = SegTree(N-1,0)

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

ANS = []
for loop in range(Q):

    query = stdin.readline()[:-1]
    if query[0] == "1":
        _,ind = map(int,query.split())
        ind -= 1

        if S[ind] == ")" and ind != 0 and ST.get(ind-1) == 1:
            ST.update(i-1,0)

        if S[ind] == "(" and ind != N-1 and ST.get(ind+1) == 1:
            ST.update(i,0)

        if S[ind] == "(":
            S[ind] = ")"
        else:
            S[ind] = "("


        if S[ind] == ")" and ind != 0 and S[ind-1] == "(":
            ST.update(i-1,1)
        if S[ind] == "(" and ind != N-1 and S[ind+1] == ")":
            ST.update(i,1)

    else:
        _,l,r = map(int,query.split())
        l -= 1
        r -= 1
        ANS.append(ST.query(l,r))

print ("\n".join(map(str,ANS)))
0