結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー puznekopuzneko
提出日時 2022-01-14 23:31:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 339 ms / 2,000 ms
コード長 1,336 bytes
コンパイル時間 589 ms
コンパイル使用メモリ 87,136 KB
実行使用メモリ 155,292 KB
最終ジャッジ日時 2023-09-12 14:17:50
合計ジャッジ時間 7,904 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,536 KB
testcase_01 AC 339 ms
142,312 KB
testcase_02 AC 299 ms
142,524 KB
testcase_03 AC 307 ms
142,564 KB
testcase_04 AC 305 ms
142,384 KB
testcase_05 AC 301 ms
142,384 KB
testcase_06 AC 304 ms
142,408 KB
testcase_07 AC 302 ms
142,676 KB
testcase_08 AC 313 ms
142,352 KB
testcase_09 AC 301 ms
142,476 KB
testcase_10 AC 303 ms
142,188 KB
testcase_11 AC 306 ms
142,116 KB
testcase_12 AC 298 ms
142,164 KB
testcase_13 AC 300 ms
142,268 KB
testcase_14 AC 297 ms
155,292 KB
testcase_15 AC 76 ms
71,320 KB
testcase_16 AC 72 ms
71,324 KB
testcase_17 AC 72 ms
71,360 KB
testcase_18 AC 72 ms
71,224 KB
testcase_19 AC 72 ms
71,448 KB
testcase_20 AC 73 ms
71,416 KB
testcase_21 AC 72 ms
71,308 KB
testcase_22 AC 71 ms
71,232 KB
testcase_23 AC 72 ms
71,456 KB
testcase_24 AC 71 ms
71,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin
n, q = [int(x) for x in stdin.readline().rstrip().split()]
s = stdin.readline().rstrip()
indata = list(map(int, stdin.read().split()))

bitree = [0 for i in range(n+1)]

def btadd(ind,x):
    global bitree
    while ind <= n:
        bitree[ind] += x
        ind += ind & (-ind)

def btsum(ind):
    val = 0
    while ind > 0:
        val += bitree[ind]
        ind -= ind & (-ind)
    return val

bracket = [0 for i in range(n)]
if s[0] == ")":
    bracket[0] = 1
for i in range(1,n):
    if s[i] == ")":
        bracket[i] = 1
        if bracket[i-1] == 0:
            btadd(i+1,1)

offset = 0
for i in range(q):
    a = indata[offset]
    if a == 1:
        x = indata[offset + 1] - 1
        offset += 2
        if bracket[x] == 0:
            if x < n-1:
                if bracket[x+1] == 1:
                    btadd(x+2,-1)
            if x > 0:
                if bracket[x-1] == 0:
                    btadd(x+1,1)
        else:
            if x < n-1:
                if bracket[x+1] == 1:
                    btadd(x+2,1)
            if x > 0:
                if bracket[x-1] == 0:
                    btadd(x+1,-1)
        bracket[x] = bracket[x] ^ 1
    else:
        l, r = indata[offset + 1], indata[offset + 2]
        offset += 3
        ans = btsum(r) - btsum(l)
        print("{}".format(ans))
0