結果

問題 No.622 点と三角柱の内外判定
ユーザー tottoripapertottoripaper
提出日時 2018-03-16 21:53:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,500 ms
コード長 2,360 bytes
コンパイル時間 2,500 ms
コンパイル使用メモリ 173,072 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-23 12:00:14
合計ジャッジ時間 2,910 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = long long;
using P = std::tuple<int,int>;
using P3 = std::tuple<double, double, double>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

P3 ps[4];

double dot(P3 p, P3 q){
    double x1, y1, z1, x2, y2, z2;
    tie(x1, y1, z1) = p;
    tie(x2, y2, z2) = q;

    return x1 * x2 + y1 * y2 + z1 * z2;
}

P3 cross(P3 p, P3 q){
    double x1, y1, z1, x2, y2, z2;
    tie(x1, y1, z1) = p;
    tie(x2, y2, z2) = q;

    return std::make_tuple(y1 * z2 - y2 * z1, -(x1 * z2 - x2 * z1), x1 * y2 - x2 * y1);
}

double sq_dist(P3 p){
    double x, y, z;
    tie(x, y, z) = p;

    return x * x + y * y + z * z;
}

P3 operator-(const P3 &p, const P3 &q){
    double x1, y1, z1, x2, y2, z2;
    tie(x1, y1, z1) = p;
    tie(x2, y2, z2) = q;

    return std::make_tuple(x1 - x2, y1 - y2, z1 - z2);
}

P3 operator*(const double k, const P3 &p){
    double x, y, z;
    tie(x, y, z) = p;

    return std::make_tuple(k * x, k * y, k * z);
}

double det(double a11, double a21, double a12, double a22){
    return a11 * a22 - a21 * a12;
}

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    for(int i=0;i<4;++i){
        int x, y, z;
        std::cin >> x >> y >> z;

        ps[i] = std::make_tuple(x, y, z);
    }

    for(int i=1;i<4;++i){
        ps[i] = ps[i] - ps[0];
    }

    P3 cr = cross(ps[1], ps[2]);
    double v[3][2], _x[3];
    tie(v[0][0], v[1][0], v[2][0]) = ps[1];
    tie(v[0][1], v[1][1], v[2][1]) = ps[2];
    tie(_x[0], _x[1], _x[2]) = ps[3] - (dot(ps[3], cr) / sq_dist(cr)) * cr;

    double s, t;
    
    if(det(v[0][0], v[1][0], v[0][1], v[1][1]) == 0){
        s = det(_x[0], _x[2], v[0][1], v[2][1]) / det(v[0][0], v[2][0], v[0][1], v[2][1]);
        t = det(v[0][0], v[2][0], _x[0], _x[2]) / det(v[0][0], v[2][0], v[0][1], v[2][1]);
    }else{
        s = det(_x[0], _x[1], v[0][1], v[1][1]) / det(v[0][0], v[1][0], v[0][1], v[1][1]);
        t = det(v[0][0], v[1][0], _x[0], _x[1]) / det(v[0][0], v[1][0], v[0][1], v[1][1]);
    }
    
    const string YESNO[2] = {"NO", "YES"};
    bool isIn = s >= 0 && t >= 0 && s + t <= 1;
    std::cout << YESNO[isIn] << std::endl;
}
0