結果
| 問題 | No.325 マンハッタン距離2 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-12-19 19:47:05 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 3,447 bytes |
| 記録 | |
| コンパイル時間 | 1,142 ms |
| コンパイル使用メモリ | 90,104 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-16 16:11:07 |
| 合計ジャッジ時間 | 2,038 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 20 WA * 4 |
ソースコード
#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)における格子点数を求める */
int a = max({0, d - y2, x1});
int b = min({d - 1, d - y1, x2});
int yc = max(1, y1);
int xc = max(0, x1);
if(b >= a){
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 = x2 - xa;
ans -= r * (r + 1) / 2;
}
}
}
else{
if(yc <= y2 && xc <= x2)
ans += (y2 - yc + 1LL) * (x2 - xc + 1LL);
}
/* 長方形を回転させることで、別の象限にも同じ処理を適用できる */
swap(x1, y1);
swap(x2, y2);
x1 *= -1;
x2 *= -1;
swap(x1, x2);
}
cout << ans << endl;
return 0;
}