Task: | Line Segment Intersection |
Sender: | snude |
Submission time: | 2024-11-08 17:36:24 +0200 |
Language: | C++ (C++11) |
Status: | READY |
Result: | ACCEPTED |
test | verdict | time | |
---|---|---|---|
#1 | ACCEPTED | 0.07 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 <bits/stdc++.h>#include <cmath>using namespace std;using ll = long long;typedef long long C;typedef complex<C> P;#define X real()#define Y imag()// Debug printing#ifdef DEBUG#define deb(fmt, args...) printf("DEBUG: %d: " fmt, __LINE__, ##args)#define debug_print(fmt, args...) printf(fmt, ##args)#else#define deb(fmt, args...)#define debug_print(fmt, args...)#endifvoid print_array(vector<int> in, const string title = "Vector"){debug_print("DEBUG: %s [\nDEBUG: ", title.c_str());for (unsigned int i = 0; i < in.size(); i++) {debug_print("%d ", in[i]);}debug_print("\nDEBUG: ] END\n");}void print_matrix(vector<vector<int> > in, const string title = "Matrix"){debug_print("DEBUG: %s [\nDEBUG: ", title.c_str());for (unsigned int i = 0; i < in.size(); i++) {for (unsigned int j = 0; j < in[i].size(); j++) {debug_print("%d ", in[i][j]);}debug_print("\nDEBUG: ");}debug_print("DEBUG: ] END\n");}double angle(int x1, int y1, int x2, int y2) {double dot = x1*x2 + y1*y2;double cross = x1*y2 - y1*x2;return atan2(cross, dot);}double dist(int x1, int y1, int x2, int y2) {return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));}int main(int argc, char *argv[]){ios::sync_with_stdio(0);cin.tie(0);// Read the input parametersint n;cin >> n;double err = 0.00000000000001;for (int i = 0; i < n; i++) {ll x1, y1, x2, y2;ll x3, y3, x4, y4;cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4;P a = {x1, y1};P b = {x2, y2};P c = {x3, y3};P d = {x4, y4};P v1 = b - a;P v2 = d - c;// deb("round: %d\n",i);// Same endpointsif (a == c || a == d || b == c || b == d) {cout << "YES\n";continue;}// double k = 1.0 * (y2 - y1) / (x2 - x1);// double k2 = 1.0 * (y4 - y3) / (x4 - x3);// deb("k: %f, k2: %f\n", k, k2);ll cross = (conj(v1)*v2).Y;// Same slopes, check if they overlapif (cross == 0) {// deb("vittu\n");double ac = dist(x1, y1, x3, y3);double cb = dist(x3, y3, x2, y2);double ab = dist(x1, y1, x2, y2);double ad = dist(x1, y1, x4, y4);double db = dist(x4, y4, x2, y2);// deb("%f %f %f\n", ac, cb, ab);if (abs(ac + cb - ab) <= err) {cout << "YES\n";continue;}// deb("%f %f %f\n", ad, db, ab);if (abs(ad + db - ab) <= err) {cout << "YES\n";continue;}double ca = dist(x1, y1, x3, y3);double cd = dist(x3, y3, x4, y4);double bd = dist(x2, y2, x4, y4);// deb("%f %f %f\n", ca, ad, cd);if (abs(ca + ad - cd) <= err) {cout << "YES\n";continue;}// deb("%f %f %f\n", cb, bd, cd);if (abs(cb + bd - cd) <= err) {cout << "YES\n";continue;}}// deb("%d %d %d %d %d %d %d %d\n", x1, y1, x2, y2, x3, y3, x4, y4);double t1 = (1.0 * (x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4));double t2 = (1.0 * (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));double u1 = -(1.0 * (x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3));double u2 = (1.0 * (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4));// deb("t1: %f, u1: %f, t2: %f, u2: %f\n", t1, u1, t2, u2);// if (abs(t1) <= err && abs(t2) <= err && abs(u1) <= err && abs(u2) <= err) {// segments are parallel// if (abs(t1) <= err || abs(u1) <= err) {if (abs(t1) <= err && abs(t2) <= err && abs(u1) <= err && abs(u2) <= err) {// deb("paskaa\n");int ac = dist(x1, y1, x3, y3);int cb = dist(x3, y3, x2, y2);int ab = dist(x1, y1, x2, y2);if (abs(ac + cb - ab) <= err) {cout << "YES\n";continue;}}double t = t1 / t2;double u = u1 / u2;// deb("t: %f, u: %f\n", t, u);if (u >= -err && u <= 1.0 + err && t >= -err && t <= 1.0 + err) {cout << "YES\n";continue;}cout << "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 ... Truncated |
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 ... Truncated |
Test 3
Verdict: ACCEPTED
input |
---|
100000 492853 -452834 -657156 -282543... |
correct output |
---|
YES YES NO YES YES ... |
user output |
---|
YES YES NO YES YES ... Truncated |
Test 4
Verdict: ACCEPTED
input |
---|
100000 788522666 939776556 -831492125... |
correct output |
---|
NO NO NO NO NO ... |
user output |
---|
NO NO NO NO NO ... Truncated |
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 |