結果
問題 | No.1778 括弧列クエリ / Bracketed Sequence Query |
ユーザー | Wizist |
提出日時 | 2021-12-11 20:18:53 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 1,781 bytes |
コンパイル時間 | 400 ms |
コンパイル使用メモリ | 82,340 KB |
実行使用メモリ | 132,248 KB |
最終ジャッジ日時 | 2024-07-20 02:54:47 |
合計ジャッジ時間 | 27,105 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 758 ms
79,564 KB |
testcase_01 | AC | 791 ms
79,204 KB |
testcase_02 | AC | 774 ms
78,836 KB |
testcase_03 | AC | 757 ms
80,008 KB |
testcase_04 | AC | 761 ms
78,980 KB |
testcase_05 | AC | 777 ms
100,304 KB |
testcase_06 | AC | 527 ms
83,540 KB |
testcase_07 | AC | 211 ms
89,220 KB |
testcase_08 | AC | 1,428 ms
131,416 KB |
testcase_09 | AC | 415 ms
97,364 KB |
testcase_10 | AC | 794 ms
118,208 KB |
testcase_11 | AC | 631 ms
106,188 KB |
testcase_12 | AC | 782 ms
99,788 KB |
testcase_13 | AC | 1,198 ms
129,296 KB |
testcase_14 | AC | 685 ms
116,068 KB |
testcase_15 | AC | 38 ms
52,872 KB |
testcase_16 | AC | 1,315 ms
131,816 KB |
testcase_17 | AC | 1,370 ms
132,248 KB |
testcase_18 | AC | 1,323 ms
131,828 KB |
testcase_19 | AC | 1,601 ms
131,740 KB |
testcase_20 | AC | 1,837 ms
131,980 KB |
testcase_21 | AC | 41 ms
52,868 KB |
testcase_22 | AC | 42 ms
53,280 KB |
testcase_23 | AC | 1,088 ms
116,508 KB |
testcase_24 | AC | 1,115 ms
127,744 KB |
testcase_25 | AC | 1,285 ms
127,560 KB |
testcase_26 | RE | - |
testcase_27 | RE | - |
ソースコード
class LCA: def __init__(self, g, root=0): n, self.g, self.tick, self.anc, m = len(g), g, 0, [], len(g) self.tin, self.tout, self.pos, self.d, self.par = [0] * n, [0] * n, [0] * n, [0] * n, [-1] * n while m != 0: self.anc.append([0] * n); m //= 2 def dfs(u): self.pos[self.tick] = u self.tin[u] = self.tick self.tick += 1 self.anc[0][u] = max(self.par[u], 0) for i in range(1, len(self.anc)): self.anc[i][u] = self.anc[i - 1][self.anc[i - 1][u]] for v in self.g[u]: if self.par[u] != v: self.par[v] = u self.d[v] = self.d[u] + 1 dfs(v) self.tout[u] = self.tick dfs(root) def is_ancestor(self, u, v): return self.tin[u] <= self.tin[v] and self.tout[u] >= self.tout[v] def lca(self, u, v): if self.is_ancestor(u, v): return u if self.is_ancestor(v, u): return v for i in range(len(self.anc) - 1, -1, -1): if not self.is_ancestor(self.anc[i][u], v): u = self.anc[i][u] return self.anc[0][u] def dist(self, u, v): return 0 if u == v else self.d[u] + self.d[v] - self.d[self.lca(u, v)] * 2 def kth(self, u, k): # k-th parent if k > self.d[u]: return -1 i = 0 while k > 0: if (k & 1) != 0: u = self.anc[i][u] k, i = k >> 1, i + 1 return u from bisect import bisect_left N, Q = map(int, input().split()) s = '(' + input().strip() + ')' n = N + 2 p, st = [0] * n, [] for i, x in enumerate(s): if x == '(': st.append(i) else: p[i] = st[-1] p[st[-1]] = i st.pop() g = [[] for _ in range(n)] def build(u, c): if c <= 2: return v = u + 1 while v < u + c and p[v] > v: g[u].append(v) build(v, p[v] - v + 1) v = p[v] + 1 build(0, n) lca = LCA(g) for _ in range(Q): x, y = map(int, input().split()) t = lca.lca(min(x, p[x]), min(y, p[y])) if t == 0: print(-1) else: print(t, p[t])