結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー SPD_9X2
提出日時 2022-01-07 23:31:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 343 ms / 2,000 ms
コード長 1,886 bytes
コンパイル時間 183 ms
コンパイル使用メモリ 82,472 KB
実行使用メモリ 102,604 KB
最終ジャッジ日時 2024-06-30 02:18:38
合計ジャッジ時間 6,567 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 24
権限があれば一括ダウンロードができます

ソースコード

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