結果

問題 No.325 マンハッタン距離2
ユーザー maspymaspy
提出日時 2020-03-04 15:56:54
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 34 ms / 1,000 ms
コード長 1,027 bytes
コンパイル時間 81 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-22 01:38:46
合計ジャッジ時間 1,946 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
10,752 KB
testcase_01 AC 34 ms
10,752 KB
testcase_02 AC 31 ms
10,752 KB
testcase_03 AC 32 ms
11,008 KB
testcase_04 AC 31 ms
10,752 KB
testcase_05 AC 31 ms
10,752 KB
testcase_06 AC 31 ms
10,752 KB
testcase_07 AC 30 ms
10,752 KB
testcase_08 AC 30 ms
10,624 KB
testcase_09 AC 31 ms
10,880 KB
testcase_10 AC 30 ms
10,880 KB
testcase_11 AC 30 ms
10,752 KB
testcase_12 AC 30 ms
10,752 KB
testcase_13 AC 32 ms
10,752 KB
testcase_14 AC 30 ms
10,752 KB
testcase_15 AC 31 ms
10,880 KB
testcase_16 AC 31 ms
10,752 KB
testcase_17 AC 31 ms
10,752 KB
testcase_18 AC 31 ms
10,624 KB
testcase_19 AC 31 ms
10,752 KB
testcase_20 AC 30 ms
10,880 KB
testcase_21 AC 31 ms
10,752 KB
testcase_22 AC 31 ms
10,880 KB
testcase_23 AC 30 ms
10,624 KB
testcase_24 AC 30 ms
10,752 KB
testcase_25 AC 30 ms
10,880 KB
testcase_26 AC 31 ms
10,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
# %%
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


# %%
x1, y1, x2, y2, D = map(int, read().split())


# %%
def range_sum(a, b):
    def f(x):
        return x * (x + 1) // 2
    return f(b - 1) - f(a - 1)


# %%
def f_RU(x, y, D):
    if D < 0:
        return 0
    if x == -1 or y == -1:
        return 0
    if x < y:
        x, y = y, x
    if x >= D and y >= D:
        return range_sum(1, D + 2)
    if x >= D and y < D:
        return range_sum(D - y + 1, D + 2)
    if x + y > D:
        return (x + 1) * (D - x + 1) + range_sum(D - y + 1, x + 1)
    return (x + 1) * (y + 1)


def f(x, y, D):
    if x < y:
        x, y = y, x
    if x >= 0 and y >= 0:
        return f_RU(x, y, D)
    if x < 0 and y < 0:
        return f_RU(-2 - x, -2 - y, D - 2)
    if x >= 0 and y < 0:
        return -f_RU(x, -2 - y, D - 1)


# %%
x = 0
x += f(x2, y2, D)
x += f(x1 - 1, y1 - 1, D)
x -= f(x1 - 1, y2, D)
x -= f(x2, y1 - 1, D)
print(x)
0