結果

問題 No.325 マンハッタン距離2
ユーザー te-shte-sh
提出日時 2017-06-27 18:52:26
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,308 bytes
コンパイル時間 2,905 ms
コンパイル使用メモリ 86,188 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-03 14:52:06
合計ジャッジ時間 1,820 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;

void main()
{
  auto rd = readln.split.to!(long[]), x1 = rd[0], y1 = rd[1], x2 = rd[2], y2 = rd[3], d = rd[4];

  auto r = 0L;

  if (x2 >= 0 && y2 >= 0)
    r += calc(max(0, x1), max(0, y1), x2, y2, d);
  if (x1 <= 0 && y2 >= 0)
    r += calc(max(0, -x2), max(0, y1), -x1, y2, d);
  if (x1 <= 0 && y1 <= 0)
    r += calc(max(0, -x2), max(0, -y2), -x1, -y1, d);
  if (x2 >= 0 && y1 <= 0)
    r += calc(max(0, x1), max(0, -y2), x2, -y1, d);

  if (x1 <= 0 && x2 >= 0)
    r -= min(y2, d) - max(y1, -d) + 1;
  if (y1 <= 0 && y2 >= 0)
    r -= min(x2, d) - max(x1, -d) + 1;

  if (x1 <= 0 && x2 >= 0 && y1 <= 0 && y2 >= 0)
    --r;

  writeln(r);
}

auto calc(long x1, long y1, long x2, long y2, long d)
{
  if (x1 + y1 > d) return 0;
  if (x2 + y2 <= d) return (x2 - x1 + 1) * (y2 - y1 + 1);
  if (x2 == 0 && y2 == 0) return 1;
  if (x2 == 0) return min(y2, d) - y1 + 1;
  if (y2 == 0) return min(x2, d) - x1 + 1;

  auto r = 0L;

  if (x1 + y2 > d) {
    y2 = d - x1;
  } else if (x1 + y2 < d) {
    r += (d - y2 - x1) * (y2 - y1 + 1);
    x1 = d - y2;
  }

  if (x2 + y1 > d) {
    x2 = d - y1;
  } else if (x2 + y1 < d) {
    r += (d - x2 - y1) * (x2 - x1 + 1);
    y1 = d - x2;
  }

  r += (x2 - x1 + 1) * (x2 - x1 + 2) / 2;
  return r;
}
0