結果

問題 No.325 マンハッタン距離2
ユーザー t98slidert98slider
提出日時 2023-03-14 21:03:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,491 bytes
コンパイル時間 1,865 ms
コンパイル使用メモリ 180,644 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-18 12:03:02
合計ジャッジ時間 2,791 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

vector<pair<int,int>> Totsuho(vector<pair<int,int>> &pos){
    int n=pos.size(),n1,n2;
    sort(pos.begin(), pos.end());
    if(pos.empty()) return pos;
    vector<pair<int,int>> res1={pos[0],pos[1]},res2={pos[0],pos[1]};
    auto cross=[&](pair<int,int> a,pair<int,int> b){
        return (long long)a.first*b.second-(long long)a.second*b.first;
    };
    auto sub=[&](pair<int,int> a,pair<int,int> b){
        return make_pair(a.first-b.first,a.second-b.second);
    };
    for(int i=2;i<n;i++){
        n1=res1.size(),n2=res2.size();
        while(n1>=2&&cross(sub(res1[n1-1],res1[n1-2]),sub(pos[i],res1[n1-1]))<=0){
            res1.pop_back();
            n1--;
        }
        while(n2>=2&&cross(sub(res2[n2-1],res2[n2-2]),sub(pos[i],res2[n2-1]))>=0){
            res2.pop_back();
            n2--;
        }
        res1.push_back(pos[i]);
        res2.push_back(pos[i]);
    }
    for (int i=res2.size()-2;i>=1;i--)res1.push_back(res2[i]);
    return res1;
}

long long Area(vector<pair<int,int>> &tot){
    int n = tot.size();
    long long res = 0,xa,xb,ya,yb,dx,dy;
    for(int i=0;i<n;i++){
        tie(xa,ya)=tot[i];
        tie(xb,yb)=tot[(i+1)%n];
        res+=(xb-xa)*(yb+ya);
    }
    return abs(res);
}

long long count_edgepoint(vector<pair<int,int>> &tot){
    int n = tot.size();
    long long res = n, xa, xb, ya, yb, dx, dy;
    for(int i=0;i<n;i++){
        tie(xa, ya) = tot[i];
        tie(xb, yb) = tot[(i + 1) % n];
        dx = abs(xa - xb);
        dy = abs(ya - yb);
        res += __gcd(dx, dy) - 1;
    }
    return res;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll x1, y1, x2, y2, d;
    cin >> x1 >> y1 >> x2 >> y2 >> d;
    vector<pair<int,int>> c = {
        {x1, y1},
        {x1, y2},
        {x2, y1},
        {x2, y2},
        {d, 0},
        {-d, 0},
        {0, d},
        {0, -d},
        {x1, d - abs(x1)},
        {x1, -(d - abs(x1))},
        {x2, d - abs(x2)},
        {x2, -(d - abs(x2))},
        {d - abs(y1), y1},
        {-(d - abs(y1)), y1},
        {d - abs(y2), y2},
        {-(d - abs(y2)), y2}
    };
    vector<pair<int,int>> pos;
    auto f = [&](int x, int y){
        if(abs(x) + abs(y) <= d && x1 <= x && x <= x2 && y1 <= y && y <= y2){
            pos.emplace_back(x, y);
        }
    };
    for(auto &&p : c) f(p.first, p.second);
    auto tot = Totsuho(pos);
    cout << (Area(tot) + count_edgepoint(tot) + 2) / 2 << '\n';
}
0