結果

問題 No.325 マンハッタン距離2
ユーザー h_nosonh_noson
提出日時 2016-06-08 18:24:53
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,270 bytes
コンパイル時間 493 ms
コンパイル使用メモリ 65,184 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-17 09:51:15
合計ジャッジ時間 1,555 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;

#define REP(i,s,e) for (i = s; i <= e; i++)
#define rep(i,n) REP (i,0,(int)(n)-1)
#define RREP(i,s,e) for (i = s; i >= e; i--)
#define rrep(i,n) RREP (i,(int)(n)-1,0)
#define INF (int)1e8
#define MOD (int)(1e9+7)

typedef long long ll;

int d;

ll sum(int x) {
    return (ll) x * (x + 1) / 2;
}

ll f(int x, int y) {
    if (x >= d && y >= d)
        return sum(d+1);
    else if (y >= d)
        return sum(d+1) - sum(d-x);
    else if (x >= d)
        return sum(d+1) - sum(d-y);
    else
        return (ll) (x+1) * (y+1) - ((x + y > d ) ? sum(x + y - d) : 0);
}

ll g(int x1, int y1, int x2, int y2) {
    if (x2 < 0)
        return g(-x2,y1,-x1,y2);
    if (y2 < 0)
        return g(x1,-y2,x2,-y1);
    if (x1 < 0)
        return g(0,y1,x2,y2) + g(0,y1,-x1,y2) - g(0,y1,0,y2);
    if (y1 < 0)
        return g(x1,0,x2,y2) + g(x1,0,x2,-y1) - g(x1,0,x2,0);
    ll ret = f(x2,y2);
    if (x1 > 0)
        ret -= f(x1-1,y2);
    if (y1 > 0)
        ret -= f(x2,y1-1);
    if (x1 > 0 && y1 > 0)
        ret += f(x1-1,y1-1);
    return ret;
}

int main(void) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2 >> d;
    cout << g(x1,y1,x2,y2) << endl;
    return 0;
}
0