結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー 👑 rin204rin204
提出日時 2022-01-23 09:32:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 755 ms / 2,000 ms
コード長 2,256 bytes
コンパイル時間 323 ms
コンパイル使用メモリ 87,216 KB
実行使用メモリ 104,760 KB
最終ジャッジ日時 2023-09-12 14:18:18
合計ジャッジ時間 13,285 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,236 KB
testcase_01 AC 730 ms
104,124 KB
testcase_02 AC 713 ms
104,280 KB
testcase_03 AC 719 ms
104,248 KB
testcase_04 AC 731 ms
104,324 KB
testcase_05 AC 704 ms
104,108 KB
testcase_06 AC 707 ms
103,992 KB
testcase_07 AC 717 ms
104,500 KB
testcase_08 AC 709 ms
104,240 KB
testcase_09 AC 723 ms
104,760 KB
testcase_10 AC 713 ms
104,108 KB
testcase_11 AC 693 ms
104,236 KB
testcase_12 AC 681 ms
104,144 KB
testcase_13 AC 678 ms
104,288 KB
testcase_14 AC 755 ms
104,388 KB
testcase_15 AC 73 ms
71,248 KB
testcase_16 AC 73 ms
71,200 KB
testcase_17 AC 74 ms
71,432 KB
testcase_18 AC 72 ms
71,288 KB
testcase_19 AC 75 ms
71,520 KB
testcase_20 AC 73 ms
71,316 KB
testcase_21 AC 73 ms
71,140 KB
testcase_22 AC 75 ms
71,324 KB
testcase_23 AC 74 ms
71,112 KB
testcase_24 AC 74 ms
71,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegTree():
    def __init__(self, n, e, ope, lst=[]):
        self.N0 = 2 ** (n - 1).bit_length()
        self.e = e
        self.ope = ope
        self.data = [e] * (2 * self.N0)
        if lst:
            for i in range(n):
                self.data[self.N0 + i] = lst[i]
            for i in range(self.N0 - 1, 0, -1):
                self.data[i] = self.ope(self.data[2 * i], self.data[2 * i + 1])
    
    def f5(self):
        for i in range(self.N0 - 1, 0, -1):
            self.data[i] = self.ope(self.data[2 * i], self.data[2 * i + 1])
                
    def update(self, i, x): #a_iの値をxに更新
        i += self.N0
        self.data[i] = x
        while i > 1:
            i >>= 1
            self.data[i] = self.ope(self.data[2 * i], self.data[2 * i + 1])
    
    def add(self, i, x):
        self.update(i, x + self.get(i))
    
    def query(self, l, r): #区間[l, r)での演算結果
        if r <= l:
            return self.e
            
        res = self.e
        l += self.N0
        r += self.N0
        while l < r:
            if l & 1:
                res = self.ope(res, self.data[l])
                l += 1
            if r & 1:
                r -= 1
                res = self.ope(res, self.data[r])
            l >>= 1
            r >>= 1
        return res
            
    
    def get(self, i): #a_iの値を返す
        return self.data[self.N0 + i]

n, q = map(int, input().split())
S = list(input())
lst = []
for i in range(n - 1):
    if S[i] == "(" and S[i + 1] == ")":
        lst.append(1)
    else:
        lst.append(0)

e = 0        
def ope(x, y):
    return x + y
seg = SegTree(n - 1, e, ope, lst)

for _ in range(q):
    query = list(map(int, input().split()))
    if query[0] == 1:
        i = query[1] - 1
        if S[i] == "(":
            if i != n - 1 and S[i + 1] == ")":
                seg.update(i, 0)
            S[i] = ")"
            if i != 0 and S[i - 1] == "(":
                seg.update(i - 1, 1)
        else:
            if i != 0 and S[i - 1] == "(":
                seg.update(i - 1, 0)
            S[i] = "("
            if i != n - 1 and S[i + 1] == ")":
                seg.update(i, 1)
    else:
        l, r = query[1:]
        print(seg.query(l - 1, r - 1))

0