CSES - Aalto Competitive Programming 2024 - wk10 - Homework - Results
Submission details
Task:Line Segment Intersection
Sender:laluj
Submission time:2024-11-10 13:34:29 +0200
Language:C++ (C++17)
Status:READY
Result:
Test results
testverdicttime
#1ACCEPTED0.30 sdetails
#2ACCEPTED0.35 sdetails
#30.46 sdetails
#40.53 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.01 sdetails

Code

#include <bits/stdc++.h>

using namespace std;

#define debug(...) Debug(#__VA_ARGS__, __VA_ARGS__);
template <typename... Args>
void Debug(const char* names, Args&&... args) {
    std::cerr << names << " = ";
    ((std::cerr << args << ", "), ...) << "\b\b " << std::endl;
}

#define ll long long
#define ull unsigned long long
#define vi vector<int>

typedef complex<ll> P;
struct segment { P p, q; };
#define X real()
#define Y imag()
ll crossproduct(const P& a, const P& b) { return (conj(a)*b).Y; }

bool onSegment(const P& r, const segment& s) {
    if (r.X <= max(s.p.X, s.q.X) && r.X >= min(s.p.X, s.q.X) &&
        r.Y <= max(s.p.Y, s.q.Y) && r.Y >= min(s.p.Y, s.q.Y))
        return true;
    return false;
}

int orientation(const P& r, const segment& s) {
    int val = crossproduct(r-s.p, r-s.q);
    if (val == 0) return 0;  // r, s.p and s.q are collinear 
    return (val > 0)? 1: 2;  // r at left or right of s
} 

bool do_intersect(const segment& s1, const segment& s2) {
    int o1 = orientation(s2.p, s1), o2 = orientation(s2.q, s1),
        o3 = orientation(s1.p, s2), o4 = orientation(s1.q, s2);

    // General case
    if (o1 != o2 && o3 != o4) return true;

    // Special cases
    if (o1 == 0 && onSegment(s2.p, s1)) return true;
    if (o2 == 0 && onSegment(s2.q, s1)) return true;
    if (o3 == 0 && onSegment(s1.p, s2)) return true;
    if (o4 == 0 && onSegment(s1.q, s2)) return true;

    return false;
}

struct test {
    segment s1, s2;
};

int main() {
    int t;
    ll x1,y1, x2,y2, x3,y3, x4,y4;

    cin >> t;

    vector<test> tests (t);

    for (int i = 0; i < t; i++) {
        cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
        segment s1 = {P{x1,y1}, P{x2,y2}},
                s2 = {P{x3,y3}, P{x4,y4}};

        tests[i] = test{s1,s2};
    }
    
    for (auto& te : tests)
    {
        if (do_intersect(te.s1, te.s2)) cout << "YES" << endl;
        else cout << "NO" << endl;
    }
    
    return 0;
}

Test details

Test 1

Verdict: ACCEPTED

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: ACCEPTED

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:

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

correct output
YES
YES
NO
YES
YES
...

user output
NO
YES
NO
NO
NO
...

Test 4

Verdict:

input
100000
788522666 939776556 -831492125...

correct output
NO
NO
NO
NO
NO
...

user output
YES
YES
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