結果

問題 No.2786 RMQ on Grid Path
ユーザー ShirotsumeShirotsume
提出日時 2024-06-14 21:49:29
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,054 bytes
コンパイル時間 226 ms
コンパイル使用メモリ 82,384 KB
実行使用メモリ 89,328 KB
最終ジャッジ日時 2024-06-14 21:49:39
合計ジャッジ時間 10,493 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
57,840 KB
testcase_01 AC 62 ms
68,332 KB
testcase_02 AC 123 ms
78,652 KB
testcase_03 AC 123 ms
78,616 KB
testcase_04 AC 115 ms
78,064 KB
testcase_05 AC 127 ms
78,676 KB
testcase_06 AC 119 ms
78,688 KB
testcase_07 AC 143 ms
78,656 KB
testcase_08 AC 113 ms
78,128 KB
testcase_09 AC 112 ms
78,064 KB
testcase_10 AC 109 ms
77,664 KB
testcase_11 AC 142 ms
78,320 KB
testcase_12 TLE -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

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()
SXSY = [[v - 1 for v in li()] for _ in range(q)]

cost = []
for i in range(h):
    for j in range(w):
        cost.append((i, j, a[i][j]))
cost.sort(key = lambda x: x[2])

ok = [h * w] * q
ng = [0] * q
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
dz = 'UDLR'
for _ in range(18):
    mid = [0] * q
    for i in range(q):
        mid[i] = (ok[i] + ng[i]) // 2
    MID = [[] for _ in range(h * w)]
    
    for i in range(q):
        MID[mid[i]].append(i)
    U = dsu(h * w)
    for i in range(h * w):
        x, y, c = cost[i]
        for k in range(4):
            tox = dx[k] + x
            toy = dy[k] + y
            if 0 <= tox < h and 0 <= toy < w and a[tox][toy] <= c:
                U.merge(x * w + y, tox * w + toy)
        for j in MID[i]:
            sx, sy, tx, ty = SXSY[j]
            if U.same(sx * w + sy, tx * w + ty):
                ok[j] = mid[j]
            else:
                ng[j] = mid[j]
for i in range(q):
    print(cost[ok[i]][2])
            
        
        
0