結果

問題 No.1141 田グリッド
ユーザー terasaterasa
提出日時 2022-05-30 20:40:55
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 416 ms / 2,000 ms
コード長 2,303 bytes
コンパイル時間 156 ms
コンパイル使用メモリ 81,660 KB
実行使用メモリ 98,168 KB
最終ジャッジ日時 2023-10-21 00:24:07
合計ジャッジ時間 7,494 ms
ジャッジサーバーID
(参考情報)
judge10 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
55,672 KB
testcase_01 AC 45 ms
55,672 KB
testcase_02 AC 44 ms
55,672 KB
testcase_03 AC 45 ms
55,672 KB
testcase_04 AC 44 ms
55,672 KB
testcase_05 AC 43 ms
55,672 KB
testcase_06 AC 44 ms
55,672 KB
testcase_07 AC 45 ms
55,672 KB
testcase_08 AC 71 ms
72,964 KB
testcase_09 AC 66 ms
70,828 KB
testcase_10 AC 71 ms
73,072 KB
testcase_11 AC 66 ms
70,828 KB
testcase_12 AC 86 ms
76,948 KB
testcase_13 AC 274 ms
89,484 KB
testcase_14 AC 416 ms
98,168 KB
testcase_15 AC 275 ms
81,060 KB
testcase_16 AC 283 ms
80,952 KB
testcase_17 AC 286 ms
81,168 KB
testcase_18 AC 192 ms
83,104 KB
testcase_19 AC 193 ms
82,908 KB
testcase_20 AC 193 ms
82,928 KB
testcase_21 AC 277 ms
80,996 KB
testcase_22 AC 274 ms
81,016 KB
testcase_23 AC 276 ms
81,000 KB
testcase_24 AC 196 ms
82,944 KB
testcase_25 AC 195 ms
83,120 KB
testcase_26 AC 203 ms
83,268 KB
testcase_27 AC 204 ms
83,188 KB
testcase_28 AC 190 ms
82,492 KB
testcase_29 AC 200 ms
82,924 KB
testcase_30 AC 195 ms
83,144 KB
testcase_31 AC 191 ms
82,808 KB
testcase_32 AC 189 ms
82,936 KB
testcase_33 AC 189 ms
82,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import pypyjit
import itertools
import heapq
import math
from collections import deque, defaultdict
import bisect

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
pypyjit.set_param('max_unroll_recursion=-1')


def index_lt(a, x):
    'return largest index s.t. A[i] < x or -1 if it does not exist'
    return bisect.bisect_left(a, x) - 1


def index_le(a, x):
    'return largest index s.t. A[i] <= x or -1 if it does not exist'
    return bisect.bisect_right(a, x) - 1


def index_gt(a, x):
    'return smallest index s.t. A[i] > x or len(a) if it does not exist'
    return bisect.bisect_right(a, x)


def index_ge(a, x):
    'return smallest index s.t. A[i] >= x or len(a) if it does not exist'
    return bisect.bisect_left(a, x)


class CumulativeSum2D:
    def __init__(self, A):
        self.A = A
        self.H = len(A)
        self.W = len(A[0])
        self.S = [[0 for _ in range(self.W + 1)] for _ in range(self.H + 1)]

        for i in range(self.H):
            for j in range(self.W):
                self.S[i + 1][j + 1] = self.S[i + 1][j] + self.S[i][j + 1] - self.S[i][j] + self.A[i][j]

    def sum(self, lx, ly, rx, ry):
        if lx > rx or ly > ry:
            return 0
        return self.S[rx + 1][ry + 1] - self.S[lx][ry + 1] - self.S[rx + 1][ly] + self.S[lx][ly]


H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
mod = 10 ** 9 + 7

is_zero = [[1 if A[i][j] == 0 else 0 for j in range(W)] for i in range(H)]
A = [[A[i][j] if A[i][j] > 0 else 1 for j in range(W)] for i in range(H)]

R = []
for i in range(H):
    acc = 1
    for j in range(W):
        acc *= A[i][j]
        acc %= mod
    R.append(acc)
C = []
for j in range(W):
    acc = 1
    for i in range(H):
        acc *= A[i][j]
        acc %= mod
    C.append(acc)
S = 1
for i in range(H):
    S *= R[i]
    S %= mod

CZ = CumulativeSum2D(is_zero)

Q = int(input())
for _ in range(Q):
    r, c = map(lambda x: int(x) - 1, input().split())
    zero = 0
    zero += CZ.sum(0, 0, r - 1, c - 1)
    zero += CZ.sum(0, c + 1, r - 1, W - 1)
    zero += CZ.sum(r + 1, 0, H - 1, c - 1)
    zero += CZ.sum(r + 1, c + 1, H - 1, W - 1)
    if zero > 0:
        print(0)
    else:
        print(S * pow(R[r], mod - 2, mod) * pow(C[c], mod - 2, mod) * A[r][c] % mod)
0