CSES - Aalto Competitive Programming 2024 - wk10 - Homework - Results
Submission details
Task:Line Segment Intersection
Sender:ZDHKLV
Submission time:2024-11-08 13:02:51 +0200
Language:C++ (C++11)
Status:READY
Result:
Test results
testverdicttime
#10.30 sdetails
#20.35 sdetails
#3ACCEPTED0.44 sdetails
#4ACCEPTED0.52 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails

Code

#include <bits/stdc++.h>

using namespace std;

struct equation_t {
    bool is_vertical;
    float a, b;
};

equation_t equation(float x1, float y1, float x2, float y2) {

    // y1 = a x1 + b
    // y2 = a x2 + b

    // y1 - y2 = a (x1 - x2)

    // y1 x2 = a x1 x2 + b x2
    // y2 x1 = a x1 x2 + b x1

    // y1 x2 - y2 x1 = b (x2 - x1)

    if (x1 == x2) {
        return { true, 0, 0 };
    } else {
        float a = (y1 - y2) / (x1 - x2);
        float b = (y1 * x2 - y2 * x1) / (x2 - x1);
        return {false, a, b};
    }

}

int main() {

    int t;
    cin >> t;

    vector<int> inputs(8*t);
    for (int i = 0; i < 8*t; i++)
        cin >> inputs[i];
    
    for (int i = 0; i < t; i++) {

        float x1 = (float) inputs[8*i+0], y1 = (float) inputs[8*i+1];
        float x2 = (float) inputs[8*i+2], y2 = (float) inputs[8*i+3];
        float x3 = (float) inputs[8*i+4], y3 = (float) inputs[8*i+5];
        float x4 = (float) inputs[8*i+6], y4 = (float) inputs[8*i+7];

        int ix1 = inputs[8*i+0], iy1 = inputs[8*i+1];
        int ix2 = inputs[8*i+2], iy2 = inputs[8*i+3];
        int ix3 = inputs[8*i+4], iy3 = inputs[8*i+5];
        int ix4 = inputs[8*i+6], iy4 = inputs[8*i+7];

        equation_t l12 = equation(x1, y1, x2, y2);
        equation_t l34 = equation(x3, y3, x4, y4);

        if (l12.is_vertical && l34.is_vertical) {

            if (ix1 == ix3 && ((iy1 >= min(iy3, iy4) && iy1 <= max(iy3, iy4)) || (iy2 >= min(iy3, iy4) && iy2 <= max(iy3, iy4)))) {
                cout << "YES" << endl;
            } else {
                cout << "NO" << endl;
            }

        } else if (l12.is_vertical) {

            float x = x1;
            float y = l34.a * x + l34.b;

            int iy = (int) round(y), ix = (int) round(x);

            if (iy >= min(iy1, iy2) && iy <= max(iy1, iy2) && ix >= min(ix3, ix4) && ix <= max(ix3, ix4)) {
                cout << "YES" << endl;
            } else {
                cout << "NO" << endl;
            }

        } else if (l34.is_vertical) {

            float x = x3;
            float y = l12.a * x + l12.b;

            int iy = (int) round(y), ix = (int) round(x);

            if (iy >= min(iy3, iy4) && iy <= max(iy3, iy4) && ix >= min(ix1, ix2) && ix <= max(ix1, ix2)) {
                cout << "YES" << endl;
            } else {
                cout << "NO" << endl;
            }

        } else {

            // y = a1 x + b1
            // y = a2 x + b2

            // a1 x + b1 = a2 x + b2
            // x (a1 - a2) = (b2 - b1)

            float epsilon = 10e-9;

            if (abs(l12.a - l34.a) <= epsilon) {

                // y = ax + b1
                // y = ax + b2

                if (abs(l12.b - l34.b) <= epsilon) {
                    
                    if ((ix1 >= min(ix3, ix4) && ix1 <= max(ix3, ix4)) || (ix2 >= min(ix3, ix4) && ix2 <= max(ix3, ix4))) {
                        cout << "YES" << endl;
                    } else {
                        cout << "NO" << endl;
                    }

                } else {
                    cout << "NO" << endl;
                }

            } else {

                float x = (l34.b - l12.b) / (l12.a - l34.a);
                float y = l12.a * x + l12.b;

                float ix = (int) round(x), iy = (int) round(y);

                if (iy >= min(iy1, iy2) && iy <= max(iy1, iy2) && iy >= min(iy3, iy4) && iy <= max(iy3, iy4) &&
                    ix >= min(ix1, ix2) && ix <= max(ix1, ix2) && ix >= min(ix3, ix4) && ix <= max(ix3, ix4)) {
                    cout << "YES" << endl;
                } else {
                    cout << "NO" << endl;
                }

            }


        }

    }

}

Test details

Test 1

Verdict:

input
100000
9 7 1 8 8 -5 0 2
10 1 -1 2 -4 1 -7 3
10 2 -8 6 1 2 2 -1
-10 1 9 -7 4 -3 -5 0
...

correct output
NO
NO
NO
NO
NO
...

user output
NO
NO
NO
NO
NO
...

Test 2

Verdict:

input
100000
81 745 -967 768 -451 -490 -454...

correct output
NO
NO
YES
NO
YES
...

user output
NO
NO
YES
NO
YES
...

Test 3

Verdict: ACCEPTED

input
100000
492853 -452834 -657156 -282543...

correct output
YES
YES
NO
YES
YES
...

user output
YES
YES
NO
YES
YES
...

Test 4

Verdict: ACCEPTED

input
100000
788522666 939776556 -831492125...

correct output
NO
NO
NO
NO
NO
...

user output
NO
NO
NO
NO
NO
...

Test 5

Verdict: ACCEPTED

input
1
1 6 6 6 4 4 1000000000 1000000...

correct output
YES

user output
YES

Test 6

Verdict: ACCEPTED

input
1
-1000000000 1000000000 9999999...

correct output
NO

user output
NO