結果

問題 No.2786 RMQ on Grid Path
ユーザー ゼットゼット
提出日時 2024-06-14 23:13:14
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,074 bytes
コンパイル時間 402 ms
コンパイル使用メモリ 81,956 KB
実行使用メモリ 171,224 KB
最終ジャッジ日時 2024-06-14 23:15:02
合計ジャッジ時間 30,800 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,376 KB
testcase_01 AC 39 ms
53,376 KB
testcase_02 AC 110 ms
76,648 KB
testcase_03 AC 110 ms
76,416 KB
testcase_04 AC 106 ms
76,720 KB
testcase_05 AC 113 ms
76,800 KB
testcase_06 AC 109 ms
76,544 KB
testcase_07 AC 116 ms
76,708 KB
testcase_08 AC 102 ms
76,544 KB
testcase_09 AC 105 ms
76,748 KB
testcase_10 AC 97 ms
76,672 KB
testcase_11 AC 114 ms
76,448 KB
testcase_12 TLE -
testcase_13 TLE -
testcase_14 AC 5,461 ms
158,608 KB
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 AC 5,450 ms
158,212 KB
testcase_19 TLE -
testcase_20 AC 5,452 ms
158,468 KB
testcase_21 AC 5,284 ms
157,708 KB
testcase_22 AC 3,860 ms
156,320 KB
testcase_23 AC 4,168 ms
156,572 KB
testcase_24 AC 4,277 ms
158,836 KB
testcase_25 AC 4,428 ms
158,584 KB
testcase_26 AC 4,309 ms
158,200 KB
testcase_27 AC 1,682 ms
108,352 KB
testcase_28 AC 1,174 ms
112,900 KB
testcase_29 AC 3,537 ms
140,864 KB
testcase_30 AC 1,458 ms
105,592 KB
testcase_31 AC 396 ms
83,496 KB
testcase_32 AC 1,449 ms
171,224 KB
testcase_33 AC 321 ms
117,888 KB
testcase_34 AC 4,055 ms
166,616 KB
testcase_35 AC 4,127 ms
166,032 KB
testcase_36 AC 4,055 ms
166,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class dsu():
    n=1
    parent_or_size=[-1 for i in range(n)]
    def __init__(self,N):
        self.n=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
        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
H,W=map(int,input().split())
A=[list(map(int,input().split())) for i in range(H)]
C=set()
q=[]
Q=int(input())
for i in range(Q):
  a,b,c,d=map(int,input().split())
  q.append(((a-1)*W+b-1,(c-1)*W+d-1))
  C.add(i)
L=[[] for i in range(H*W+1)]
for i in range(H):
  for j in range(W):
    if i<H-1:
      x=A[i][j]
      y=A[i+1][j]
      z=max(x,y)
      L[z].append((i*W+j,(i+1)*W+j))
    if j<W-1:
      x=A[i][j]
      y=A[i][j+1]
      z=max(x,y)
      L[z].append((i*W+j,i*W+j+1))
result=[0]*Q
Z=dsu(H*W)
from math import sqrt
k=int(sqrt(H*W))
M=(H*W+k-1)//k
D=[[] for i in range(M)]
from math import sqrt
k=int(sqrt(H*W))
M=(H*W+k-1)//k
D=[[] for i in range(M+1)]
ans=[False]*Q
for i in range(M+1):
  l=i*k
  r=(i+1)*k
  for j in range(k):
    z=i*k+j
    if z>H*W:
      break
    for B in L[z]:
      pos1,pos2=B[:]
      Z.merge(pos1,pos2)
  for j in range(Q):
    p1,p2=q[j][:]
    if ans[j]==True:
      continue
    if Z.same(p1,p2)==True:
      ans[j]=True
      D[i].append(j)
result=[0]*Q
Z=dsu(H*W)
ans=[False]*Q
for i in range(M+1):
  for j in range(k):
    z=i*k+j
    if z>H*W:
      break
    for B in L[z]:
      pos1,pos2=B[:]
      Z.merge(pos1,pos2)
    if len(L[z])>0:
      for j in D[i]:
        p1,p2=q[j]
        if ans[j]==True:
          continue
        if Z.same(p1,p2)==True:
          ans[j]=True
          result[j]=z
for i in range(Q):
  print(result[i])
  
0