| Task: | Line Segment Intersection |
| Sender: | aalto26jh_035 |
| Submission time: | 2026-11-09 00:04:13 +0200 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.08 s | details |
| #2 | ACCEPTED | 0.08 s | details |
| #3 | ACCEPTED | 0.10 s | details |
| #4 | ACCEPTED | 0.12 s | details |
| #5 | ACCEPTED | 0.00 s | details |
| #6 | ACCEPTED | 0.00 s | details |
Code
#include <iostream>
using namespace std;
typedef long long ll;
ll cross(ll ax, ll ay, ll bx, ll by, ll cx, ll cy) {
ll v = (bx - ax) * (cy - ay) - (by - ay) * (cx - ax);
return (v > 0) - (v < 0);
}
bool onSeg(ll ax, ll ay, ll bx, ll by, ll cx, ll cy) {
return min(ax, bx) <= cx && cx <= max(ax, bx) && min(ay, by) <= cy && cy <= max(ay, by);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
ll x1, y1, x2, y2, x3, y3, x4, y4;
cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;
ll d1 = cross(x1, y1, x2, y2, x3, y3), d2 = cross(x1, y1, x2, y2, x4, y4);
ll d3 = cross(x3, y3, x4, y4, x1, y1), d4 = cross(x3, y3, x4, y4, x2, y2);
bool ok = d1 * d2 < 0 && d3 * d4 < 0;
if (d1 == 0 && onSeg(x1, y1, x2, y2, x3, y3)) ok = true;
if (d2 == 0 && onSeg(x1, y1, x2, y2, x4, y4)) ok = true;
if (d3 == 0 && onSeg(x3, y3, x4, y4, x1, y1)) ok = true;
if (d4 == 0 && onSeg(x3, y3, x4, y4, x2, y2)) ok = true;
cout << (ok ? "YES" : "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 |
