CSES - Aalto Competitive Programming 2024 - wk10 - Homework - Results
Submission details
Task:Line Segment Intersection
Sender:Rasse
Submission time:2024-11-08 09:23:25 +0200
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.21 sdetails
#2ACCEPTED0.22 sdetails
#3ACCEPTED0.24 sdetails
#4ACCEPTED0.26 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails

Code

#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <climits>
#include <cmath>
#include <functional>
#include <type_traits>
#include <fstream>
#include <bitset>
#include <complex>
 
#define int long long
using namespace std;
 
//#define cross(x, y) ((x).real()*(y).imag()-(x).imag()*(y).real()) 
#define cross(x, y) (((x)*conj(y)).imag())
#define sign(v) ((0 < (v)) - ((v) < 0))

void solve()
{
    int x1, y1, x2, y2, x3, y3, x4, y4;
    cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;

    complex<int> v1 = {x1, y1};
    complex<int> v2 = {x2, y2};
    complex<int> v3 = {x3, y3};
    complex<int> v4 = {x4, y4};

    // Case: end point
    if (v1 == v3 || v2 == v3 || v1 == v4 || v2 == v4)
    {
        cout << "YES\n";
        return;
    }

    // Case: The same line
    if (cross(v2-v1, v3-v2) == 0 && cross(v3-v2, v4-v3) == 0)
    {
        vector<complex<int>> ps = {v1, v2, v3, v4};
        sort(ps.begin(), ps.end(), [](auto v1, auto v2){ if (v1.real() == v2.real()) return v1.imag() < v2.imag(); else return v1.real() < v2.real(); });
        if ((ps[0] == v1 || ps[0] == v2) == (ps[1] == v1 || ps[1] == v2))
        {
            cout << "NO\n";
            return;
        }
        else
        {
            cout << "YES\n";
            return;
        }
    }


    // Other on the left side and other on the right side
    int r1 = sign(cross(v2-v1, v3-v1));
    int r2 = sign(cross(v2-v1, v4-v1));
    int r3 = sign(cross(v4-v3, v1-v3));
    int r4 = sign(cross(v4-v3, v2-v3));
    if (r1 != r2 && r3 != r4)
    {
        cout << "YES\n";
        return;
    }

    cout << "NO\n";
}
 
 
signed main()
{
    ios::sync_with_stdio(0);
    //cin.tie(0);
 
    int t = 1;
 
    cin >> t;
    //ifstream f("testcase.txt");
    //cin.rdbuf(f.rdbuf());
 
    for (int i = 0; i < t; i++)
        solve();
}

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