:heavy_check_mark: test/Geometry/integer_line/aoj-CGL_2_A.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A

#include "Geometry/integer_line.hpp"
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int q;
    cin >> q;

    while (q--)
    {
        long long x0, y0, x1, y1;
        long long x2, y2, x3, y3;
        cin >> x0 >> y0 >> x1 >> y1;
        cin >> x2 >> y2 >> x3 >> y3;

        const IntegerLine::Point p0 = {x0, y0};
        const IntegerLine::Point p1 = {x1, y1};
        const IntegerLine::Point p2 = {x2, y2};
        const IntegerLine::Point p3 = {x3, y3};

        const IntegerLine line1 = IntegerLine::through(p0, p1);
        const IntegerLine line2 = IntegerLine::through(p2, p3);

        if (line1.is_parallel(line2))
        {
            cout << 2 << endl;
        }
        else if (line1.is_perpendicular(line2))
        {
            cout << 1 << endl;
        }
        else
        {
            cout << 0 << endl;
        }
    }

    return 0;
}
#line 1 "test/Geometry/integer_line/aoj-CGL_2_A.cpp"
// competitive-verifier: PROBLEM https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=CGL_2_A

#line 1 "Geometry/integer_line.hpp"
#include <bits/stdc++.h>
using namespace std;

struct IntegerLine
{
    using Point = std::pair<__int128_t, __int128_t>;
    using Intersection = std::array<__int128_t, 3>;

    // A*x + B*y + C = 0
    __int128_t A, B, C;

    IntegerLine(__int128_t A, __int128_t B, __int128_t C) : A(A), B(B), C(C)
    {
        assert(A != 0 || B != 0);
        normalize();
    }

    // 異なる2点を通る直線
    static IntegerLine through(Point p, Point q)
    {
        const auto [px, py] = p;
        const auto [qx, qy] = q;

        assert(p != q);

        return IntegerLine(py - qy, qx - px, px * qy - qx * py);
    }

    // 異なる2点の垂直二等分線
    static IntegerLine perpendicular_bisector(Point p, Point q)
    {
        const auto [px, py] = p;
        const auto [qx, qy] = q;

        assert(p != q);

        return IntegerLine(2 * (qx - px), 2 * (qy - py), px * px + py * py - qx * qx - qy * qy);
    }

    // 点を直線方程式の左辺へ代入した値
    __int128_t eval(Point p) const
    {
        const auto [x, y] = p;
        return A * x + B * y + C;
    }

    // 点が直線上にあるか
    bool contains(Point p) const
    {
        return eval(p) == 0;
    }

    // 2点が直線の同じ側にあるか
    // どちらかが直線上にある場合は false
    bool same_side(Point p, Point q) const
    {
        const __int128_t pValue = eval(p);
        const __int128_t qValue = eval(q);

        return (pValue > 0 && qValue > 0) || (pValue < 0 && qValue < 0);
    }

    // 2点が直線の反対側にあるか
    // どちらかが直線上にある場合は false
    bool opposite_side(Point p, Point q) const
    {
        const __int128_t pValue = eval(p);
        const __int128_t qValue = eval(q);

        return (pValue > 0 && qValue < 0) || (pValue < 0 && qValue > 0);
    }

    // 2直線が平行か
    bool is_parallel(const IntegerLine &other) const
    {
        return A * other.B == B * other.A;
    }

    // 2直線が同一か
    bool is_same(const IntegerLine &other) const
    {
        return A == other.A && B == other.B && C == other.C;
    }

    // 2直線が垂直か
    bool is_perpendicular(const IntegerLine &other) const
    {
        return A * other.A + B * other.B == 0;
    }

    // 交点を {xの分子, yの分子, 共通分母} で返す
    // 平行または同一直線の場合は std::nullopt
    std::optional<Intersection> intersection(const IntegerLine &other) const
    {
        __int128_t denominator = A * other.B - other.A * B;

        if (denominator == 0)
        {
            return std::nullopt;
        }

        __int128_t xNumerator = B * other.C - other.B * C;

        __int128_t yNumerator = C * other.A - other.C * A;

        if (denominator < 0)
        {
            denominator = -denominator;
            xNumerator = -xNumerator;
            yNumerator = -yNumerator;
        }

        return Intersection{xNumerator, yNumerator, denominator};
    }

private:
    static __int128_t abs_value(__int128_t value)
    {
        return value < 0 ? -value : value;
    }

    static __int128_t gcd(__int128_t a, __int128_t b)
    {
        a = abs_value(a);
        b = abs_value(b);

        while (b != 0)
        {
            const __int128_t remainder = a % b;
            a = b;
            b = remainder;
        }

        return a;
    }

    void normalize()
    {
        const __int128_t divisor = gcd(gcd(A, B), C);

        A /= divisor;
        B /= divisor;
        C /= divisor;

        // 最初の非ゼロ係数を正に統一する。
        // A と B が同時に 0 になることはない。
        if (A < 0 || (A == 0 && B < 0))
        {
            A = -A;
            B = -B;
            C = -C;
        }
    }
};
#line 5 "test/Geometry/integer_line/aoj-CGL_2_A.cpp"

using namespace std;

int main()
{
    int q;
    cin >> q;

    while (q--)
    {
        long long x0, y0, x1, y1;
        long long x2, y2, x3, y3;
        cin >> x0 >> y0 >> x1 >> y1;
        cin >> x2 >> y2 >> x3 >> y3;

        const IntegerLine::Point p0 = {x0, y0};
        const IntegerLine::Point p1 = {x1, y1};
        const IntegerLine::Point p2 = {x2, y2};
        const IntegerLine::Point p3 = {x3, y3};

        const IntegerLine line1 = IntegerLine::through(p0, p1);
        const IntegerLine line2 = IntegerLine::through(p2, p3);

        if (line1.is_parallel(line2))
        {
            cout << 2 << endl;
        }
        else if (line1.is_perpendicular(line2))
        {
            cout << 1 << endl;
        }
        else
        {
            cout << 0 << endl;
        }
    }

    return 0;
}

Test cases

Env Name Status Elapsed Memory
g++ 00_small_00 :heavy_check_mark: AC 3 ms 3 MB
g++ 00_small_01 :heavy_check_mark: AC 2 ms 3 MB
g++ 00_small_02 :heavy_check_mark: AC 2 ms 3 MB
g++ 00_small_03 :heavy_check_mark: AC 2 ms 3 MB
g++ 02_horizontal_00 :heavy_check_mark: AC 4 ms 3 MB
g++ 02_horizontal_01 :heavy_check_mark: AC 4 ms 3 MB
g++ 03_vertical_00 :heavy_check_mark: AC 4 ms 3 MB
g++ 03_vertical_01 :heavy_check_mark: AC 4 ms 3 MB
g++ 04_extreme_00 :heavy_check_mark: AC 2 ms 3 MB
g++ 04_extreme_01 :heavy_check_mark: AC 2 ms 3 MB
g++ 05_rand_00 :heavy_check_mark: AC 3 ms 3 MB
g++ 05_rand_01 :heavy_check_mark: AC 3 ms 3 MB
g++ 05_rand_02 :heavy_check_mark: AC 3 ms 3 MB
g++ 05_rand_03 :heavy_check_mark: AC 3 ms 3 MB
g++ 06_rand_00 :heavy_check_mark: AC 6 ms 3 MB
g++ 06_rand_01 :heavy_check_mark: AC 6 ms 3 MB
g++ 06_rand_02 :heavy_check_mark: AC 6 ms 3 MB
g++ 06_rand_03 :heavy_check_mark: AC 6 ms 3 MB
g++ 06_rand_04 :heavy_check_mark: AC 6 ms 3 MB
g++ 06_rand_05 :heavy_check_mark: AC 6 ms 3 MB
g++ 06_rand_06 :heavy_check_mark: AC 6 ms 3 MB
g++ 06_rand_07 :heavy_check_mark: AC 6 ms 3 MB
g++ 06_rand_08 :heavy_check_mark: AC 6 ms 3 MB
g++ 06_rand_09 :heavy_check_mark: AC 6 ms 3 MB
g++ 06_rand_10 :heavy_check_mark: AC 6 ms 3 MB
g++ 06_rand_11 :heavy_check_mark: AC 6 ms 3 MB
g++ 06_rand_12 :heavy_check_mark: AC 6 ms 3 MB
g++ 06_rand_13 :heavy_check_mark: AC 6 ms 3 MB
g++ 07_rand_00 :heavy_check_mark: AC 6 ms 3 MB
g++ 07_rand_01 :heavy_check_mark: AC 6 ms 3 MB
g++ 07_rand_02 :heavy_check_mark: AC 6 ms 3 MB
g++ 07_rand_03 :heavy_check_mark: AC 7 ms 3 MB
Back to top page

This site uses Just the Docs, a documentation theme for Jekyll.