結果

問題 No.1802 Range Score Query for Bracket Sequence
ユーザー titiatitia
提出日時 2022-01-07 22:47:15
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 985 ms / 2,000 ms
コード長 1,429 bytes
コンパイル時間 834 ms
コンパイル使用メモリ 10,964 KB
実行使用メモリ 11,748 KB
最終ジャッジ日時 2023-09-12 14:13:48
合計ジャッジ時間 16,133 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
8,276 KB
testcase_01 AC 974 ms
11,592 KB
testcase_02 AC 966 ms
11,568 KB
testcase_03 AC 970 ms
11,588 KB
testcase_04 AC 972 ms
11,572 KB
testcase_05 AC 966 ms
11,592 KB
testcase_06 AC 982 ms
11,584 KB
testcase_07 AC 985 ms
11,508 KB
testcase_08 AC 971 ms
11,688 KB
testcase_09 AC 977 ms
11,584 KB
testcase_10 AC 979 ms
11,640 KB
testcase_11 AC 861 ms
11,748 KB
testcase_12 AC 862 ms
11,664 KB
testcase_13 AC 877 ms
11,688 KB
testcase_14 AC 914 ms
11,656 KB
testcase_15 AC 15 ms
8,272 KB
testcase_16 AC 16 ms
8,244 KB
testcase_17 AC 16 ms
8,196 KB
testcase_18 AC 15 ms
8,236 KB
testcase_19 AC 16 ms
8,236 KB
testcase_20 AC 16 ms
8,316 KB
testcase_21 AC 16 ms
8,356 KB
testcase_22 AC 16 ms
8,208 KB
testcase_23 AC 16 ms
8,312 KB
testcase_24 AC 16 ms
7,796 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

        if S[y]==")":
            print(getvalue(z)-getvalue(y))
        else:
            print(getvalue(z)-getvalue(y-1))
            

        
            
            
    
0