結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー titiatitia
提出日時 2021-12-09 03:25:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 811 ms / 2,000 ms
コード長 2,033 bytes
コンパイル時間 381 ms
コンパイル使用メモリ 86,584 KB
実行使用メモリ 156,984 KB
最終ジャッジ日時 2023-09-23 14:29:09
合計ジャッジ時間 18,541 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 465 ms
140,740 KB
testcase_01 AC 474 ms
144,836 KB
testcase_02 AC 473 ms
141,052 KB
testcase_03 AC 433 ms
141,448 KB
testcase_04 AC 477 ms
144,244 KB
testcase_05 AC 444 ms
116,504 KB
testcase_06 AC 322 ms
101,100 KB
testcase_07 AC 143 ms
82,236 KB
testcase_08 AC 792 ms
154,192 KB
testcase_09 AC 211 ms
90,616 KB
testcase_10 AC 399 ms
115,684 KB
testcase_11 AC 372 ms
108,968 KB
testcase_12 AC 475 ms
119,748 KB
testcase_13 AC 727 ms
150,816 KB
testcase_14 AC 369 ms
111,672 KB
testcase_15 AC 75 ms
70,988 KB
testcase_16 AC 773 ms
154,500 KB
testcase_17 AC 758 ms
154,876 KB
testcase_18 AC 760 ms
154,232 KB
testcase_19 AC 752 ms
153,608 KB
testcase_20 AC 773 ms
154,260 KB
testcase_21 AC 75 ms
71,224 KB
testcase_22 AC 75 ms
70,732 KB
testcase_23 AC 680 ms
150,068 KB
testcase_24 AC 692 ms
153,700 KB
testcase_25 AC 811 ms
155,244 KB
testcase_26 AC 717 ms
153,064 KB
testcase_27 AC 431 ms
156,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,Q=map(int,input().split())
S=input().strip()
Query=[list(map(int,input().split())) for i in range(Q)]

Qu=[]

TAI=[-1]*N

for i in range(N):
    s=S[i]

    if s=="(":
        Qu.append(i)
    else:
        x=Qu.pop()
        TAI[x]=i
        TAI[i]=x

E=[[] for i in range(N)]

for i in range(Q):
    x,y=Query[i]
    x-=1
    y-=1
    E[x].append(i)
    E[y].append(i)
    E[TAI[x]].append(i)
    E[TAI[y]].append(i)

    Query[i]=[x,y,TAI[x],TAI[y]]
    Query[i].sort()

# Segment tree(1-indexed,再帰を使わないもの)

A=TAI[:]
N=len(A)

def seg_function(x,y): # Segment treeで扱うfunction
    return min(x,y)

seg_el=1<<(N.bit_length()) # Segment treeの台の要素数
SEG=[0]*(2*seg_el) # 1-indexedなので、要素数2*seg_el.Segment treeの初期値で初期化

for i in range(N): # Aを対応する箇所へupdate
    SEG[i+seg_el]=A[i]

for i in range(seg_el-1,0,-1): # 親の部分もupdate
    SEG[i]=seg_function(SEG[i*2],SEG[i*2+1])

def update(n,x,seg_el): # A[n]をxへ更新(反映)
    i=n+seg_el
    SEG[i]=x
    i>>=1 # 子ノードへ
    
    while i!=0:
        SEG[i]=seg_function(SEG[i*2],SEG[i*2+1])
        i>>=1
        
def getvalues(l,r): # 区間[l,r)に関するseg_functionを調べる
    L=l+seg_el
    R=r+seg_el
    ANS=1<<30

    while L<R:
        if L & 1:
            ANS=seg_function(ANS , SEG[L])
            L+=1

        if R & 1:
            R-=1
            ANS=seg_function(ANS , SEG[R])
        L>>=1
        R>>=1

    return ANS

def bisect_on_SEG(l,x):
    L=l+seg_el
    R=seg_el*2-1
    while L<R:
        if SEG[L]<=x:
            break
        if L & 1:
            L+=1
        L>>=1
        R>>=1
 
    if SEG[L]>x:
        return N
 
    while L<seg_el:
        if SEG[L*2]<=x:
            L=L*2
        else:
            L=L*2+1
 
    return L-seg_el 

for x,y,z,w in Query:
    k=bisect_on_SEG(w,x)
    if k==N:
        print(-1)
    else:
        ANS=[k+1,TAI[k]+1]
        ANS.sort()
        print(*ANS)
        
0