結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー 👑 H20H20
提出日時 2021-12-07 02:37:44
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 1,652 ms / 2,000 ms
コード長 2,154 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 86,812 KB
実行使用メモリ 268,744 KB
最終ジャッジ日時 2023-09-21 16:29:29
合計ジャッジ時間 28,114 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 592 ms
106,820 KB
testcase_01 AC 597 ms
105,684 KB
testcase_02 AC 593 ms
106,380 KB
testcase_03 AC 526 ms
104,576 KB
testcase_04 AC 600 ms
106,996 KB
testcase_05 AC 753 ms
118,992 KB
testcase_06 AC 507 ms
96,416 KB
testcase_07 AC 260 ms
93,360 KB
testcase_08 AC 1,274 ms
172,808 KB
testcase_09 AC 385 ms
104,508 KB
testcase_10 AC 726 ms
141,952 KB
testcase_11 AC 655 ms
121,116 KB
testcase_12 AC 805 ms
121,168 KB
testcase_13 AC 1,268 ms
171,096 KB
testcase_14 AC 700 ms
134,884 KB
testcase_15 AC 96 ms
71,224 KB
testcase_16 AC 1,300 ms
174,036 KB
testcase_17 AC 1,292 ms
174,968 KB
testcase_18 AC 1,306 ms
174,048 KB
testcase_19 AC 1,298 ms
172,952 KB
testcase_20 AC 1,318 ms
174,084 KB
testcase_21 AC 96 ms
71,508 KB
testcase_22 AC 96 ms
71,432 KB
testcase_23 AC 1,174 ms
156,520 KB
testcase_24 AC 1,223 ms
172,348 KB
testcase_25 AC 1,305 ms
175,164 KB
testcase_26 AC 1,652 ms
263,244 KB
testcase_27 AC 1,605 ms
268,744 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)
    elif 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**6, 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]]

XY = [list(map(int, input().split())) for _ in range(Q)]

for x,y in XY:
    if x==y:
        print(min(x,pair[x]),max(x,pair[x]))
    else:
        l = query(x,y)
        if l == 0:
            print(-1)
        else:
            if l<pair[l]:
                print(l,pair[l])
            else:
                print(pair[l],l)
0