結果

問題 No.1778 括弧列クエリ / Bracketed Sequence Query
ユーザー 👑 H20H20
提出日時 2021-12-07 02:06:04
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,308 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 86,612 KB
実行使用メモリ 252,608 KB
最終ジャッジ日時 2023-09-21 16:24:27
合計ジャッジ時間 37,306 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 878 ms
79,516 KB
testcase_01 AC 865 ms
79,256 KB
testcase_02 AC 853 ms
80,080 KB
testcase_03 AC 757 ms
79,664 KB
testcase_04 AC 862 ms
80,020 KB
testcase_05 AC 945 ms
109,564 KB
testcase_06 AC 622 ms
87,264 KB
testcase_07 AC 273 ms
95,800 KB
testcase_08 AC 1,739 ms
153,256 KB
testcase_09 AC 533 ms
105,392 KB
testcase_10 AC 883 ms
137,676 KB
testcase_11 AC 789 ms
117,008 KB
testcase_12 AC 1,000 ms
109,452 KB
testcase_13 AC 1,566 ms
152,780 KB
testcase_14 AC 923 ms
133,696 KB
testcase_15 AC 99 ms
71,548 KB
testcase_16 AC 1,750 ms
158,796 KB
testcase_17 AC 1,790 ms
154,792 KB
testcase_18 AC 1,778 ms
154,684 KB
testcase_19 AC 1,620 ms
154,816 KB
testcase_20 AC 1,894 ms
155,480 KB
testcase_21 AC 153 ms
71,272 KB
testcase_22 AC 96 ms
71,300 KB
testcase_23 AC 1,528 ms
141,820 KB
testcase_24 AC 1,566 ms
155,540 KB
testcase_25 AC 1,690 ms
155,892 KB
testcase_26 TLE -
testcase_27 AC 1,703 ms
251,856 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,1))
        G[i-1].append((i,1))
    if i>0 and S[i-1]==')' and S[i]=='(':
        G[dn[-1]].append((i,1))
        G[i].append((dn[-1],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,1))
        G[r].append((l,1))

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

def dfs(v, d , p):
    F[v] = len(S)
    depth[v] = d
    S.append(v)
    for n,w in G[v]:
        if p!=n:
            dfs(n, d+w, 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