結果
問題 | No.1141 田グリッド |
ユーザー | kohei2019 |
提出日時 | 2022-07-20 22:19:58 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 551 ms / 2,000 ms |
コード長 | 4,720 bytes |
コンパイル時間 | 382 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 101,308 KB |
最終ジャッジ日時 | 2024-07-02 14:02:30 |
合計ジャッジ時間 | 12,794 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 38 ms
53,376 KB |
testcase_01 | AC | 37 ms
53,504 KB |
testcase_02 | AC | 35 ms
53,504 KB |
testcase_03 | AC | 36 ms
53,760 KB |
testcase_04 | AC | 35 ms
53,120 KB |
testcase_05 | AC | 36 ms
53,120 KB |
testcase_06 | AC | 35 ms
53,248 KB |
testcase_07 | AC | 36 ms
53,248 KB |
testcase_08 | AC | 80 ms
77,552 KB |
testcase_09 | AC | 70 ms
77,012 KB |
testcase_10 | AC | 82 ms
77,280 KB |
testcase_11 | AC | 82 ms
77,440 KB |
testcase_12 | AC | 82 ms
77,268 KB |
testcase_13 | AC | 223 ms
92,544 KB |
testcase_14 | AC | 373 ms
101,308 KB |
testcase_15 | AC | 491 ms
91,348 KB |
testcase_16 | AC | 536 ms
92,628 KB |
testcase_17 | AC | 464 ms
88,272 KB |
testcase_18 | AC | 466 ms
88,212 KB |
testcase_19 | AC | 506 ms
92,372 KB |
testcase_20 | AC | 517 ms
92,068 KB |
testcase_21 | AC | 503 ms
93,288 KB |
testcase_22 | AC | 463 ms
91,256 KB |
testcase_23 | AC | 476 ms
91,944 KB |
testcase_24 | AC | 494 ms
87,352 KB |
testcase_25 | AC | 468 ms
93,280 KB |
testcase_26 | AC | 461 ms
88,196 KB |
testcase_27 | AC | 514 ms
88,312 KB |
testcase_28 | AC | 431 ms
91,204 KB |
testcase_29 | AC | 415 ms
87,192 KB |
testcase_30 | AC | 438 ms
87,168 KB |
testcase_31 | AC | 551 ms
92,592 KB |
testcase_32 | AC | 504 ms
91,516 KB |
testcase_33 | AC | 510 ms
92,284 KB |
ソースコード
import math class SegTree2D: DEFAULT = { 'min': 1 << 60, 'max': -(1 << 60), 'sum': 0, 'prd': 1, 'gcd': 0, 'lmc': 1, '^': 0, '&': (1 << 60) - 1, '|': 0, } FUNC = { 'min': min, 'max': max, 'sum': (lambda x, y: x + y), 'prd': (lambda x, y: (x * y)%(10**9+7)), 'gcd': math.gcd, 'lmc': (lambda x, y: (x * y) // math.gcd(x, y)), '^': (lambda x, y: x ^ y), '&': (lambda x, y: x & y), '|': (lambda x, y: x | y), } def __init__(self,ls2D, mode='min', func=None, default=None): """ 要素ls2D, 関数mode (min,max,sum,prd(product),gcd,lmc,^,&,|) func,defaultを指定すれば任意の関数、単位元での計算が可能 """ N = len(ls2D) M = len(ls2D[0]) if default == None: self.default = self.DEFAULT[mode] else: self.default = default if func == None: self.func = self.FUNC[mode] else: self.func = func self.N = N self.M = M self.KN = (N - 1).bit_length() self.KM = (M - 1).bit_length() self.N2 = 1 << self.KN self.M2 = 1 << self.KM self.dat = [[self.default] * (2**(self.KM + 1)) for i in range(2**(self.KN + 1))] for i in range(self.N): for j in range(self.M): self.dat[self.N2 + i][self.M2 + j] = ls2D[i][j] self.build() def build(self): for j in range(self.M): for i in range(self.N2 - 1, 0, -1): self.dat[i][self.M2 + j] = self.func(self.dat[i << 1][self.M2 + j], self.dat[i << 1 | 1][self.M2 + j]) for i in range(2**(self.KN + 1)): for j in range(self.M2 - 1, 0, -1): self.dat[i][j] = self.func(self.dat[i][j << 1], self.dat[i][j << 1 | 1]) def leafvalue(self, x,y): # (x,y)番目の値の取得 return self.dat[x + self.N2][y + self.M2] def update(self, x, y, value): # (x,y)の値をvalueに変える i = x + self.N2 j = y + self.M2 self.dat[i][j] = value while j > 1: j >>= 1 self.dat[i][j] = self.func(self.dat[i][j << 1], self.dat[i][j << 1 | 1]) j = y + self.M2 while i > 1: i >>= 1 self.dat[i][j] = self.func(self.dat[i << 1][j], self.dat[i << 1 | 1][j]) while j > 1: j >>= 1 self.dat[i][j] = self.func(self.dat[i][j << 1], self.dat[i][j << 1 | 1]) j = y + self.M2 return def query(self, Lx, Rx, Ly, Ry): # [Lx,Rx)×[Ly,Ry)の区間取得 Lx += self.N2 Rx += self.N2 Ly += self.M2 Ry += self.M2 vLx = self.default vRx = self.default while Lx < Rx: if Lx & 1: vLy = self.default vRy = self.default Ly1 = Ly Ry1 = Ry while Ly1 < Ry1: if Ly1 & 1: vLy = self.func(vLy, self.dat[Lx][Ly1]) Ly1 += 1 if Ry1 & 1: Ry1 -= 1 vRy = self.func(self.dat[Lx][Ry1], vRy) Ly1 >>= 1 Ry1 >>= 1 vy = self.func(vLy, vRy) vLx = self.func(vLx,vy) Lx += 1 if Rx & 1: Rx -= 1 vLy = self.default vRy = self.default Ly1 = Ly Ry1 = Ry while Ly1 < Ry1: if Ly1 & 1: vLy = self.func(vLy, self.dat[Rx][Ly1]) Ly1 += 1 if Ry1 & 1: Ry1 -= 1 vRy = self.func(self.dat[Rx][Ry1], vRy) Ly1 >>= 1 Ry1 >>= 1 vy = self.func(vLy, vRy) vRx = self.func(vy, vRx) Lx >>= 1 Rx >>= 1 return self.func(vLx, vRx) H,W = map(int,input().split()) lsA = [list(map(int,input().split())) for i in range(H)] sg2 = SegTree2D(lsA,mode='prd') Q = int(input()) mod = 10**9+7 lsans = [] #print(sg2.dat) for i in range(Q): r,c = map(int,input().split()) a,b,cc,d = 1,1,1,1 if r>1 and c>1: a = sg2.query(0, r-1, 0, c-1) if r>1 and c<W: b = sg2.query(0, r-1, c, W) if r<H and c<W: cc = sg2.query(r, H , c, W) if r<H and c>1: d = sg2.query(r, H, 0, c-1) ans = (a*b*cc*d)%mod lsans.append(ans) print(*lsans,sep='\n')