CSES - Aalto Competitive Programming 2024 - wk10 - Homework - Results
Submission details
Task:Line Segment Intersection
Sender:htoik
Submission time:2024-11-14 17:05:42 +0200
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.07 sdetails
#2ACCEPTED0.06 sdetails
#3ACCEPTED0.07 sdetails
#4ACCEPTED0.08 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails

Compiler report

input/code.cpp: In function 'void readInt128(__int128&)':
input/code.cpp:17:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   17 |     for(; i < s.size(); i++){
      |           ~~^~~~~~~~~~

Code

#include <bits/stdc++.h>
using namespace std;
// typedef long long ll;
typedef __int128_t ll;
typedef unsigned long long ull;

void readInt128(__int128 &n){
    string s;
    cin >> s;
    n = 0;
    bool neg = false;
    int i = 0;
    if(s[0] == '-'){
        neg = true;
        i = 1;
    }
    for(; i < s.size(); i++){
        n = n * 10 + (s[i] - '0');
    }
    if(neg) n = -n;
}

void printInt128(__int128 n){
    if(n == 0){
        cout << '0';
        return;
    }
    if(n < 0){
        cout << '-';
        n = -n;
    }
    string s;
    while(n > 0){
        s.push_back('0' + n % 10);
        n /= 10;
    }
    for(int i=s.size()-1; i>=0; i--){
        cout << s[i];
    }
}

struct pt{ ll x; ll y; };
pt operator+(const pt& a, const pt& b){ return pt{.x=a.x+b.x, .y=a.y+b.y}; };
pt operator-(const pt& a, const pt& b){ return pt{.x=a.x-b.x, .y=a.y-b.y}; };
bool operator==(const pt& a, const pt& b){ return a.x == b.x && a.y == b.y; };
// ostream& operator<<(ostream& os, const pt& a){ return os << "(" << a.x << "," << a.y << ")"; }
ll cross(pt p1, pt p2){
    return p1.x*p2.y - p1.y*p2.x;
}
ll orient(pt p, pt q, pt r) {
    ll v = (q.x-p.x)*(r.y-p.y)-(q.y-p.y)*(r.x-p.x);
    if(v == 0) return 0;
    return (v > 0) ? 2:1;
}

// bool on(const pt& a, const pt& b, const pt& c){
//     return min(a.x,b.x)<=c.x&&c.x<=max(a.x,b.x)&&min(a.y,b.y)<=c.y&&c.y<=max(a.y,b.y);
// }
bool on(const pt& p, const pt&r,  const pt& q) {
    return (min(p.x,q.x) <= r.x&& r.x <= max(p.x, q.x) &&min(p.y, q.y) <= r.y && r.y <= max(p.y, q.y));
}

bool inters(const pt& p1, const pt& q1, const pt& p2, const pt& q2){
    auto o1 = orient(p1, q1, p2);
    auto o2 = orient(p1, q1, q2);
    auto o3 = orient(p2, q2, p1);
    auto o4 = orient(p2, q2, q1);
    if(o1 != o2 && o3 != o4) return true;
    if(o1 == 0 && on(p1, p2, q1)) return true;
    if(o2 == 0 && on(p1, q2, q1)) return true;
    if(o3 == 0 && on(p2, p1, q2)) return true;
    if(o4 == 0 && on(p2, q1, q2)) return true;
    return false;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    ull t;
    cin >> t;

    for(ull i=0; i<t; i++){
        pt p1, p2, p3, p4;
        readInt128(p1.x); readInt128(p1.y);
        readInt128(p2.x); readInt128(p2.y);
        readInt128(p3.x); readInt128(p3.y);
        readInt128(p4.x); readInt128(p4.y);
        cout << (inters(p1, p2, p3, p4) ? "YES\n" : "NO\n");
    }
}

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