結果
問題 | No.2786 RMQ on Grid Path |
ユーザー | mkawa2 |
提出日時 | 2024-06-14 23:19:25 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,518 bytes |
コンパイル時間 | 589 ms |
コンパイル使用メモリ | 81,664 KB |
実行使用メモリ | 270,856 KB |
最終ジャッジ日時 | 2024-06-14 23:21:05 |
合計ジャッジ時間 | 17,415 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 34 ms
52,864 KB |
testcase_01 | AC | 35 ms
54,264 KB |
testcase_02 | AC | 83 ms
76,488 KB |
testcase_03 | AC | 91 ms
76,748 KB |
testcase_04 | AC | 90 ms
76,844 KB |
testcase_05 | AC | 91 ms
76,416 KB |
testcase_06 | AC | 92 ms
76,544 KB |
testcase_07 | AC | 92 ms
76,288 KB |
testcase_08 | AC | 90 ms
76,616 KB |
testcase_09 | AC | 89 ms
76,288 KB |
testcase_10 | AC | 88 ms
76,388 KB |
testcase_11 | AC | 94 ms
76,612 KB |
testcase_12 | TLE | - |
testcase_13 | TLE | - |
testcase_14 | TLE | - |
testcase_15 | TLE | - |
testcase_16 | TLE | - |
testcase_17 | TLE | - |
testcase_18 | TLE | - |
testcase_19 | TLE | - |
testcase_20 | TLE | - |
testcase_21 | TLE | - |
testcase_22 | AC | 5,468 ms
270,856 KB |
testcase_23 | AC | 5,308 ms
267,548 KB |
testcase_24 | AC | 891 ms
169,552 KB |
testcase_25 | AC | 922 ms
169,488 KB |
testcase_26 | AC | 933 ms
168,000 KB |
testcase_27 | AC | 1,766 ms
221,920 KB |
testcase_28 | AC | 1,221 ms
209,788 KB |
testcase_29 | AC | 5,316 ms
265,896 KB |
testcase_30 | AC | 1,347 ms
205,500 KB |
testcase_31 | AC | 401 ms
90,620 KB |
testcase_32 | AC | 453 ms
143,176 KB |
testcase_33 | AC | 247 ms
109,632 KB |
testcase_34 | AC | 2,713 ms
266,508 KB |
testcase_35 | AC | 2,929 ms
266,636 KB |
testcase_36 | AC | 2,736 ms
265,096 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)] h,w=LI() aa=LLI(h) a2uv=[[] for _ in range(h*w+5)] 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")