結果
問題 | No.1778 括弧列クエリ / Bracketed Sequence Query |
ユーザー | tamato |
提出日時 | 2021-12-07 14:28:35 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 795 ms / 2,000 ms |
コード長 | 2,904 bytes |
コンパイル時間 | 163 ms |
コンパイル使用メモリ | 82,308 KB |
実行使用メモリ | 196,244 KB |
最終ジャッジ日時 | 2024-07-07 14:05:23 |
合計ジャッジ時間 | 18,053 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 369 ms
82,940 KB |
testcase_01 | AC | 355 ms
82,892 KB |
testcase_02 | AC | 348 ms
83,400 KB |
testcase_03 | AC | 320 ms
81,724 KB |
testcase_04 | AC | 353 ms
82,516 KB |
testcase_05 | AC | 430 ms
120,172 KB |
testcase_06 | AC | 313 ms
87,232 KB |
testcase_07 | AC | 150 ms
94,692 KB |
testcase_08 | AC | 741 ms
191,108 KB |
testcase_09 | AC | 329 ms
115,072 KB |
testcase_10 | AC | 532 ms
163,300 KB |
testcase_11 | AC | 433 ms
134,196 KB |
testcase_12 | AC | 448 ms
120,536 KB |
testcase_13 | AC | 743 ms
188,664 KB |
testcase_14 | AC | 508 ms
154,952 KB |
testcase_15 | AC | 33 ms
52,992 KB |
testcase_16 | AC | 751 ms
192,060 KB |
testcase_17 | AC | 771 ms
192,004 KB |
testcase_18 | AC | 772 ms
195,540 KB |
testcase_19 | AC | 795 ms
196,244 KB |
testcase_20 | AC | 793 ms
192,680 KB |
testcase_21 | AC | 36 ms
52,992 KB |
testcase_22 | AC | 34 ms
52,992 KB |
testcase_23 | AC | 596 ms
168,184 KB |
testcase_24 | AC | 695 ms
191,420 KB |
testcase_25 | AC | 727 ms
192,572 KB |
testcase_26 | AC | 731 ms
191,648 KB |
testcase_27 | AC | 479 ms
190,760 KB |
ソースコード
mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline def STfunc(a, b): if a[0] < b[0]: return a else: return b # クエリは0-indexedで[l, r) class SparseTable(): def __init__(self, A): # A: 処理したい数列 self.N = len(A) self.K = self.N.bit_length() - 1 self.table = [[0] * (self.K + 1) for _ in range(self.N)] for i, a in enumerate(A): self.table[i][0] = a for k in range(1, self.K + 1): for i in range(self.N): j = i + (1 << (k - 1)) if j <= self.N - 1: self.table[i][k] = STfunc(self.table[i][k - 1], self.table[j][k - 1]) else: self.table[i][k] = self.table[i][k - 1] def query(self, l, r): # [l, r)の最小値を求める k = (r - l).bit_length() - 1 return STfunc(self.table[l][k], self.table[r - (1 << k)][k]) # adj[0] must be empty list def EulerTour(adj, root): st = [root] ret = [] seen = [0] * len(adj) par = [0] * len(adj) depth = [0] * len(adj) while st: v = st.pop() if seen[v]: ret.append(v) continue ret.append(v) seen[v] = 1 if par[v] != 0: st.append(par[v]) for u in adj[v]: if seen[u] == 0: st.append(u) par[u] = v depth[u] = depth[v] + 1 return ret, depth N, Q = map(int, input().split()) S = input().rstrip('\n') P = [0] * (N+1) adj = [[] for _ in range(N+2)] st = [] for i, s in enumerate(S): if s == "(": st.append(i+1) else: j = st.pop() P[i+1] = j P[j] = i+1 if st: adj[j+1].append(st[-1] + 1) adj[st[-1] + 1].append(j+1) else: adj[j+1].append(1) adj[1].append(j+1) et, depth = EulerTour(adj, 1) depth_list = [None] * len(et) left = [-1] * (N + 2) right = [-1] * (N + 2) for i, v in enumerate(et): depth_list[i] = (depth[v], v) if left[v] < 0: left[v] = i right[v] = i ST = SparseTable(depth_list) for _ in range(Q): x, y = map(int, input().split()) xx, yy = P[x], P[y] if x > xx: x, xx = xx, x if y > yy: y, yy = yy, y lca_depth, lca = ST.query(min(left[x+1], left[y+1]), max(right[x+1], right[y+1]) + 1) if lca != 1: print(lca - 1, P[lca - 1]) else: print(-1) if __name__ == '__main__': main()