結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー titiatitia
提出日時 2021-12-09 03:24:35
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,033 bytes
コンパイル時間 323 ms
コンパイル使用メモリ 11,104 KB
実行使用メモリ 93,648 KB
最終ジャッジ日時 2023-09-23 14:27:49
合計ジャッジ時間 49,858 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,073 ms
56,772 KB
testcase_01 AC 1,102 ms
56,656 KB
testcase_02 AC 1,092 ms
56,764 KB
testcase_03 AC 930 ms
54,900 KB
testcase_04 AC 1,165 ms
56,656 KB
testcase_05 AC 1,332 ms
52,428 KB
testcase_06 AC 744 ms
35,784 KB
testcase_07 AC 110 ms
15,136 KB
testcase_08 TLE -
testcase_09 AC 323 ms
24,060 KB
testcase_10 AC 1,280 ms
53,356 KB
testcase_11 AC 1,026 ms
44,424 KB
testcase_12 AC 1,398 ms
55,872 KB
testcase_13 TLE -
testcase_14 AC 1,143 ms
48,796 KB
testcase_15 AC 16 ms
8,492 KB
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 AC 17 ms
8,384 KB
testcase_22 AC 17 ms
8,360 KB
testcase_23 TLE -
testcase_24 TLE -
testcase_25 TLE -
testcase_26 TLE -
testcase_27 AC 1,310 ms
87,996 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