We need to write a solution that determines if two line segments intersect. Given q up to 1000, coordinates up to 10000, no issues with performance. The typical approach uses orientation tests and checking bounding boxes.
We must consider collinear cases: segments may share endpoints, be collinear overlapping, etc. The problem likely expects standard intersection: they intersect if they share at least one common point, including endpoints touching? Usually yes, "intersect" means they have at least one point in common. So we need to handle degenerate cases properly.
The orientation test: cross product (b - a) x (c - a) = (x1 - x0)(y2 - y0) - (y1 - y0)(x2 - x0). It returns >0 for ccw, <0 for cw, 0 for collinear.
Two segments (p0,p1) and (p2,p3) intersect if:
General case: (orientation(p0,p1,p2) * orientation(p0,p1,p3) <= 0) AND (orientation(p2,p3,p0) * orientation(p2,p3,p1) <= 0), and also handle collinear cases.
But careful: if both products are <= 0, includes negative (different sides) or zero (collinear). However, if all orientations are zero, i.e., all four points collinear, we need to check if the intervals of projections overlap. So we can do: For collinear case (when orient(p0,p1,p2)==0 and orient(p0,p1,p3)==0), we check if the rectangles (min/max) overlap in x and y (actually we can check if the projections onto the line overlap). Since they are collinear, we can check if the x-ranges (or a parameter) overlap. Specifically, we check if p2 lies between p0 and p1 (inclusive) or p3 lies between, or p0 between p2 and p3, etc. Equivalent: max(min(p0.x,p1.x), min(p2.x,p3.x)) <= min(max(p0.x,p1.x), max(p2.x,p3.x)) and same for y? But since collinear, we could just check that the intervals in the one-dimensional projection onto the line overlap. However, checking both x and y is redundant; if they are collinear, it's enough to check that the intervals along the x-axis overlap (if the line is not vertical) else check y. But simpler: we can check if the bounding boxes overlap for collinear case: Actually for collinear segments, they intersect if the projections onto the line overlap. To avoid having to treat vertical separately, we can check using dot products: check if one endpoint of one segment lies on the other segment (inclusive). That is: For collinear case, check if the x-coordinate of p2 is between min(p0.x,p1.x) and max(p0.x,p1.x) and similarly for y; but if the segment is vertical, the x is constant but y changes; checking both min/max in x and y works? If collinear vertical, then p0.x==p1.x==p2.x==p3.x. Then we check y intervals: if the y intervals overlap. So we can check both axes: if the x intervals overlap AND y intervals overlap. But for collinear non-vertical, that also holds because if they are collinear, the overlapping segment must have overlapping x and y ranges. Actually if they are collinear, the intersection (if any) is a segment, which will have its own min and max x and y. The condition for overlapping is that the intervals on the line overlap, which implies both x-projection and y-projection overlap (since x and y are linearly related). However, for a horizontal segment (y constant), the y intervals are just points; checking y overlap is fine. So we can simply check: max(minX1, minX2) <= min(maxX1, maxX2) and similarly for y. This is also the condition for bounding box overlap, but bounding box overlap alone is insufficient for non-collinear intersecting segments that cross outside? Actually for general segments, we already use orientation tests, and the bounding box check is not needed except for collinear case. So for collinear case we need to check if the intervals overlap. So we can do:
Let orient012 = orientation(p0, p1, p2)
orient013 = orientation(p0, p1, p3)
orient230 = orientation(p2, p3, p0)
orient231 = orientation(p2, p3, p1)
if orient012 == 0 and orient013 == 0 and orient230 == 0 and orient231 == 0:
# all collinear
# check if intervals overlap
check if max(min(p0.x,p1.x), min(p2.x,p3.x)) <= min(max(p0.x,p1.x), max(p2.x,p3.x))
and similarly for y.
If both true -> intersect (1), else 0.
Otherwise:
if orient012 * orient013 <= 0 and orient230 * orient231 <= 0:
return 1
else:
return 0
But careful: The condition orient012 * orient013 <= 0 and orient230 * orient231 <= 0 covers the collinear cases as well (since product could be 0). However, if all orientations are zero, it's collinear and we need to check the interval overlap; but if only some are zero (e.g., p2 is collinear with p0-p1 but p3 is not), then the product may be 0, and the other product may be negative, and the segments might intersect at the endpoint (p2) if p2 lies on the segment p0-p1. However, the general condition with products <=0 already handles cases where one orientation is zero, provided we also check that the point is on the segment? Actually, if orient012 == 0 but orient013 != 0, then product <=0, and similarly the other product maybe something. But the segments may intersect if p2 lies on the segment p0-p1 and p0-p1 and p2-p3 cross? But if p2 is collinear with p0-p1 but not necessarily between them, the segments might not intersect. For example: p0=(0,0), p1=(1,0); p2=(2,0), p3=(2,1). Here orient012 = 0 (p2 is collinear on extended line), but p2 is not on segment p0-p1 (since x=2 >1). Then product orient012orient013 = 0 (since orient012=0). orient230? compute orientation(p2,p3,p0): (p2->p3 = (0,1), p2->p0 = (-2,0): cross=00? Actually (0,1) x (-2,0) = 00 - 1(-2) = 2 >0). orientation(p2,p3,p1): (0,1) x (-1,0) = 00 - 1(-1)=1 >0, product positive. So second condition fails, so overall false. That's fine.
But consider case: p0=(0,0), p1=(2,0); p2=(1,0), p3=(3,0) i.e., collinear overlapping? That is collinear case with all orientations zero, handled separately. Our general condition would yield: orient012=0, orient013=0 => product=0; orient230=0, orient231=0 => product=0; so both conditions satisfied (<=0) => would return 1. That is correct: they intersect (overlap). But wait, is overlapping considered intersect? Yes, they share points. So we should return 1. But does the general condition with products <=0 correctly return 1 for collinear overlapping? It does because both products are 0, so both conditions are true. However, we must ensure that the test doesn't give false positive for collinear non-overlapping. Let's test collinear but disjoint: p0=(0,0), p1=(1,0); p2=(2,0), p3=(3,0). orient012 = 0, orient013 = 0 (since p2 and p3 collinear). So product = 0. orient230 = 0, orient231 = 0. So both conditions satisfied => would incorrectly say intersect. That's the problem: When all four are collinear, the product conditions are satisfied even if they are disjoint. So we must treat collinear case separately to check overlap. So we need to distinguish: if all orientations are zero -> collinear case, check overlap. Else, use product condition.
But also consider case where three points are collinear but not all four? Example: p0=(0,0), p1=(2,0); p2=(1,0), p3=(1,1). Then orient012=0 (p2 on line), orient013 =? p3: (1,1) => cross = (2,0) x (1,1) = 21 - 01 = 2 >0. So product = 0. orient230: orientation(p2,p3,p0): p2p3=(0,1), p2p0=(-1,0) => cross=00-1(-1)=1>0; orient231: p2p3=(0,1), p2p1=(1,0)=> cross=00 -11 = -1 <0 => product negative. So both conditions satisfied => returns 1. Does segment intersect? p2 is endpoint of second segment? Actually p2=(1,0) lies on first segment, and p2 is endpoint of second segment? Wait p2 is endpoint of second segment? In problem, s2 is formed by p2 and p3. So p2 is an endpoint. So they intersect at point (1,0) which is p2. So yes they intersect. Good.
Now check collinear but one endpoint exactly at boundary: p0=(0,0), p1=(2,0); p2=(2,0), p3=(3,0). This is collinear overlapping at point (2,0). All four collinear? p2 lies on line, p3 also on line, so all orientations zero. Our collinear case would check interval overlap: minX1=0, maxX1=2; minX2=2, maxX2=3; overlap condition: max(0,2) <= min(2,3) => 2 <= 2 true. For y: all y=0 so minY1=0, maxY1=0; minY2=0, maxY2=0; overlap: max(0,0) <= min(0,0) => 0 <=0 true. So returns 1. Good.
Now collinear disjoint as above: minX1=0,max=1; minX2=2,max=3; overlap: max(0,2)=2 <= min(1,3)=1? false; so returns 0. Good.
Thus algorithm works.
Implementation details:
Define orientation(a,b,c): cross = (b.x - a.x)(c.y - a.y) - (b.y - a.y)(c.x - a.x). Return 1 if cross>0, -1 if cross<0, 0 if cross==0.
But careful about overflow: coordinates up to 10000, differences up to 20000, product up to 400 million, which fits within 32-bit signed int (2^31-1=2.147e9). 400 million < 2.147e9, so safe. But cross involves two multiplications, then subtraction; worst case: (20000)*(20000) = 400 million, minus another 400 million => up to 800 million, still safe. Actually difference up to 20000, product up to 400 million, sum up to 800 million, within 2.147e9. So int is fine.
Now coding: read integer q, then loop q times, read eight ints. For each, create points.
Define on_segment? Actually for collinear case we'll use the interval check as described.
Alternatively, we could unify by using bounding box check in collinear case; but it's okay.
Edge Cases: Floating point not needed.
Now produce output for each query: "1" or "0", each on its own line.
Now confirm example:
Input:
3
0 0 3 0 1 1 2 -1
0 0 3 0 3 1 3 -1
0 0 3 0 3 -2 5 0
First: s1: (0,0)-(3,0) horizontal; s2: (1,1)-(2,-1). These cross? The segment from (1,1) to (2,-1) is a line descending. It should intersect the x-axis between x=1 and x=2? Let's check orientation: p0=(0,0),p1=(3,0),p2=(1,1): orient012 = (3-0,0-0)x(1-0,1-0) = (3,0)x(1,1)=31-01=3>0; orient013 for p3=(2,-1): (3,0)x(2, -1)=3*(-1)-02 = -3<0 => product negative. orient230: p2=(1,1),p3=(2,-1),p0=(0,0): vector p2p3=(1,-2), p2p0=(-1,-1) => cross = 1(-1) - (-2)(-1) = -1 - 2 = -3? Wait compute properly: cross = (dx1)(dy2) - (dy1)(dx2) where (dx1,dy1)=p3-p2 = (1, -2), (dx2,dy2)=p0-p2 = (-1, -1). So cross = 1(-1) - (-2)(-1) = -1 - 2 = -3. orient231: p2p3=(1,-2), p2p1: p1-p2 = (3-1,0-1)=(2,-1) => cross = 1(-1) - (-2)*2 = -1 - (-4) = 3? Actually -1 - (-4)=3. So product = negative. So both conditions true, intersect -> 1.
Second: (0,0)-(3,0) and (3,1)-(3,-1). This is a vertical segment at x=3 from y=-1 to 1. They meet at (3,0) which is on both? The vertical segment includes (3,0)? Yes, because y=0 is between -1 and 1. So intersect. Compute: orient012: (3,0)x(3-0? Actually p2=(3,1) relative to p0: (3,1). cross = (3,0)x(3,1) = 31-03=3>0. orient013: p3=(3,-1): cross = 3*(-1)-03 = -3<0 => product negative. orient230: p2=(3,1),p3=(3,-1),p0=(0,0): p2p3 = (0,-2), p2p0 = (-3,-1): cross = 0(-1) - (-2)(-3) = 0 - 6 = -6? Actually compute: (0)(-1) - (-2)(-3) = 0 - 6 = -6 <0. orient231: p2p3=(0,-2), p2p1: p1-p2=(0-3? Actually p1=(3,0), so p2p1 = (0,-1): cross = 0(-1) - (-2)*0 = 0 - 0 = 0. So product = 0 * negative = 0? Actually orient231 = 0. So second product <=0. So conditions satisfied. Return 1.
Third: (0,0)-(3,0) and (3,-2)-(5,0). s2 from (3,-2) to (5,0). They share point? Possibly at (3,0)? But (3,0) is on s1 endpoint. s2: line from (3,-2) to (5,0). Parameter: at x=3, y=-2; at x=5, y=0. So (3,0) is not on that line because at x=3, y should be -2 (since line passes through those points? Actually the segment is from (3,-2) to (5,0). The line equation: y+2 = (0+2)/(5-3)(x-3) => slope 1? (2/2=1) so y = x-5? Check: when x=3, y = -2; when x=5, y=0. So at x=3, y=-2. So (3,0) is above. So they do not intersect. Let's compute orientation: orient012: p2=(3,-2) relative to p0,p1: cross (3,0)x(3,-2)=3(-2)-03 = -6<0; orient013: p3=(5,0): cross = 30 - 05 = 0? Actually (3,0)x(5,0) = 30 - 05 = 0. So product = 0? (0 * negative = 0). orient230: p2,p3,p0: p2p3 = (2,2), p2p0 = (-3,2): cross = 22 - 2*(-3) = 4 +6 =10>0; orient231: p2p3 = (2,2), p2p1 = (0,0?) Actually p1=(3,0), p2p1 = (0,2): cross = 22 - 20 = 4 >0. So product positive. So second condition fails -> 0. So output as expected.
Thus algorithm yields correct.
We'll code accordingly.
Potential pitfalls: In collinear case, we also need to check for proper overlapping including endpoints. The interval check inclusive: using <=. So condition: if max(minX1,minX2) <= min(maxX1,maxX2) and similarly for y. This works because inclusive.
But note: If the collinear segment is a point? But constraints p0!=p1 and p2!=p3, so segments have positive length. However, endpoints could coincide? Possibly, but still segments have length >0? Actually p0 != p1 ensures segment has non-zero length. So we don't have degenerate zero-length segments.
But collinear case with overlapping at a single point is okay; the interval check will see that the max of mins equals min of max (if point is shared) and condition holds.
Now test some tricky cases:
-
Segments cross exactly at interior: orientation products both negative => 1.
-
One segment endpoint lies exactly on the other segment but not collinear: orientation for that endpoint = 0, product condition yields zero, other product might be nonzero but still condition satisfied if the other orientation product is <=0. But need to ensure the point is actually between the endpoints. In such case, orientation zero indicates collinear with the segment? Wait if an endpoint lies on the other segment but not collinear? Actually if a point lies on a segment, it must be collinear with the endpoints of that segment. So orientation will be zero. So product condition includes that. But we also need to ensure that the point is within the bounding box of the segment. However, the orientation zero alone does not guarantee the point is on the segment; it could be on the infinite extension line. But the other orientation product condition (the other segment's orientations relative to the first segment's endpoints) helps to restrict that? Let's test: Consider p0=(0,0), p1=(2,0). p2=(3,0), p3=(3,1). Here p2 is on extension but not on segment. We computed earlier and found that product condition fails because orient230*orient231 >0. So it's fine.
But consider scenario: p0=(0,0), p1=(2,0). p2=(4,0), p3=(4,2). Then orient012=0, orient013? p3 is not collinear, cross? (2,0)x(4,2)=22-04=4>0 => product=0. orient230: p2p3=(0,2), p2p0=(-4,0): cross=00 - 2(-4)=8>0; orient231: p2p3=(0,2), p2p1=(-2,0): cross=00 -2(-2)=4>0 => product positive. So fails.
Now consider scenario: p0=(0,0), p1=(2,2). p2=(1,1), p3=(3,0). p2 lies on segment p0-p1? Yes, collinear and between. p2 is endpoint of second segment. orient012 = orientation(p0,p1,p2) = (2,2)x(1,1)=21-21=0. orient013: p3=(3,0): (2,2)x(3,0)=20-23=-6 => product=0. orient230: p2,p3,p0: p2p3=(2,-1), p2p0=(-1,-1): cross=2*(-1)-(-1)(-1) = -2 -1 = -3 <0. orient231: p2p3=(2,-1), p2p1=(1,1): cross=21 - (-1)*1 = 2+1=3>0 => product negative. So both conditions satisfied. Good.
Now collinear case: both segments collinear but disjoint: we handle separately. But what about collinear but one segment's endpoint exactly touches the other? That is overlapping at a point, we covered.
Now what about collinear but one segment completely inside the other? Overlap, return 1.
Thus algorithm fine.
Implementation in C++: We'll define a struct Point with int x,y.
Orientation function returns long long maybe to avoid overflow? but int okay.
We'll have a function intersect that returns bool.
Pseudocode:
bool onSegment(Point p, Point q, Point r) // check if q lies on segment pr
{
return q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) &&
q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y);
}
But we will not need separate; for collinear case we can just do interval checks.
Alternatively, we can use the classic approach from CLRS: two segments intersect iff (o1 = orient(p0,p1,p2)), (o2=orient(p0,p1,p3)), (o3=orient(p2,p3,p0)), (o4=orient(p2,p3,p1)) and either:
- o1 != 0, o2 != 0, o3 != 0, o4 != 0 and o1o2 < 0 and o3o4 <0, or
- (o1 == 0 && onSegment(p0,p2,p1)) || (o2 == 0 && onSegment(p0,p3,p1)) || (o3 == 0 && onSegment(p2,p0,p3)) || (o4 == 0 && onSegment(p2,p1,p3)).
That handles collinear endpoints properly.
But the simpler approach with collinear case separate works.
Given q up to 1000, we can use either.
I'll implement the classic approach because it's clearer and avoids the need for separate check for all four collinear? Actually classic also checks all four collinear by using onSegment conditions; but if all four collinear, then it will check onSegment conditions: if any endpoint lies on the other segment, then intersect. However, if they overlap but no endpoint lies on the other segment (e.g., one segment completely inside the other but endpoints are distinct), then the classic approach might miss it if we only check endpoints? Actually classic condition often includes: if all orientations are zero and the segments' projections overlap (or they check onSegment for each endpoint). But typical implementation from geeksforgeeks:
int orient(Point p, Point q, Point r) { ... }
bool onSegment(Point p, Point q, Point r) { ... }
bool intersect(Point p1, Point q1, Point p2, Point q2) {
int o1 = orient(p1,q1,p2);
int o2 = orient(p1,q1,q2);
int o3 = orient(p2,q2,p1);
int o4 = orient(p2,q2,q1);
if (o1 != 0 && o2 != 0 && o3 != 0 && o4 != 0) {
return (o1 > 0) != (o2 > 0) && (o3 > 0) != (o4 > 0);
}
// Special Cases: Collinear
if (o1 == 0 && onSegment(p1,p2,q1)) return true;
if (o2 == 0 && onSegment(p1,q2,q1)) return true;
if (o3 == 0 && onSegment(p2,p1,q2)) return true;
if (o4 == 0 && onSegment(p2,q1,q2)) return true;
return false;
}
Does this cover overlapping collinear segments without endpoint on the other? Example: p1=(0,0), q1=(3,0); p2=(1,0), q2=(2,0). Here none of the endpoints lie on the other segment? Actually p2=(1,0) lies on segment p1-q1? Yes, it does (since 1 between 0 and 3). So onSegment(p1,p2,q1) would be true because p2 is on segment p1q1. So condition triggers. So good. But consider p1=(0,0), q1=(3,0); p2=(1,0), q2=(4,0). Then p2 lies on p1q1 (1 between 0 and3) -> triggers. If p2 and q2 are both inside? Actually if both inside, p2 triggers. If p2 outside but q2 inside, q2 triggers. So any overlap will cause at least one endpoint of one segment to lie on the other segment. The only case where overlapping but no endpoint lies on the other segment is when one segment completely contains the other, but then the endpoints of the inner segment are inside the outer segment, meaning they lie on the outer segment? Actually if inner segment is strictly inside, its endpoints are points on the outer segment? No, if outer segment is from 0 to 3, inner from 1 to 2, both endpoints 1 and 2 lie on outer segment? Yes, they lie on the outer segment because outer segment includes all points from 0 to 3. So onSegment will be true for p2 or q2. So that works. If they are collinear but disjoint, no endpoint lies on the other segment. So the classic approach works. And it's simpler because we don't need to treat all-four-collinear specially; we just check each orientation zero case.
Thus I'll implement classic approach.
We'll define orientation returning int (or long long) that returns positive, negative, zero.
Define onSegment: check if point q lies on segment pr, assuming collinear. Check x between min and max, y between min and max inclusive.
Then intersect: as above.
Let's test: Example with collinear disjoint: p1=(0,0), q1=(1,0); p2=(2,0), q2=(3,0). o1=orient(p1,q1,p2)=0, onSegment(p1,p2,q1)? p2 is (2,0) not on segment p1q1 because x not between 0 and1 -> false. o2=orient(p1,q1,q2)=0, onSegment(p1,q2,q1)? false. o3=orient(p2,q2,p1)=0, onSegment(p2,p1,q2)? p1=(0,0) not on p2q2 -> false. o4=0, onSegment(p2,q1,q2)? q1=(1,0) not on -> false. So returns false. Good.
Case with collinear overlapping interior: p1=(0,0), q1=(3,0); p2=(1,0), q2=(2,0). o1=0, onSegment(p1,p2,q1) => p2 on segment p1q1? Yes -> true, returns true.
Case with collinear touching at endpoint: p1=(0,0), q1=(2,0); p2=(2,0), q2=(3,0). o1=0, onSegment(p1,p2,q1)? p2 on p1q1? Since p2=(2,0) is endpoint of p1q1 (q1). Typically onSegment(p1,p2,q1) would check if p2 lies on segment p1-q1 inclusive. It should return true if p2 equals q1. Our onSegment will check min and max inclusive. So returns true. So intersect = true.
Thus works.
Now confirm edge case: When orientations are all non-zero and segments cross. Then the first if condition (general case) works: return (o1 > 0) != (o2 > 0) && (o3 > 0) != (o4 > 0). This is equivalent to o1o2 < 0 and o3o4 < 0 because they are non-zero. But careful: if we use product, we must beware overflow. But we can compare signs directly without multiplication: (o1 > 0) ^ (o2 > 0) etc. But using product <0 works as long as we ensure they are ints and product doesn't overflow. Since o1 and o2 are at most ~800 million? Actually orientation value can be up to around 400 million? Actually earlier computed up to 800 million? But product of two such numbers could be up to ~6.4e17, which exceeds 32-bit but maybe fits in 64-bit. In C++ with int (32-bit) product could overflow and cause undefined behavior or wrong sign. So it's safer to compare signs without multiplication. We'll use the condition with comparisons.
Implementation:
if(o1 != 0 && o2 != 0 && o3 != 0 && o4 != 0) {
// general case: check if signs differ
if( (o1 > 0) != (o2 > 0) && (o3 > 0) != (o4 > 0) )
return true;
else
return false;
}
Else // special cases
But note: The classic code often does:
if (o1 != o2 && o3 != o4) return true; but that's when orientations are considered as -1,0,1 after sign? Actually we can map to -1,0,1 to avoid overflow. But better to compute sign separately.
Simpler: We can use long long for orientation to avoid overflow when multiplying. But we can also compare signs.
I'll compute orientation as long long to be safe; but product of two long long's also large but not overflow given range (still within 64-bit). Actually orientation values can be up to 8e8, product up to 6.4e17, which fits in signed 64-bit (9.22e18). So if we use long long for orientation and product, it's safe. But we also need to compare signs; product <0 is simpler.
But I'll stick with typical approach using direct sign comparison to be clean.
Now check if we need to include case where some orientation is zero but not all? The classic code handles that by checking onSegment when orientation zero.
Thus overall:
bool segments_intersect(Point p0, Point p1, Point p2, Point p3) {
int o1 = orientation(p0, p1, p2);
int o2 = orientation(p0, p1, p3);
int o3 = orientation(p2, p3, p0);
int o4 = orientation(p2, p3, p1);
// General case
if (o1 != 0 && o2 != 0 && o3 != 0 && o4 != 0) {
return ((o1 > 0) != (o2 > 0)) && ((o3 > 0) != (o4 > 0));
}
// Special cases (collinear)
if (o1 == 0 && onSegment(p0, p2, p1)) return true;
if (o2 == 0 && onSegment(p0, p3, p1)) return true;
if (o3 == 0 && onSegment(p2, p0, p3)) return true;
if (o4 == 0 && onSegment(p2, p1, p3)) return true;
return false;
}
Note: onSegment function expects points p, q, r where q is the point to test, and we assume q is collinear with p and r. We'll implement accordingly.
I'll define orientation returning long long to be safe, but we can also use int.
Now complexity: O(q). q max 1000, trivial.
Now input format: q on first line, then q lines each with eight integers.
We need to output each result on its own line.
Now confirm with example.
Now potential pitfalls: When reading coordinates, they are integers, possibly negative.
Now implement onSegment:
bool onSegment(Point p, Point q, Point r) {
// Check if q lies on segment pr
return (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) &&
q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y));
}
Note: The function expects p and r are endpoints of segment, q is candidate. This inclusive.
Now I'll write the full solution in C++.
Now test with given example.
Edge Cases:
-
Vertical/horizontal lines, orientation zero with correct onSegment.
-
Segments intersect at endpoint: should return true.
Now test manually:
Case 1: p0=(0,0), p1=(0,5), p2=(0,5), p3=(5,5). They intersect at (0,5). o1 = orientation(p0,p1,p2) = orientation((0,0),(0,5),(0,5)) = cross = (0,5)x(0,5)=05 -50=0. So o1==0, onSegment(p0,p2,p1): p2=(0,5) is endpoint of p1, so returns true. Good.
Case: p0=(0,0), p1=(0,5), p2=(0,6), p3=(6,6). Here p2 not on segment. o1=0, onSegment(p0,p2,p1)? p2.x=0, but y=6 >5, false. o2 orientation(p0,p1,p3) non-zero (likely). o3 orientation(p2,p3,p0) maybe? It's not collinear. So no true. Should return false. Good.
Now test crossing at interior: p0=(0,0), p1=(2,2), p2=(0,2), p3=(2,0). They cross at (1,1). o1 = orientation(p0,p1,p2): (2,2)x(0,2) = 22 - 20 = 4 >0. o2: orientation(p0,p1,p3): (2,2)x(2,0)=20 - 22 = -4 <0 => signs differ. o3: orientation(p2,p3,p0): (2,-2)x(0,-2)=2*(-2)-(-2)0= -4? Let's compute correctly: p2p3 = (2, -2), p2p0 = (0, -2) => cross = 2(-2) - (-2)0 = -4, negative. o4: orientation(p2,p3,p1): p2p3 = (2,-2), p2p1 = (2,0) => cross = 20 - (-2)*2 = 4, positive => signs differ. So all non-zero, signs differ for both => true.
Now test parallel but not collinear: p0=(0,0), p1=(1,0), p2=(0,1), p3=(1,1). o1: (1,0)x(0,1)=11-00=1>0; o2: (1,0)x(1,1)=11-01=1>0 => same sign; o3: (1,0)x(-0, -1?) Actually compute: p2p3=(1,0), p2p0=(0,-1) => cross=1*(-1)-00=-1<0; o4: (1,0)x(1,-1)=1(-1)-0*1=-1<0 => same sign. So all non-zero, but signs not different => false.
Now test one segment's endpoint exactly on the other segment but not collinear? That can't happen because if it's on the segment, it must be collinear. So orientation zero.
Thus algorithm works.
Now I'll code.
Potential issue: orientation might overflow 32-bit int? I'll use long long to be safe, but int is fine. But to be absolutely safe, I'll use long long.
Thus:
typedef long long ll;
struct Point { ll x, y; };
ll orient(Point a, Point b, Point c) {
return (b.x - a.x)(c.y - a.y) - (b.y - a.y)(c.x - a.x);
}
bool onSegment(Point p, Point q, Point r) {
return q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) &&
q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y);
}
bool intersect(Point p0, Point p1, Point p2, Point p3) {
ll o1 = orient(p0, p1, p2);
ll o2 = orient(p0, p1, p3);
ll o3 = orient(p2, p3, p0);
ll o4 = orient(p2, p3, p1);
if (o1 != 0 && o2 != 0 && o3 != 0 && o4 != 0) {
return ((o1 > 0) != (o2 > 0)) && ((o3 > 0) != (o4 > 0));
}
// Special cases
if (o1 == 0 && onSegment(p0, p2, p1)) return true;
if (o2 == 0 && onSegment(p0, p3, p1)) return true;
if (o3 == 0 && onSegment(p2, p0, p3)) return true;
if (o4 == 0 && onSegment(p2, p1, p3)) return true;
return false;
}
In main: read q, loop, read coordinates, create points, call intersect, output 1 or 0.
Now test quickly with example.
But note: In the onSegment function, we need to include the case where the segment is a single point? But not needed because segments have distinct endpoints. However, for safety, it's fine.
Now ensure that the onSegment condition works when p and r are same? Not needed, but if they were same, the min/max would be same and point must equal that. But p0!=p1, p2!=p3, so safe.
Now consider integer sign: orientation returns long long, we compare to zero.
Now output: each result on a new line.
Now the code is done.
But I'll double-check the classic approach with collinear overlapping interior.
Test: p0=(0,0), p1=(5,0); p2=(2,0), p3=(3,0). Here o1=0, onSegment(p0,p2,p1) -> true, so returns 1. Good.
Test: p0=(0,0), p1=(5,0); p2=(6,0), p3=(7,0). o1=0, onSegment(p0,p2,p1) false; o2=0, onSegment(p0,p3,p1) false; o3=0, onSegment(p2,p0,p3) false? Check onSegment(p2,p0,p3): p2=(6,0), p3=(7,0), p0=(0,0). Need to check if p0 lies on segment p2-p3. p0.x=0 not between 6 and 7, false. o4=0, onSegment(p2,p1,p3): p1=(5,0) not between 6 and 7, false. So returns false. Good.
Now test scenario where one segment's endpoint lies exactly on the other segment but not at endpoint? That's same as above.
Now test scenario where segments are collinear and share exactly one endpoint. Already covered.
Now test scenario where both segments are collinear and one is exactly the same as the other: p0=(0,0), p1=(5,0); p2=(0,0), p3=(5,0). Then o1=0, onSegment(p0,p2,p1) -> p2 on segment? p2 is (0,0) which is endpoint of p0-p1, onSegment returns true, so intersect. Good. Also could be detected by other conditions.
Now test scenario where they are collinear and one is subset but endpoints exactly equal? Already covered.
Now edge case: orientation might be zero due to collinearity and the point is on the line extension. onSegment will check bounding box, so it's safe.
Now check scenario where three points are collinear but not all four, but the zero orientation is for a point that is collinear but not between endpoints, and other orientations are non-zero but may still allow intersection? Example: p0=(0,0), p1=(2,0); p2=(3,0), p3=(3,1). o1=0 (p2 collinear but outside), onSegment(p0,p2,p1) false because p2 not between. o2 non-zero, o3 non-zero, o4 non-zero. The general case: o1=0 so we skip general case and go to special cases. Since o1==0 but onSegment false, we check others: o2 !=0, o3? o3 = orientation(p2,p3,p0) not zero, o4 not zero. No other zero orientation. So all special cases fail, return false. That's correct.
Now test scenario where segments touch at endpoints: p0=(0,0), p1=(1,1); p2=(1,1), p3=(2,0). o1 = orientation(p0,p1,p2) = (1,1)x(1,1)=0, onSegment(p0,p2,p1) -> p2 equals p1, so onSegment returns true (since min(p0.x,p1.x)=0 max=1, p2.x=1 within; similarly y). So returns true. Good.
Now scenario where endpoints are same but segments are different directions: p0=(0,0), p1=(1,1); p2=(0,0), p3=( -1,-1). They share endpoint (0,0). o1? orientation(p0,p1,p2): p2 = (0,0) same as p0? Actually p2 is (0,0) same as p0? But p0 is (0,0) and p2 is (0,0). That means we have two segments sharing an endpoint. Compute orientation(p0,p1,p2): p0=(0,0), p1=(1,1), p2=(0,0). Then vector p0p1=(1,1), p0p2=(0,0). Cross=10 - 10 =0. So o1=0. onSegment(p0,p2,p1): check if p2 lies on segment p0-p1. p2 = (0,0) is endpoint p0, so yes. So returns true. Good.
Now scenario where they touch at a point that is not an endpoint for either? That would be interior crossing, already handled.
Thus algorithm is sound.
Now we just need to code accordingly.
Potential nuance: The onSegment function uses <= and >= comparisons. Since all coordinates are integers, fine.
Now about reading input: we have to be careful with large q but simple.
Now I'll write final answer.
But the problem statement says: "For given two segments s1 and s2, print "1" if they are intersect, "0" otherwise." So output just 1 or 0.
Now final code.
Wait, I just realized something: In the classic algorithm, the general case condition is often written as:
if (o1 != 0 && o2 != 0 && o3 != 0 && o4 != 0) {
if ((o1 > 0) != (o2 > 0) && (o3 > 0) != (o4 > 0))
return true;
else
return false;
}
But what about the case where some orientation is zero, but the other orientations indicate crossing? Actually if any orientation is zero, we need to handle special cases. However, consider scenario where one endpoint lies exactly on the line of the other segment but not on the segment (i.e., collinear but outside), and the other segment's endpoints are on opposite sides of that line? In that case, the segments may intersect at that endpoint? Actually if the endpoint is on the extension, they could intersect only if that endpoint is also on the other segment (i.e., lies between its endpoints). But if it's outside, they do not intersect. So we rely on onSegment to confirm.
Thus fine.
Now implement.
Potential pitfalls with orientation sign for zero: In the general case we require all non-zero. That's fine.
Now also consider case where both segments are collinear but overlapping as per earlier. That will be caught by special cases because at least one orientation zero and onSegment true. So correct.
Now double-check: For collinear overlapping, indeed some orientation zero, and onSegment for that point true.
Now about performance: orientation computed 4 times per query. Fine.
Now code.
Will use #include <bits/stdc++.h> for simplicity.
Now final answer.
But wait: The problem constraints: q up to 1000, but could be more? It's fine.
Now produce final answer.
But the user is ChatGPT, not a compiler; we just need to output the solution code. So we produce final answer as code block with explanation.
Given that the instructions are to provide solution, I'll output a C++ implementation.
Thus final response. ```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Point {
ll x, y;
};
// Orientation test: >0 if ccw, <0 if cw, =0 if collinear
ll orient(const Point& a, const Point& b, const Point& c) {
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
// Checks if point q lies on segment pr (collinearity assumed)
bool onSegment(const Point& p, const Point& q, const Point& r) {
return (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) &&
q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y));
}
// Returns true if the two segments (p0,p1) and (p2,p3) intersect
bool intersect(const Point& p0, const Point& p1, const Point& p2, const Point& p3) {
ll o1 = orient(p0, p1, p2);
ll o2 = orient(p0, p1, p3);
ll o3 = orient(p2, p3, p0);
ll o4 = orient(p2, p3, p1);
// General case: all orientations non‑zero and signs differ
if (o1 != 0 && o2 != 0 && o3 != 0 && o4 != 0) {
return ((o1 > 0) != (o2 > 0)) && ((o3 > 0) != (o4 > 0));
}
// Special cases: some orientation is zero → check collinear overlap
if (o1 == 0 && onSegment(p0, p2, p1)) return true;
if (o2 == 0 && onSegment(p0, p3, p1)) return true;
if (o3 == 0 && onSegment(p2, p0, p3)) return true;
if (o4 == 0 && onSegment(p2, p1, p3)) return true;
return false;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int q;
cin >> q;
while (q--) {
Point p0, p1, p2, p3;
cin >> p0.x >> p0.y >> p1.x >> p1.y >> p2.x >> p2.y >> p3.x >> p3.y;
cout << (intersect(p0, p1, p2, p3) ? "1\n" : "0\n");
}
return 0;
}