結果
問題 | No.325 マンハッタン距離2 |
ユーザー |
|
提出日時 | 2015-12-19 20:24:21 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
AC
|
実行時間 | 2 ms / 1,000 ms |
コード長 | 3,818 bytes |
コンパイル時間 | 752 ms |
コンパイル使用メモリ | 89,824 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-17 10:45:33 |
合計ジャッジ時間 | 1,661 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 24 |
ソースコード
#include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include <numeric> #include <limits> #include <climits> #include <cfloat> #include <functional> using namespace std; int main() { int x1, y1, x2, y2, d; cin >> x1 >> y1 >> x2 >> y2 >> d; /* 象限別に処理する */ /* 222211111 222211111 222211111 222211111 2222原4444 333334444 333334444 333334444 333334444 */ long long ans = 0; /* 原点が正方形に含まれるか調べる */ if(y1 <= 0 && 0 <= y2 && x1 <= 0 && 0 <= x2) ++ ans; for(int i=0; i<4; ++i){ /* 第1象限(0<=x, 0<y)における格子点数を求める */ if(0 < y2 && 0 <= x2){ int yc = max(1, y1); int xc = max(0, x1); int yd = max(1, y2); int xd = max(0, x2); if(yd + xd <= d){ ans += (yd - yc + 1LL) * (xd - xc + 1LL); } else if(yc + xc <= d){ int a = max(d - yd, xc); int b = min(d - yc, xd); int ya = d - a; int xa = a; int yb = d - b; int xb = b; if(xc == xa){ if(yc == ya){ /* □□□□□ □□□□□ ■□□□□ ■■□□□ ■■■□□ */ long long p = ya - yc + 1; ans += p * (p + 1) / 2; } else{ /* □□□ □□□ ■□□ ■■□ ■■■ ■■■ */ long long p = xb - xc + 1; ans += p * (p + 1) / 2; long long q = ya - yc + 1 - p; ans += p * q; } } else{ if(yc == yb){ /* ■■□□□□ ■■■□□□ ■■■■□□ */ long long p = ya - yc + 1; ans += p * (p + 1) / 2; long long q = xb - xc + 1 - p; ans += p * q; } else{ /* ■■□□□ ■■■□□ ■■■■□ ■■■■■ ■■■■■ */ long long p = ya - yc + 1; long long q = xb - xc + 1; ans += p * q; long long r = xd - xa; ans -= r * (r + 1) / 2; } } } } /* 長方形を原点中心に回転させることで、別の象限にも同じ処理を適用できる */ swap(x1, y1); swap(x2, y2); x1 *= -1; x2 *= -1; swap(x1, x2); } cout << ans << endl; return 0; }