結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー 👑 H20H20
提出日時 2021-12-07 02:31:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,907 ms / 2,000 ms
コード長 2,282 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 86,668 KB
実行使用メモリ 242,124 KB
最終ジャッジ日時 2023-09-21 16:28:16
合計ジャッジ時間 33,689 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 857 ms
79,468 KB
testcase_01 AC 857 ms
79,456 KB
testcase_02 AC 851 ms
80,220 KB
testcase_03 AC 781 ms
79,552 KB
testcase_04 AC 859 ms
79,592 KB
testcase_05 AC 923 ms
105,008 KB
testcase_06 AC 636 ms
86,220 KB
testcase_07 AC 276 ms
93,244 KB
testcase_08 AC 1,605 ms
147,068 KB
testcase_09 AC 420 ms
103,876 KB
testcase_10 AC 883 ms
131,524 KB
testcase_11 AC 776 ms
111,364 KB
testcase_12 AC 1,007 ms
104,928 KB
testcase_13 AC 1,517 ms
147,124 KB
testcase_14 AC 810 ms
126,188 KB
testcase_15 AC 98 ms
71,224 KB
testcase_16 AC 1,631 ms
147,612 KB
testcase_17 AC 1,584 ms
147,528 KB
testcase_18 AC 1,626 ms
148,036 KB
testcase_19 AC 1,605 ms
147,384 KB
testcase_20 AC 1,635 ms
147,696 KB
testcase_21 AC 95 ms
71,428 KB
testcase_22 AC 96 ms
71,336 KB
testcase_23 AC 1,505 ms
133,300 KB
testcase_24 AC 1,544 ms
147,248 KB
testcase_25 AC 1,614 ms
148,204 KB
testcase_26 AC 1,907 ms
241,440 KB
testcase_27 AC 1,659 ms
242,124 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#LCA(最近共通祖先)
#距離や間に挟まれているかも判定可能
import collections
import sys
sys.setrecursionlimit(10**7)

# N: 頂点数
# G[v]: 頂点vの子頂点 (親頂点は含む)

# Euler Tour の構築
N,Q = map(int, input().split())
N+=2
pair = [0]*(N)
S = list('('+input()+')')
G = [[] for _ in range(N)]
d = collections.deque()
dn = collections.deque()
d.append('x')
d.append('x')
for i in range(len(S)):
    if i>0 and S[i-1]=='(' and S[i]=='(':
        G[i].append(i-1)
        G[i-1].append(i)
    if i>0 and S[i-1]==')' and S[i]=='(':
        G[dn[-1]].append(i)
        G[i].append(dn[-1])
    d.append(S[i])
    dn.append(i)
    if d[-2]=='(' and d[-1]==')':
        d.pop()
        d.pop()
        l = dn.pop()
        r = dn.pop()
        pair[l]=r
        pair[r]=l
        G[l].append(r)
        G[r].append(l)

S = []
F = [0]*N
depth = [0]*N

def dfs(v, d , p):
    F[v] = len(S)
    depth[v] = d
    S.append(v)
    for n in G[v]:
        if p!=n:
            dfs(n, d+1, v)
            S.append(v)
dfs(0, 0, -1)

# 存在しない範囲は深さが他よりも大きくなるようにする
INF = (10**18, 0)

# LCAを計算するクエリの前計算
M = 2*N
M0 = 2**(M-1).bit_length()
data = [INF]*(2*M0)
for i, v in enumerate(S):
    data[M0-1+i] = (depth[v], i)
for i in range(M0-2, -1, -1):
    data[i] = min(data[2*i+1], data[2*i+2])

# LCAの計算 (generatorで最小値を求める)
def _query(a, b):
    yield INF
    a += M0; b += M0
    while a < b:
        if b & 1:
            b -= 1
            yield data[b-1]
        if a & 1:
            yield data[a-1]
            a += 1
        a >>= 1; b >>= 1

# LCAの計算 (外から呼び出す関数)
def query(u, v):
    fu = F[u]; fv = F[v]
    if fu > fv:
        fu, fv = fv, fu
    return S[min(_query(fu, fv+1))[1]]

# 2点間の距離
def distance(u, v):
    return depth[u]+depth[v]-2*depth[query(u, v)]

# aがuとvの間に挟まれているか
def isOnPath(u,v,a):
    return distance(u,v)==distance(u,a)+distance(v,a)


for _ in range(Q):
    x,y = map(int,input().split())
    if x==y:
        print(min(x,pair[x]),max(x,pair[x]))
    else:
        l = query(x,y)
        if l == 0:
            print(-1)
        else:
            print(min(l,pair[l]),max(l,pair[l]))
0