結果

問題 No.1141 田グリッド
ユーザー kohei2019kohei2019
提出日時 2022-07-20 22:19:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 662 ms / 2,000 ms
コード長 4,720 bytes
コンパイル時間 295 ms
コンパイル使用メモリ 87,172 KB
実行使用メモリ 102,764 KB
最終ジャッジ日時 2023-09-15 10:09:15
合計ジャッジ時間 15,252 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 75 ms
71,080 KB
testcase_01 AC 72 ms
71,076 KB
testcase_02 AC 72 ms
71,172 KB
testcase_03 AC 72 ms
71,368 KB
testcase_04 AC 71 ms
71,060 KB
testcase_05 AC 72 ms
71,028 KB
testcase_06 AC 72 ms
71,132 KB
testcase_07 AC 72 ms
71,408 KB
testcase_08 AC 121 ms
78,148 KB
testcase_09 AC 107 ms
77,472 KB
testcase_10 AC 125 ms
78,164 KB
testcase_11 AC 119 ms
78,500 KB
testcase_12 AC 125 ms
78,336 KB
testcase_13 AC 272 ms
92,844 KB
testcase_14 AC 451 ms
102,764 KB
testcase_15 AC 579 ms
95,116 KB
testcase_16 AC 633 ms
93,120 KB
testcase_17 AC 562 ms
91,660 KB
testcase_18 AC 550 ms
87,908 KB
testcase_19 AC 579 ms
92,608 KB
testcase_20 AC 608 ms
94,492 KB
testcase_21 AC 607 ms
94,936 KB
testcase_22 AC 562 ms
94,604 KB
testcase_23 AC 603 ms
95,184 KB
testcase_24 AC 589 ms
90,056 KB
testcase_25 AC 595 ms
94,104 KB
testcase_26 AC 568 ms
90,104 KB
testcase_27 AC 636 ms
90,372 KB
testcase_28 AC 530 ms
93,484 KB
testcase_29 AC 516 ms
88,364 KB
testcase_30 AC 544 ms
88,040 KB
testcase_31 AC 662 ms
96,484 KB
testcase_32 AC 596 ms
93,756 KB
testcase_33 AC 599 ms
93,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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')
0