結果

問題 No.440 2次元チワワ問題
ユーザー maspymaspy
提出日時 2020-06-05 19:22:18
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,227 ms / 5,000 ms
コード長 1,438 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 10,992 KB
実行使用メモリ 43,008 KB
最終ジャッジ日時 2023-08-22 10:44:27
合計ジャッジ時間 21,943 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 138 ms
29,900 KB
testcase_01 AC 141 ms
29,940 KB
testcase_02 AC 140 ms
29,972 KB
testcase_03 AC 143 ms
29,956 KB
testcase_04 AC 147 ms
30,004 KB
testcase_05 AC 141 ms
30,056 KB
testcase_06 AC 142 ms
29,876 KB
testcase_07 AC 393 ms
32,476 KB
testcase_08 AC 989 ms
33,080 KB
testcase_09 AC 1,068 ms
36,776 KB
testcase_10 AC 321 ms
31,988 KB
testcase_11 AC 708 ms
31,956 KB
testcase_12 AC 360 ms
33,808 KB
testcase_13 AC 929 ms
37,348 KB
testcase_14 AC 272 ms
30,284 KB
testcase_15 AC 815 ms
40,068 KB
testcase_16 AC 867 ms
31,456 KB
testcase_17 AC 1,141 ms
43,008 KB
testcase_18 AC 1,168 ms
42,920 KB
testcase_19 AC 1,117 ms
42,900 KB
testcase_20 AC 1,156 ms
42,904 KB
testcase_21 AC 1,217 ms
41,068 KB
testcase_22 AC 1,183 ms
40,996 KB
testcase_23 AC 1,188 ms
41,172 KB
testcase_24 AC 1,227 ms
41,020 KB
testcase_25 AC 1,177 ms
41,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

H, W = map(int, readline().split())
A = np.frombuffer(read(H * (W + 1)), 'S1').reshape(H, -1)[:, :W]
Q = int(readline())
nums = np.array(read().split(), np.int32) - 1
X1 = nums[::4]
Y1 = nums[1::4]
X2 = nums[2::4]
Y2 = nums[3::4]

def solve(A, X1, Y1, X2, Y2):
    # 上から下に向かっての cww を数える
    H, W = A.shape
    c = np.zeros((H + 1, W), np.int64)
    w = np.zeros((H + 1, W), np.int64)
    c[1:] = (A == b'c').cumsum(axis=0)
    w[1:] = (A == b'w').cumsum(axis=0)
    cw = np.zeros_like(c)
    ww = np.zeros_like(c)
    cww = np.zeros_like(c)
    for n in range(1, len(A)):
        row = A[n]
        is_w = row == b'w'
        cw[n + 1] = cw[n] + is_w * c[n]
        ww[n + 1] = w[n] + is_w * w[n]
        cww[n + 1] = cww[n] + is_w * cw[n]
    for x1, x2, y1, y2 in zip(X1, X2, Y1, Y2):
        y = slice(y1, y2 + 1, 1)
        x = cww[x2 + 1, y] - cww[x1, y]
        n = w[x2 + 1, y] - w[x1, y]
        x -= c[x1, y] * (n * (n - 1) // 2)
        x -= cw[x1, y] * (w[x2 + 1, y] - w[x1, y])
        yield x.sum()

answer = [0] * Q
for _ in range(4):
    for i, x in enumerate(solve(A, X1, Y1, X2, Y2)):
        answer[i] += x
    A = A.T[::-1]
    H, W = A.shape
    X1, Y1 = H - 1 - Y1, X1
    X2, Y2 = H - 1 - Y2, X2
    X1, X2 = X2, X1

print('\n'.join(map(str, answer)))
0