結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー puznekopuzneko
提出日時 2022-01-14 23:22:54
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,354 bytes
コンパイル時間 535 ms
コンパイル使用メモリ 82,580 KB
実行使用メモリ 150,596 KB
最終ジャッジ日時 2024-04-30 17:25:22
合計ジャッジ時間 7,424 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,000 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 AC 287 ms
150,596 KB
testcase_15 WA -
testcase_16 AC 40 ms
52,452 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 38 ms
53,268 KB
testcase_22 AC 39 ms
53,264 KB
testcase_23 WA -
testcase_24 AC 40 ms
52,708 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] = 0
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)
            bracket[x] = 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] = 1
    else:
        l, r = indata[offset + 1], indata[offset + 2]
        offset += 3
        ans = btsum(r) - btsum(l)
        print("{}".format(ans))
0