結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2022-01-07 23:31:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 375 ms / 2,000 ms
コード長 1,886 bytes
コンパイル時間 722 ms
コンパイル使用メモリ 86,988 KB
実行使用メモリ 103,184 KB
最終ジャッジ日時 2023-09-12 14:14:41
合計ジャッジ時間 8,315 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,288 KB
testcase_01 AC 375 ms
97,348 KB
testcase_02 AC 366 ms
97,552 KB
testcase_03 AC 373 ms
96,760 KB
testcase_04 AC 369 ms
97,196 KB
testcase_05 AC 367 ms
96,788 KB
testcase_06 AC 354 ms
97,488 KB
testcase_07 AC 368 ms
97,360 KB
testcase_08 AC 361 ms
97,300 KB
testcase_09 AC 365 ms
96,928 KB
testcase_10 AC 357 ms
97,340 KB
testcase_11 AC 340 ms
97,452 KB
testcase_12 AC 332 ms
97,328 KB
testcase_13 AC 346 ms
97,320 KB
testcase_14 AC 292 ms
103,184 KB
testcase_15 AC 73 ms
71,588 KB
testcase_16 AC 73 ms
71,228 KB
testcase_17 AC 73 ms
71,500 KB
testcase_18 AC 73 ms
71,412 KB
testcase_19 AC 74 ms
71,220 KB
testcase_20 AC 74 ms
71,404 KB
testcase_21 AC 74 ms
71,344 KB
testcase_22 AC 74 ms
71,252 KB
testcase_23 AC 75 ms
71,252 KB
testcase_24 AC 74 ms
71,276 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(ind-1,0)

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

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


        if S[ind] == ")" and ind != 0 and S[ind-1] == "(":
            ST.update(ind-1,1)
        if S[ind] == "(" and ind != N-1 and S[ind+1] == ")":
            ST.update(ind,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