結果
問題 | No.1778 括弧列クエリ / Bracketed Sequence Query |
ユーザー | 👑 H20 |
提出日時 | 2021-12-07 02:08:21 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,308 bytes |
コンパイル時間 | 674 ms |
コンパイル使用メモリ | 11,156 KB |
実行使用メモリ | 56,232 KB |
最終ジャッジ日時 | 2023-09-21 16:25:40 |
合計ジャッジ時間 | 18,899 ms |
ジャッジサーバーID (参考情報) |
judge15 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,754 ms
8,820 KB |
testcase_01 | AC | 1,800 ms
8,860 KB |
testcase_02 | AC | 1,767 ms
8,780 KB |
testcase_03 | AC | 1,570 ms
8,748 KB |
testcase_04 | AC | 1,835 ms
8,944 KB |
testcase_05 | AC | 1,813 ms
56,232 KB |
testcase_06 | AC | 1,035 ms
19,196 KB |
testcase_07 | AC | 248 ms
33,108 KB |
testcase_08 | TLE | - |
testcase_09 | -- | - |
testcase_10 | -- | - |
testcase_11 | -- | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
testcase_21 | -- | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
ソースコード
#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]))