結果
問題 | No.2786 RMQ on Grid Path |
ユーザー | mkawa2 |
提出日時 | 2024-06-14 23:08:28 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,546 bytes |
コンパイル時間 | 491 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 273,464 KB |
最終ジャッジ日時 | 2024-06-14 23:10:03 |
合計ジャッジ時間 | 11,139 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
54,528 KB |
testcase_01 | AC | 41 ms
54,784 KB |
testcase_02 | AC | 93 ms
76,972 KB |
testcase_03 | AC | 92 ms
76,820 KB |
testcase_04 | AC | 93 ms
77,056 KB |
testcase_05 | AC | 92 ms
77,056 KB |
testcase_06 | AC | 97 ms
76,984 KB |
testcase_07 | AC | 94 ms
77,092 KB |
testcase_08 | AC | 92 ms
76,904 KB |
testcase_09 | AC | 92 ms
77,032 KB |
testcase_10 | AC | 90 ms
76,992 KB |
testcase_11 | AC | 98 ms
76,928 KB |
testcase_12 | AC | 5,808 ms
272,124 KB |
testcase_13 | TLE | - |
testcase_14 | AC | 5,853 ms
271,228 KB |
testcase_15 | AC | 5,990 ms
272,168 KB |
testcase_16 | AC | 5,848 ms
271,172 KB |
testcase_17 | AC | 5,890 ms
270,972 KB |
testcase_18 | AC | 5,851 ms
270,028 KB |
testcase_19 | AC | 5,842 ms
269,784 KB |
testcase_20 | AC | 5,800 ms
271,976 KB |
testcase_21 | AC | 5,842 ms
272,048 KB |
testcase_22 | AC | 5,287 ms
273,464 KB |
testcase_23 | AC | 5,300 ms
271,864 KB |
testcase_24 | AC | 841 ms
162,844 KB |
testcase_25 | AC | 791 ms
160,944 KB |
testcase_26 | AC | 826 ms
159,604 KB |
testcase_27 | AC | 1,633 ms
222,712 KB |
testcase_28 | AC | 1,113 ms
209,968 KB |
testcase_29 | AC | 5,068 ms
264,756 KB |
testcase_30 | AC | 1,288 ms
207,440 KB |
testcase_31 | AC | 411 ms
92,056 KB |
testcase_32 | AC | 400 ms
133,932 KB |
testcase_33 | AC | 226 ms
109,840 KB |
testcase_34 | AC | 2,441 ms
264,012 KB |
testcase_35 | AC | 2,578 ms
263,532 KB |
testcase_36 | AC | 2,556 ms
264,252 KB |
ソースコード
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)] from collections import defaultdict h,w=LI() aa=LLI(h) a2uv=defaultdict(list) sa=set() for i in range(h): for j,a in enumerate(aa[i]): u=i*w+j if i: b=aa[i-1][j] a2uv[max(a,b)].append((u,u-w)) if j: b=aa[i][j-1] a2uv[max(a,b)].append((u,u-1)) sa.add(a) q=II() st=[] for _ in range(q): si,sj,gi,gj=LI1() st.append((si*w+sj,gi*w+gj)) sa=sorted(sa) n=len(sa) ll=[-1]*q rr=[n-1]*q ii=list(range(q)) ans=[-1]*q while ii: m2i=[[] for _ in range(n)] nii=[] for i in ii: l,r=ll[i],rr[i] m=(l+r)//2 m2i[m].append(i) uf=UnionFind(h*w) for m,a in enumerate(sa): for u,v in a2uv[a]:uf.merge(u,v) for i in m2i[m]: s,t=st[i] if uf.same(s,t): rr[i]=m else: ll[i]=m if ll[i]+1==rr[i]: ans[i]=sa[rr[i]] else: nii.append(i) ii=nii print(*ans,sep="\n")