CSES - Aalto Competitive Programming 2024 - wk10 - Homework - Results
Submission details
Task:Line Segment Intersection
Sender:hungdojan
Submission time:2024-11-11 01:29:42 +0200
Language:C++ (C++17)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.07 sdetails
#2ACCEPTED0.09 sdetails
#3ACCEPTED0.10 sdetails
#4ACCEPTED0.12 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails

Code

#include <bits/stdc++.h>
using namespace std;

#define I_2D(row, col, width) ((row) * (width) + (col))
#define PRINT_ARR(arr, n)                                                      \
  do {                                                                         \
    for (int i = 0; i < n; i++) {                                              \
      cout << arr[i] << " ";                                                   \
    }                                                                          \
    cout << "\n";                                                              \
  } while (0)
#define PRINT_VEC_ARR(v, n)                                                    \
  do {                                                                         \
    for (int i = 0; i < n; i++) {                                              \
      cout << i << ": ";                                                       \
      for (auto item : v[i]) {                                                 \
        cout << item << " ";                                                   \
      }                                                                        \
      cout << endl;                                                            \
    }                                                                          \
  } while (0)

#define endl '\n'
typedef long long ll;
typedef complex<ll> Point;
#define X real()
#define Y imag()
#define CP(a, b, c) ((conj(c - a) * (c - b)).Y)

bool cmp(const Point &a, const Point &b) {
  return (a.X == b.X) ? (a.Y < b.Y) : (a.X < b.X);
}

bool is_on_the_line(const Point a, const Point b, const Point p) {
  vector<Point> pp{a, b, p};
  sort(pp.begin(), pp.end(), cmp);
  return pp[1] == p;
}

int sign(ll x) { return (x > 0) - (x < 0); }

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  int x, y;
  vector<Point> p(4);
  for (int i = 0; i < t; i++) {
    for (int j = 0; j < 4; j++) {
      cin >> x >> y;
      p[j] = {x, y};
    }
    Point a = p[0];
    Point b = p[1];
    Point c = p[2];
    Point d = p[3];

    ll cp1 = CP(a, b, c);
    ll cp2 = CP(a, b, d);
    ll cp3 = CP(c, d, a);
    ll cp4 = CP(c, d, b);
    if ((cp1 == 0 && is_on_the_line(a, b, c)) ||
        (cp2 == 0 && is_on_the_line(a, b, d)) ||
        (cp3 == 0 && is_on_the_line(c, d, a)) ||
        (cp4 == 0 && is_on_the_line(c, d, b))) {
      cout << "YES" << endl;
    } else if (sign(cp1) != sign(cp2) && sign(cp3) != sign(cp4)) {
      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: 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