結果
問題 | No.2786 RMQ on Grid Path |
ユーザー | mkawa2 |
提出日時 | 2024-06-15 19:06:19 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,966 bytes |
コンパイル時間 | 365 ms |
コンパイル使用メモリ | 82,428 KB |
実行使用メモリ | 267,952 KB |
最終ジャッジ日時 | 2024-06-15 19:07:15 |
合計ジャッジ時間 | 52,153 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
53,484 KB |
testcase_01 | AC | 41 ms
53,744 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | AC | 2,275 ms
258,760 KB |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
testcase_31 | WA | - |
testcase_32 | AC | 1,212 ms
235,288 KB |
testcase_33 | WA | - |
testcase_34 | WA | - |
testcase_35 | WA | - |
testcase_36 | WA | - |
ソースコード
import sys # sys.setrecursionlimit(1000006) # sys.set_int_max_str_digits(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] # inf = -1-(-1 << 31) inf = -1-(-1 << 62) # md = 10**9+7 md = 998244353 class UnionFind: def __init__(self, n): self._tree = [-1]*n # number of connected component self.cnt = n def root(self, u): stack = [] while self._tree[u] >= 0: stack.append(u) u = self._tree[u] for v in stack: self._tree[v] = u return u def same(self, u, v): return self.root(u) == self.root(v) def merge(self, u, v): u, v = self.root(u), self.root(v) if u == v: return False if self._tree[u] > self._tree[v]: u, v = v, u self._tree[u] += self._tree[v] self._tree[v] = u self.cnt -= 1 return True # size of connected component def size(self, u): return -self._tree[self.root(u)] class LCA: def __init__(self, parent, depth, op, e, weight): n = len(parent) self._depth = depth self._op = op self._e = e log = max(self._depth).bit_length() self._table = [parent]+[[-1]*n for _ in range(log)] self._val = [weight]+[[e for _ in range(n)] for _ in range(log)] row0 = self._table[0] vrow0 = self._val[0] for lv in range(log): row1 = self._table[lv+1] vrow1 = self._val[lv+1] for u in range(n): if row0[u] == -1: continue row1[u] = row0[row0[u]] vrow1[u] = op(vrow0[u], vrow0[row0[u]]) row0 = row1 vrow0 = vrow1 def anc(self, u, v): diff = self._depth[u]-self._depth[v] if diff < 0: u, v = v, u diff = abs(diff) u, p = self.up(u, diff) if u == v: return u, p for lv in range(self._depth[u].bit_length()-1, -1, -1): anclv = self._table[lv] if anclv[u] != anclv[v]: p = self._op(p, self._op(self._val[lv][u], self._val[lv][v])) u, v = anclv[u], anclv[v] return self._table[0][u], p def up(self, u, dist): lv = 0 p = self._e while dist and u != -1: if dist & 1: u, p = self._table[lv][u], self._op(p, self._val[lv][u]) lv, dist = lv+1, dist >> 1 return u, p def dfs(root=0): uu, stack = [], [root] while stack: u = stack.pop() uu.append(u) for v, c in to[u]: if v == parent[u]: continue parent[v] = u depth[v] = depth[u]+1 weight[v] = c stack.append(v) return uu h, w = LI() aa = LLI(h) auv = [] for i in range(h): for j, a in enumerate(aa[i]): u = i*w+j if i: b = aa[i-1][j] auv.append((max(a, b), u-w, u)) if j: b = aa[i][j-1] auv.append((max(a, b), u-1, u)) uf = UnionFind(h*w) auv.sort(key=lambda x: x[0]) to = [[] for _ in range(h*w)] for a, u, v in auv: if uf.merge(u, v): to[u].append((v, a)) to[v].append((u, a)) n = h*w parent, depth, weight = [-1]*n, [0]*n, [0]*n dfs() lca = LCA(parent, depth, max, 0, weight) q = II() st = [] for _ in range(q): si, sj, gi, gj = LI1() u = si*w+sj v = gi*w+gj _, ans = lca.anc(u, v) print(ans)