結果

問題 No.325 マンハッタン距離2
ユーザー mamekinmamekin
提出日時 2015-12-19 19:47:05
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,447 bytes
コンパイル時間 1,104 ms
コンパイル使用メモリ 88,996 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-14 22:34:12
合計ジャッジ時間 2,222 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 WA -
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 WA -
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,352 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 1 ms
4,348 KB
testcase_14 AC 1 ms
4,352 KB
testcase_15 AC 1 ms
4,352 KB
testcase_16 AC 1 ms
4,352 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 AC 1 ms
4,352 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 WA -
testcase_21 AC 1 ms
4,348 KB
testcase_22 AC 1 ms
4,352 KB
testcase_23 WA -
testcase_24 AC 1 ms
4,352 KB
testcase_25 AC 2 ms
4,352 KB
testcase_26 AC 1 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)における格子点数を求める */
        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;
}
0