結果

問題 No.2786 RMQ on Grid Path
ユーザー ShirotsumeShirotsume
提出日時 2024-06-14 22:01:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,795 ms / 6,000 ms
コード長 3,257 bytes
コンパイル時間 392 ms
コンパイル使用メモリ 82,420 KB
実行使用メモリ 291,056 KB
最終ジャッジ日時 2024-06-14 22:03:11
合計ジャッジ時間 74,262 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
57,532 KB
testcase_01 AC 52 ms
64,520 KB
testcase_02 AC 114 ms
78,044 KB
testcase_03 AC 120 ms
78,520 KB
testcase_04 AC 114 ms
78,628 KB
testcase_05 AC 116 ms
78,064 KB
testcase_06 AC 111 ms
78,048 KB
testcase_07 AC 128 ms
78,548 KB
testcase_08 AC 103 ms
77,776 KB
testcase_09 AC 103 ms
77,724 KB
testcase_10 AC 100 ms
77,720 KB
testcase_11 AC 124 ms
78,240 KB
testcase_12 AC 3,795 ms
284,984 KB
testcase_13 AC 3,726 ms
286,940 KB
testcase_14 AC 3,598 ms
286,740 KB
testcase_15 AC 3,608 ms
287,696 KB
testcase_16 AC 3,579 ms
284,368 KB
testcase_17 AC 3,479 ms
285,368 KB
testcase_18 AC 3,653 ms
284,876 KB
testcase_19 AC 3,518 ms
288,424 KB
testcase_20 AC 3,610 ms
285,984 KB
testcase_21 AC 3,548 ms
287,340 KB
testcase_22 AC 2,805 ms
284,984 KB
testcase_23 AC 2,672 ms
285,320 KB
testcase_24 AC 2,948 ms
284,384 KB
testcase_25 AC 2,849 ms
287,316 KB
testcase_26 AC 2,728 ms
284,004 KB
testcase_27 AC 1,331 ms
154,340 KB
testcase_28 AC 1,056 ms
137,384 KB
testcase_29 AC 3,247 ms
291,056 KB
testcase_30 AC 1,255 ms
133,180 KB
testcase_31 AC 516 ms
88,552 KB
testcase_32 AC 2,363 ms
287,528 KB
testcase_33 AC 716 ms
211,924 KB
testcase_34 AC 2,372 ms
290,248 KB
testcase_35 AC 2,356 ms
290,628 KB
testcase_36 AC 2,374 ms
290,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys, time, random
from collections import deque, Counter, defaultdict
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
mi = lambda: map(int, input().split())
li = lambda: list(mi())
inf = 2 ** 61 - 1
mod = 998244353
class dsu():
    n=1
    parent_or_size=[-1 for i in range(n)]
    def __init__(self,N):
        self.n=N
        self.num = N
        self.parent_or_size=[-1 for i in range(N)]
    def merge(self,a,b):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n)
        x=self.leader(a)
        y=self.leader(b)
        if x==y:
            return x
        self.num -= 1
        if (-self.parent_or_size[x]<-self.parent_or_size[y]):
            x,y=y,x
        self.parent_or_size[x]+=self.parent_or_size[y]
        self.parent_or_size[y]=x
        return x
    def same(self,a,b):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        assert 0<=b<self.n, "0<=b<n,b={0},n={1}".format(b,self.n)
        return self.leader(a)==self.leader(b)
    def leader(self,a):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        if (self.parent_or_size[a]<0):
            return a
        self.parent_or_size[a]=self.leader(self.parent_or_size[a])
        return self.parent_or_size[a]
    def size(self,a):
        assert 0<=a<self.n, "0<=a<n,a={0},n={1}".format(a,self.n)
        return -self.parent_or_size[self.leader(a)]
    def groups(self):
        leader_buf=[0 for i in range(self.n)]
        group_size=[0 for i in range(self.n)]
        for i in range(self.n):
            leader_buf[i]=self.leader(i)
            group_size[leader_buf[i]]+=1
        result=[[] for i in range(self.n)]
        for i in range(self.n):
            result[leader_buf[i]].append(i)
        result2=[]
        for i in range(self.n):
            if len(result[i])>0:
                result2.append(result[i])
        return result2
    def count(self):
        return self.num


h, w = mi()

a = [li() for _ in range(h)]

q = ii()
S = [[v - 1 for v in li()] for _ in range(q)]
SXSY = []

for i in range(q):
    sx, sy, tx, ty = S[i]
    SXSY.append((sx* w + sy, tx * w + ty))
cost = []
for i in range(h):
    for j in range(w):
        cost.append((i * w + j, a[i][j]))
cost.sort(key = lambda x: x[1])

ok = [h * w] * q
ng = [0] * q
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
dz = 'UDLR'
MID = [[] for _ in range(h * w)]
mid = [0] * q
EDGE = [[] for _ in range(h * w)]
for i in range(h * w):
    X, c = cost[i]
    x = X // w
    y = X % w
    for j in range(4):
        nx = x + dx[j]
        ny = y + dy[j]
        if 0 <= nx < h and 0 <= ny < w and a[nx][ny] <= c:
            EDGE[i].append(nx * w + ny)
ans = []
for _ in range(18):
    MID = [[] for _ in range(h * w)]
    for i in range(q):
        mid[i] = (ok[i] + ng[i]) // 2
        MID[mid[i]].append(i)
    U = dsu(h * w)
    for i in range(h * w):
        u, c = cost[i]
        for v in EDGE[i]:
            U.merge(u, v)
        for j in MID[i]:
            s, t = SXSY[j]
            if U.same(s, t):
                ok[j] = mid[j]
            else:
                ng[j] = mid[j]

for i in range(q):
    print(cost[ok[i]][1])
            
        
        
0