結果

問題 No.325 マンハッタン距離2
ユーザー mamekinmamekin
提出日時 2015-12-19 20:24:21
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 3,818 bytes
コンパイル時間 1,450 ms
コンパイル使用メモリ 90,340 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-17 12:40:09
合計ジャッジ時間 2,261 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#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;
}
0