| Task: | Line Segment Intersection |
| Sender: | aalto26jh_006 |
| Submission time: | 2026-11-07 18:39:54 +0200 |
| Language: | Java |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.91 s | details |
| #2 | ACCEPTED | 0.96 s | details |
| #3 | ACCEPTED | 0.96 s | details |
| #4 | ACCEPTED | 0.96 s | details |
| #5 | ACCEPTED | 0.05 s | details |
| #6 | ACCEPTED | 0.05 s | details |
Code
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class LineSegmentIntersection {
public static void main(String[] args) {
FastScanner input = new FastScanner(System.in);
int t = input.nextInt();
String[] outputs = new String[t];
for (int i = 0; i < t; i++){
int x1 = input.nextInt();
int y1 = input.nextInt();
int x2 = input.nextInt();
int y2 = input.nextInt();
int x3 = input.nextInt();
int y3 = input.nextInt();
int x4 = input.nextInt();
int y4 = input.nextInt();
if(intersects(x1, y1, x2, y2, x3, y3, x4, y4)){
outputs[i] = "YES";
} else {
outputs[i] = "NO";
}
}
for (int i = 0; i < t; i++){
System.out.println(outputs[i]);
}
}
static long cross(long ax, long ay, long bx, long by,
long cx, long cy) {
return (bx - ax) * (cy - ay)
- (by - ay) * (cx - ax);
}
static boolean onSegment(long ax, long ay,
long bx, long by,
long px, long py) {
return px >= Math.min(ax, bx)
&& px <= Math.max(ax, bx)
&& py >= Math.min(ay, by)
&& py <= Math.max(ay, by);
}
static boolean intersects(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) {
long o1 = cross(x1, y1, x2, y2, x3, y3);
long o2 = cross(x1, y1, x2, y2, x4, y4);
long o3 = cross(x3, y3, x4, y4, x1, y1);
long o4 = cross(x3, y3, x4, y4, x2, y2);
if (((o1 > 0 && o2 < 0) || (o1 < 0 && o2 > 0)) && ((o3 > 0 && o4 < 0) || (o3 < 0 && o4 > 0))) return true;
if (o1 == 0 && onSegment(x1, y1, x2, y2, x3, y3)) return true;
if (o2 == 0 && onSegment(x1, y1, x2, y2, x4, y4)) return true;
if (o3 == 0 && onSegment(x3, y3, x4, y4, x1, y1)) return true;
if (o4 == 0 && onSegment(x3, y3, x4, y4, x2, y2)) return true;
return false;
}
static class FastScanner { //static class fastScanner
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public FastScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = null;
}
public String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
}
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 |
