結果
問題 | No.325 マンハッタン距離2 |
ユーザー |
![]() |
提出日時 | 2020-03-04 15:56:54 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 32 ms / 1,000 ms |
コード長 | 1,027 bytes |
コンパイル時間 | 81 ms |
コンパイル使用メモリ | 12,544 KB |
実行使用メモリ | 10,880 KB |
最終ジャッジ日時 | 2024-10-14 00:15:31 |
合計ジャッジ時間 | 1,771 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 24 |
ソースコード
#!/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)