結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー titiatitia
提出日時 2022-01-07 22:44:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,333 bytes
コンパイル時間 200 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 14,308 KB
最終ジャッジ日時 2024-04-26 18:50:16
合計ジャッジ時間 16,935 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,880 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 WA -
testcase_15 WA -
testcase_16 AC 30 ms
10,880 KB
testcase_17 AC 29 ms
10,880 KB
testcase_18 WA -
testcase_19 AC 30 ms
10,880 KB
testcase_20 WA -
testcase_21 AC 29 ms
10,880 KB
testcase_22 AC 29 ms
10,880 KB
testcase_23 AC 29 ms
10,880 KB
testcase_24 AC 29 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,Q=map(int,input().split())
S=list(input().strip())

LEN=N

BIT=[0]*(LEN+1) # 1-indexedなtree. 配列BITの長さはLEN+1にしていることに注意。

def update(v,w): # index vにwを加える
    while v<=LEN:
        BIT[v]+=w
        v+=(v&(-v)) # v&(-v)で、最も下の立っているビット. 自分を含む大きなノードへ. たとえばv=3→v=4

def getvalue(v): # [1,v]の区間の和を求める
    if v<=0:
        return 0
    ANS=0
    while v!=0:
        ANS+=BIT[v]
        v-=(v&(-v)) # 自分より小さい自分の和を構成するノードへ. たとえばv=14→v=12へ
    return ANS

for i in range(1,N):
    if S[i-1]=="(" and S[i]==")":
        update(i,1)
        
for i in range(Q):
    X=list(map(int,input().split()))

    if X[0]==1:
        y=X[1]-1

        if S[y]=="(":
            if y+1<N and S[y+1]==")":
                update(y+1,-1)

            if y-1>=0 and S[y-1]=="(":
                update(y,1)
            S[y]=")"

        else:
            if y+1<N and S[y+1]==")":
                update(y+1,1)

            if y-1>=0 and S[y-1]=="(":
                update(y,-1)
            S[y]="("
    else:
        y,z=X[1],X[2]
        y-=1
        z-=1

        print(getvalue(z)-getvalue(y-1))

        
            
            
    
0