The teacher gave Anton a large geometry homework, but he didn't do it (as usual) as he participated in a regular round on Codeforces. In the task he was given a set of n lines defined by the equations y = ki·x + bi. It was necessary to determine whether there is at least one point of intersection of two of these lines, that lays strictly inside the strip between x1 < x2. In other words, is it true that there are 1 ≤ i < j ≤ n and x', y', such that:
y' = ki * x' + bi, that is, point (x', y') belongs to the line number i;
y' = kj * x' + bj, that is, point (x', y') belongs to the line number j;
x1 < x' < x2, that is, point (x', y') lies inside the strip bounded by x1 < x2.
You can't leave Anton in trouble, can you? Write a program that solves the given task.
Input
The first line of the input contains an integer n (2 ≤ n ≤ 100 000) — the number of lines in the task given to Anton. The second line contains integers x1 and x2 ( - 1 000 000 ≤ x1 < x2 ≤ 1 000 000) defining the strip inside which you need to find a point of intersection of at least two lines.
The following n lines contain integers ki, bi ( - 1 000 000 ≤ ki, bi ≤ 1 000 000) — the descriptions of the lines. It is guaranteed that all lines are pairwise distinct, that is, for any two i ≠ j it is true that either ki ≠ kj, or bi ≠ bj.
Output
Print "Yes" (without quotes), if there is at least one intersection of two distinct lines, located strictly inside the strip. Otherwise print "No" (without quotes).
Examples
Input
4
1 2
1 2
1 0
0 1
0 2
Output
NO
Input
2
1 3
1 0
-1 3
Output
YES
Input
2
1 3
1 0
0 2
Output
YES
Input
2
1 3
1 0
0 3
Output
NO
Note
In the first sample there are intersections located on the border of the strip, but there are no intersections located strictly inside it.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to determine if there exist two distinct lines with intersection point strictly between x1 and x2. Let's denote two lines i,j with equations y = ki x + bi, y = kj x + bj. Intersection at x = (bj - bi) / (ki - kj), provided ki != kj. If ki = kj, lines are parallel (no intersection). Since distinct lines guarantee either different slopes or intercepts, if slopes are equal, they are distinct and parallel (no intersection). So only pairs with different slopes can intersect.
Thus condition: there exist i<j with ki != kj such that x1 < (bj - bi) / (ki - kj) < x2.
We need to check if any such pair exists efficiently for n up to 100k.
Observation: For each line, we can compute its y-values at x1 and x2: y_i(x1)=kix1+bi, y_i(x2)=kix2+bi. The intersection point between two lines i and j lies strictly between x1 and x2 if and only if the order of the lines at x1 and at x2 is different (i.e., one line is above the other at x1 and the other is above at x2), assuming they are not parallel. Actually careful: For two non-parallel lines, consider their values at x1: f_i(x1) and f_j(x1); at x2: f_i(x2) and f_j(x2). The intersection x-coordinate is between x1 and x2 if and only if (f_i(x1)-f_j(x1)) and (f_i(x2)-f_j(x2)) have opposite signs. Because at x1, one line is above; at x2, the other is above; since lines are linear, they cross exactly once. So the condition for the intersection being strictly inside (x1,x2) is that f_i(x1) - f_j(x1) and f_i(x2) - f_j(x2) have opposite signs (i.e., product < 0). Also if they are equal at either endpoint, then intersection is at that endpoint; that is not allowed (must be strictly inside). So we need strict inequality: sign change.
Thus we want to know if there exist i<j such that (f_i(x1)-f_j(x1))*(f_i(x2)-f_j(x2)) < 0.
Alternatively, we can think of each line as a point (a_i, b_i) maybe? But here f_i(x1) = kix1+bi, f_i(x2) = kix2+bi. So define A_i = f_i(x1) and B_i = f_i(x2). Then the condition is A_i < A_j and B_i > B_j (or A_i > A_j and B_i < B_j). So we need to check if there exists an inversion between the ordering of lines by A and by B. That is: sort lines by A, then check if the sequence of B's is not increasing (or decreasing). If there is any inversion (i.e., in sorted order by A, the B-values are not monotonic) then there exists a pair with opposite order. However we must also consider equality? If A_i = A_j for some i,j? Then intersection at x1? Actually if A_i = A_j, then they intersect at x=x1 exactly (if slopes are different they cross at x1? Let's see: If f_i(x1)=f_j(x1) then intersection point x = (bj - bi)/(ki - kj) yields exactly x1 if the expression equals x1? Not necessarily; if f_i(x1)=f_j(x1), the two lines have same value at x1. They could be parallel (if slopes equal) or they could intersect at some x, but if they are not parallel, the intersection x coordinate might not be x1. Actually if f_i(x1)=f_j(x1), then they share a point at x1. The intersection point may be exactly x1, because at x1 the y-values are equal. But is it guaranteed that the intersection point is exactly x1? For two lines, if they have same y at x1, then they intersect at x1 (since lines: each line is defined for all x; they both pass through (x1, y) for that y). However, if they are distinct lines, they can intersect at exactly one point; so if they have same value at x1, that point (x1,y) is on both lines, so it is the intersection point. Therefore intersection at x=x1, which is not strictly inside. So we should treat equality as not counting. So condition for strictly inside is that the orders are opposite and not equal at endpoints. So in terms of ordering by A, we want strict inequalities: A_i < A_j and B_i > B_j (or vice versa). If A_i = A_j, then they intersect at x1 or are parallel? Actually if A_i = A_j and slopes are different, then intersection is at x1. But if slopes are same? Then they are distinct parallel lines: they never intersect. However if slopes are same and A_i = A_j, then they are actually the same line? Wait, if slopes are same (ki=kj) and also f_i(x1)=f_j(x1) => kix1+bi = kjx1+bj => since ki=kj, then bi=bj. That would imply lines are identical, contradict distinctness. So if slopes are same, they can't have A_i = A_j unless they are same line. So distinct lines with same A must have different slopes, and then they intersect at x1. So we don't count them. So we require strict inequality in A ordering to avoid intersection exactly at boundary.
Similarly, B_i = B_j would lead to intersection at x2.
Thus we need to check if there exists a pair with A_i < A_j and B_i > B_j. That is exactly an inversion in the sequence when sorting by A.
Therefore a simple algorithm: For each line i, compute a_i = kix1 + bi, b_i = kix2 + bi. Then sort the lines by a_i, breaking ties arbitrarily? Actually if there are ties (a_i = a_j), then they would give intersection exactly at x1. But we don't want to count them because intersection is on border. However ties could cause false inversion detection if we treat them as non-strict? For example, suppose a_i = a_j, but b_i < b_j. If we sort with ties arbitrarily, then we might get an inversion in b's? But we need to ensure we only consider pairs with a_i < a_j strictly, because a_i = a_j are not valid for inside intersection. So we can sort by a_i, and then for equal a_i we need to handle them carefully: If there are multiple lines with same a_i, any two of them intersect at x1 (unless slopes equal? But as argued, distinct lines with same a_i must have different slopes, so they intersect at x1). So they are not a valid pair. Moreover, for the purpose of checking inversion for strict A ordering, we can ignore equal a_i pairs; that is, we want to know if there exists i<j after sorting by a_i (with some tie-breaking) such that a_i < a_j (i.e., strictly less) and b_i > b_j. If we break ties arbitrarily, then if there are lines with same a_i, they will be adjacent. For any pair among them, a_i = a_j, so not counted. However, when we check b_i > b_j for those, we might erroneously count inversion if we treat them as distinct and a_i < a_j? But a_i is not < a_j; they are equal. So we must only check pairs where a_i < a_j. So if we sort by a_i, and then scan and maintain the minimum b seen so far? Actually typical algorithm to check for inversion: Sort by a (ascending), then if any b value is not sorted ascending, i.e., there exists i<j with a_i < a_j but b_i > b_j, then inversion exists. Equivalent to: After sorting by a, check if the sequence of b is strictly increasing? Not necessarily strictly? For inversion we want strict >. But if b_i = b_j and a_i < a_j, then intersection at x2? Actually if b_i = b_j, then they intersect at x2 (since they share same y at x2). But if a_i < a_j and b_i = b_j, then intersection would be at x2? Let's verify: Two distinct lines with different slopes: if b_i = b_j, then they have same value at x2, so intersection at x2 exactly. So not strictly inside. So we need b_i > b_j (or <) but not equal. So indeed we need strictly decreasing. So we need a pair with a_i < a_j and b_i > b_j. So after sorting by a, we need to check if the sequence of b is not non-decreasing. Actually if there exists i b_j then inversion. If there exists i<j with b_i = b_j and a_i < a_j, then intersection at x2, not counted. So we need to check for a decreasing pair (strict). So the condition for "NO" is that for all i<j (with a_i < a_j), we have b_i < b_j. That means b is strictly increasing along increasing a. Actually if we sort by a, the b values must be strictly increasing to avoid any valid crossing inside. However what if there are ties in a? For a_i = a_j, we don't care about b because they are invalid anyway (crossing at x1). But could such ties cause a situation where after sorting by a, the b sequence has a drop but only due to ties? For example, suppose we have lines: line1: a=0, b=10; line2: a=0, b=5; line3: a=1, b=8. Sorting by a (tie break arbitrarily) could give order: line1 (0,10), line2 (0,5), line3 (1,8). Then we check pairs: (line1,line2): a equal -> ignore. (line1,line3): a1=0 < a3=1, b1=10 > b3=8 => inversion! But is that a valid crossing inside? Let's compute: line1 and line3: a1 < a3, b1 > b3. That suggests f1(x1) < f3(x1)? Actually a_i = f_i(x1). If a1=0, a3=1, then at x1, line1 has value 0, line3 has 1, so f3(x1) > f1(x1). At x2, b1=10, b3=8, so f1(x2) > f3(x2). So they cross between x1 and x2 => yes, there is intersection strictly inside. But wait, line1 and line3 are valid lines with a1 != a3. So that would be a YES. However, could this happen even if the actual correct condition (ignoring ties) yields NO? Let's see: Among lines with equal a values (i.e., same y at x1), they all intersect at x1. But they can have different slopes. Among them, some might have higher b (value at x2) than others. For a line with a equal to some value and another line with larger a, it's possible that the line with larger a (at x1) could be above or below. If there is any line with the same a, but we choose an ordering that causes inversion, does that correspond to a valid pair with a_i < a_j? Yes, because if there exists any line with a larger value, then it's fine. But we must ensure that the inversion is not due to a pair where a_i = a_j. In our example, the inversion is between line1 (a=0) and line3 (a=1). That's valid because a1 < a3. So if there is at least one line with a smaller a and a line with larger a such that b_i > b_j, that is valid. So the presence of multiple lines with same a does not invalidate that. In the sorting, if we break ties arbitrarily, we might have a situation where for lines with same a, they are placed in some order. When scanning, we maintain the maximum b among lines with same a? Actually careful: The condition to check if there is any inversion can be done by sorting by a, and while scanning, we track the minimum b seen so far? But we need to detect if there exists a previous line with smaller a and larger b. Typically, we can sort by a ascending, and then check if the sequence of b is increasing. But if there are ties in a, the order of b among those ties might affect the monotonicity. For example, suppose actual lines: lineA: a=0,b=5; lineB: a=0,b=10; lineC: a=1,b=6. Sorted by a, with ties broken arbitrarily: order: lineB (0,10), lineA (0,5), lineC (1,6). Then b sequence: 10,5,6 -> there is inversion: 10>5? But that's among equal a? (lineB,lineA): a equal, ignore. Then (lineB,lineC): aB=0 < aC=1, bB=10 > bC=6 => inversion valid. So YES. But now consider if we sorted as lineA (0,5), lineB (0,10), lineC (1,6). b:5,10,6 -> after sorting, pair (lineB,lineC): aB=0 < aC=1, bB=10 > bC=6 -> inversion. Still YES. So regardless of tie-breaking, if there exists a valid pair with a_i < a_j and b_i > b_j, then in the sorted order by a, there will exist some i<j with a_i < a_j (since ties might be grouped, but among different a groups, the groups are ordered; if we pick the minimal b among the earlier group and the maximal b among later group? Actually to guarantee that some inversion appears as a descent in the b sequence, we need to be careful. Suppose we have lines: group A: a=0 with b values: 10,20; group B: a=1 with b values: 15,25. The valid pair: a=0,b=20 and a=1,b=15 yields inversion. In sorted order, if we put b values in group A as [10,20] and group B as [15,25], then b sequence: 10,20,15,25. There is a descent from 20 to 15, i<j with a_i < a_j? 20 (i=2) and 15 (j=3): a_i=0? Actually group B lines have a=1, so a_i for i=2 is 0, for j=3 is 1, so a_i < a_j, b_i > b_j, inversion. So detected. But if we reorder within groups: e.g., group A: 20,10; group B: 25,15 => b:20,10,25,15. Then inversion: from 20 to 10 (both group A, equal a, ignore), from 10 to 25 (a=0 to a=1, b increases, ok), from 25 to 15 (a=1 to a=1? Actually 25 is from group B? Wait ordering: after sorting by a, all group A then group B. So if we put group A: [20,10] then group B: [25,15], then indices: i=1 (a=0,b=20), i=2 (a=0,b=10), i=3 (a=1,b=25), i=4 (a=1,b=15). Check pairs across groups: (i=1,j=3): a1=0< a3=1, b1=20 > b3=25? No, 20 < 25, so no inversion. (i=2,j=3): a2=0< a3=1, b2=10 < 25, no inversion. (i=1,j=4): a1=0< a4=1, b1=20 >15 -> inversion! So detected. So appears any valid pair will manifest as some inversion in the sequence after sorting by a, regardless of tie-breaking, because you can pick the i corresponding to the line with larger b from the earlier group and the j with smaller b from the later group. However, it's not guaranteed that in the ordering, the particular pair i<j will be in that order; but since all lines from earlier group appear before later group, the larger b from earlier group could appear at some position i, and the smaller b from later group could appear at some position j>i. So indeed there will exist i<j with a_i < a_j and b_i > b_j. So we can simply sort by a, and then check if there exists any inversion in b (i.e., b not strictly increasing). However, if there is a pair with a_i < a_j and b_i > b_j, we will detect it. But also we might get false positive due to equal a? No because we ignore equal a pairs. But if we simply check if there is any i b_j, we might count a pair where a_i = a_j, which would be invalid. However if we sort by a and then check for b_i > b_j, we must skip pairs with equal a. But if we simply scan and keep track of the maximum b seen so far among previous groups with strictly smaller a, we can avoid that. Approach: sort by a ascending, and for equal a, we need to treat them carefully: we should only compare b of current line with the maximum (or minimum?) b among previous lines with strictly smaller a. Actually we want to know if there exists a previous line with a strictly less and b > current b. So we can sort, and while scanning in order of increasing a, maintain the maximum b seen among all lines processed so far (but only those with a less than current a? Since scanning in non-decreasing a, when we encounter a new line, all previous lines have a <= current a. But if some previous lines have a equal to current a, they should not be considered because a_i = a_j not allowed. So we need to only compare with lines whose a is strictly less. So we can handle groups: For each group of lines with same a, we first compute for each line in the group, check if there exists any previous line (from earlier groups) with b_max > current b (since if b_max > current b, then that previous line had a smaller a and larger b => inversion). Then after checking all lines in the group, we update the global max b to include the b values from this group (maybe the maximum b of the group). Actually to find inversion, we need existence of any pair: if we have a line with a smaller a and larger b, that means for current line (with larger a) we want some previous line with b > current b. So we can maintain the maximum b seen so far among all previous groups (strictly smaller a). Then for each line in the current group (all have same a), if max_b_prev > b_current, then inversion found. After processing all lines in the group, we update max_b_prev to max(max_b_prev, max_b_in_group). Since we only need to detect existence, we can return YES if any such inversion found.
But also what about the reverse? Could there be inversion where the line with smaller a has smaller b but line with larger a has larger b? That's not inversion. So we only need to detect the case where b decreases as a increases.
Alternatively, we could sort by a and then check if b sequence is not strictly increasing. If we have ties in a, we could break ties by b descending? Let's think: If we sort by a ascending, and for equal a, we sort by b ascending? Then the b sequence would be non-decreasing for lines within same a group. Then if there is a valid inversion, it would appear as a drop when moving from one group to the next. But if we sort equal a by b ascending, then within group, b is increasing. Then moving to next group, suppose the maximum b in previous group (since they are sorted ascending, the last element in previous group has the largest b in that group). Then if the first element of next group has b less than that max, then we will see a drop at the boundary. However the first element of next group may not be the smallest b in that group; it is the smallest b in that group (since sorted ascending). But if there is any element in previous group with b > some element in next group, then particularly the largest b in previous group is > that element. Since the last element of previous group is the maximum, and the first element of next group is the minimum, if the maximum > minimum then there exists a drop? Actually we need existence of any pair: if the maximum previous b > some current b, then inversion exists. The drop from max previous to min current is the most likely place for inversion. But it's possible that max previous <= min current, but still there might be some previous b > some later b? If max previous <= min current, then for all previous b <= min current <= any b in current group? Actually if max previous <= min current, then all previous b are ≤ min current. But the later group may have some b smaller than min? No, min is the smallest; so all later b are >= min current, thus all later b are >= min current ≥ max previous? Actually if max previous <= min current, then all previous b <= all later b? Not necessarily; later group may have some b smaller than some previous b if there is variation within later group? Since min later is the smallest, all later b >= min later. If max previous <= min later, then for any later b, b >= min later >= max previous, so later b >= max previous, thus no previous b > later b. So no inversion. So checking max_prev > min_current is sufficient to detect if there exists any inversion across groups. However what about inversion within the same group? That would be equal a, not allowed. So we ignore. Therefore algorithm: Sort lines by a (value at x1). Then for each line in order, we want to check if there is a previous line with a strictly less and b > current b. To implement efficiently, we can group by a. Maintain max_b_sofar (maximum b among all lines processed from previous groups, i.e., with a < current a). For the current group (all lines with same a), we need to compare each b to max_b_sofar. If any b < max_b_sofar, then inversion found -> YES. After processing the entire group, update max_b_sofar = max(max_b_sofar, max b in this group). Continue. This runs in O(n log n) after sorting.
But also we must consider possibility that inversion could be with a_i > a_j and b_i < b_j? That's symmetric; we are sorting by a ascending, so we only detect cases where earlier (smaller a) has larger b. The other case (larger a has larger b and smaller a has smaller b) is not inversion; but the sign change condition requires opposite ordering, which can be either direction. But if we sort by a ascending, we cover one direction? Let's see: Suppose we have two lines with a_i > a_j and b_i < b_j. That is also a valid inversion because at x1, line i has larger a, at x2, line i has smaller b. That pair would satisfy a_i > a_j and b_i < b_j. In our sorted order ascending by a, we would have j (smaller a) before i (larger a). Then condition for inversion in our scanning (previous smaller a with larger b) would be: for i (larger a) with b_i, we check if there exists previous line with a smaller and b > b_i. That is exactly the case: j has a smaller a, and if b_j > b_i, then we detect inversion. So scanning from small a to large a, we detect all cases where there is a line with larger a having smaller b than some line with smaller a. That's exactly the condition for a valid intersection (since the ordering flips). So we are covering all valid pairs.
Thus the algorithm seems correct.
But there is nuance: The condition (a_i - a_j)*(b_i - b_j) < 0 is equivalent to the existence of an inversion between the sequences sorted by a and b. The algorithm described is a standard way to check if there exists an inversion.
However, careful: a_i and b_i are computed using floating point? They are integers up to 1e6, and x1,x2 up to 1e6, so products up to about 1e12, within 64-bit signed integer range. So we can compute exactly as 64-bit integers. But we need to sort by a, which is integer.
But what about the intersection being strictly inside? Our condition a_i < a_j and b_i > b_j ensures intersection x' is between x1 and x2? Let's verify rigorously.
Given two lines i,j with slopes ki and kj, ki != kj. Compute x0 = (bj - bi)/(ki - kj). Condition x1 < x0 < x2. Let's express in terms of a_i, a_j, b_i, b_j. We have:
a_i = kix1 + bi
a_j = kjx1 + bj
b_i = kix2 + bi
b_j = kjx2 + bj
Now, we want to see when x0 lies between x1 and x2. Without loss, assume ki > kj. Then denominator positive. Then x0 - x1 = (bj - bi)/(ki - kj) - x1 = (bj - bi - (ki - kj)x1)/(ki - kj) = ((kjx1 + bj) - (kix1 + bi))/(ki - kj) = (a_j - a_i)/(ki - kj). Similarly, x2 - x0 = x2 - (bj - bi)/(ki - kj) = ((ki - kj)x2 - (bj - bi))/(ki - kj) = ((kix2+bi) - (kjx2+bj))/(ki - kj) = (b_i - b_j)/(ki - kj). So the signs of (a_j - a_i) and (b_i - b_j) determine if x0 is between. For x1 < x0 < x2, we need (x0 - x1) > 0 and (x2 - x0) > 0, i.e., (a_j - a_i) and (b_i - b_j) have same sign as denominator? Actually if ki > kj, denominator positive, then we need a_j - a_i > 0 and b_i - b_j > 0. That means a_j > a_i and b_i > b_j. So if ki > kj, then a_i < a_j and b_i > b_j. If ki < kj, denominator negative, then for x0 - x1 = (a_j - a_i)/(negative) > 0 => a_j - a_i < 0 => a_i > a_j. And x2 - x0 = (b_i - b_j)/(negative) > 0 => b_i - b_j < 0 => b_i < b_j. So in both cases, the ordering of a and b is opposite: one is larger, the other smaller. More succinctly, (a_i - a_j) and (b_i - b_j) have opposite signs.
Thus condition a_i < a_j and b_i > b_j (or a_i > a_j and b_i < b_j) is exactly equivalent to the intersection lying strictly inside, assuming lines are not parallel.
But what about degenerate case where a_i = a_j or b_i = b_j? Those give intersection on border. We exclude them because we need strict inequalities.
Thus the algorithm is correct.
Now implementation details:
Given n up to 100k, we compute pairs (a, b) as long integers (64-bit). But note that k, b, x can be up to 1e6 in absolute, so a = k*x1 + b. Max |k| * |x1| up to 1e6 * 1e6 = 1e12, plus b up to 1e6 => up to around 1e12+1e6, fits in 64-bit signed (9e18). So we can use int64_t.
Sorting by a: we need to group equal a. Since a can be large, but we can sort the array of structs by a. Then process in order.
Pseudo:
struct Line {
long long a, b;
};
vector L(n);
for i in 0..n-1:
read ki, bi
L[i].a = ki * x1 + bi
L[i].b = ki * x2 + bi
sort L by a ascending.
Initialize max_b_prev = -inf (or maybe not set). We'll process groups.
i = 0
while i < n:
j = i
while j < n and L[j].a == L[i].a:
j++
// group [i, j-1] all same a.
// First check each element in group against max_b_prev (if i>0)
if i > 0:
for k = i to j-1:
if L[k].b < max_b_prev:
print "YES" and exit.
// Then compute max_b_in_group = max(L[k].b for k in group)
// Update max_b_prev = max(max_b_prev, max_b_in_group)
// Set i = j
After loop, print "NO".
But note: we need to consider also the case where inversion could be within groups? Not valid, so ignore.
Edge Cases: What about lines with same a but different b, and there is inversion across groups? Our group check uses max_b_prev from earlier groups. That is correct. But what about inversion where the line with smaller a appears later in the sorted order because we sorted ascending? That can't happen; sorted ensures smaller a first. So fine.
But there is also possibility that inversion could be between lines where both have distinct a but the pair ordering is such that a_i < a_j and b_i > b_j, but the b_i is not the maximum among previous group, and the b_j is not the minimum among its group? Our approach checks each b_j against max_b_prev. If there exists some i (with a_i < a_j) and b_i > b_j, then max_b_prev (which is the maximum b among all lines with a < a_j) must be >= b_i > b_j, so max_b_prev > b_j (strictly? Could be equal? If b_i > b_j, then max_b_prev >= b_i > b_j, so max_b_prev > b_j. So we would detect. So it's sufficient.
Thus O(n log n) sorting and O(n) scan.
But is there a case where a_i < a_j, b_i > b_j, but b_i equals max_b_prev? That's fine, max_b_prev > b_j as b_i > b_j. So detection works.
Now sort by a:
line2 (1,2), line3 (1,1), line4 (2,2), line1 (3,4)
Groups:
a=1 group: (1,2) and (1,1). max_b_prev initially? i=0, group first: i>0 false, so no check. Then update max_b_prev = max(-inf, max in group = 2) => max_b_prev=2.
Next group a=2: group includes (2,2). Check: i>0 true, for element b=2, compare to max_b_prev=2: b < max_b_prev? 2<2 false. So no inversion. Update max_b_prev = max(2,2)=2.
Next group a=3: (3,4). Check: b=4 < max_b_prev=2? false. Update max_b_prev= max(2,4)=4.
Result: No inversion -> NO. Output matches.
Example2:
n=2, x1=1,x2=3
lines:
1: k=1,b=0 => a=1, b=3
2: k=-1,b=3 => a= -11+3=2? Actually -11+3=2, b= -13+3=0
Wait compute: line2: k=-1,b=3 => a = -11+3=2, b = -1*3+3=0
So points: (1,3) and (2,0). Sort by a: line1 (1,3) then line2 (2,0)? Actually a values: 1 and 2, so line1 a=1, line2 a=2. So order: line1 (1,3), line2 (2,0). Scan: max_b_prev after first group (a=1) = 3. Next group a=2: b=0 < 3 => inversion => YES. Output YES matches.
Example3:
n=2, x1=1,x2=3
lines:
1: k=1,b=0 => a=1, b=3
2: k=0,b=2 => a=01+2=2? Actually 01+2=2, b=0*3+2=2
So a1=1,b1=3; a2=2,b2=2. Sort by a: line1 (1,3), line2 (2,2). Scan: max_b_prev after first group =3; second group b=2<3 => YES. Output YES matches.
Example4:
n=2, x1=1,x2=3
lines:
1: k=1,b=0 => a=1,b=3
2: k=0,b=3 => a=3? 01+3=3, b=03+3=3
So a1=1,b1=3; a2=3,b2=3. Sort: line1 (1,3), line2 (3,3). Scan: max_b_prev=3; second group b=3 < 3? false. So NO. matches.
Thus algorithm works.
But wait: In example1, there was an intersection at x=1? Actually lines 2 and 3 have a=1 (same), so they intersect at x=1 (border). And lines 1 and 4: compute intersection? line1: y=x+2, line4: y=2 (since k=0,b=2). Intersection at x+2=2 => x=0, which is outside left. Also line1 and line2: both slope 1, parallel, no intersection. So no intersection strictly inside. Good.
Now check for other edge cases: Intersection exactly at x2 but not inside: we want strictly inside, so our strict inequality avoids that. In example above where b_i = b_j and a_i < a_j, we don't count. So good.
Now what about lines with same a but different slopes? They intersect at x1. Our algorithm ignores them because we don't check across groups when a equal. So fine.
But there is another subtlety: Could there be an intersection strictly inside even if a_i and a_j are equal? No, because if a_i = a_j, then they have same y at x1. If slopes differ, they intersect at x1 exactly. So not strictly inside.
Could there be intersection strictly inside even if b_i = b_j? Similarly, they intersect at x2 exactly.
Thus condition is exactly as we have.
Now complexity: Sorting O(n log n). n=100k, fine.
Memory: store array.
But can we do better? Not needed.
Now we need to be careful about integer types: long long in C++.
Potential pitfalls: The values can be negative, so max_b_prev initial should be something like -9e18, but we can initialize with a very small number, or set to a sentinel and handle first group properly.
One more nuance: When there are many lines with same a, we might need to consider that within the same group, there could be a line that has b less than max_b_prev, which we check; that's valid. But also, after updating max_b_prev with the group's max, we might lose information about the fact that within the group, there might be lines with lower b that we already checked. But that's fine.
Alternatively, we can sort by a and then just check if the sequence of b is strictly increasing, ignoring ties in a? But as argued, ties in a cause b values that may be not monotonic, but they are irrelevant for valid pairs. However, if we just sort by a ascending, and then for all i from 0 to n-2, if a_i < a_{i+1} and b_i > b_{i+1}, then we get inversion. But what if the inversion occurs not between adjacent indices but further apart? For example, a sorted order: (1,100), (2,90), (3,80) would show adjacent inversions? Actually (1,100) and (2,90): a1< a2, b1 > b2, so inversion. So adjacent will catch if b is decreasing. But what about (1,100), (2,200), (3,50)? Here (2,200) vs (3,50): a2< a3, b2 > b3 => inversion, caught adjacent. But consider (1,100), (2,200), (3,150), (4,50): Adjacent: (1,100) vs (2,200): b increases, okay; (2,200) vs (3,150): b decreases, a2< a3, b2 > b3 => inversion caught. So seems any decreasing will cause at least one adjacent decrease? Not necessarily if there are ties in a: if there is a group with same a, you could have (1,100), (1,90), (2,80) – the inversion between (1,100) and (2,80) might be detected by comparing (1,90) vs (2,80) which is a_i < a_j? Actually (1,90) has same a as (1,100) but when sorted, both have a=1, but for adjacent indices i=1 (first of group) and i=2 (second of group), a are equal, so we ignore because condition requires a_i < a_j. But the valid pair (first element with a=1,b=100) and (2,80) is not adjacent if we have (1,90) in between. However, if we check only adjacent, we might miss it because the adjacent pair (1,90) vs (2,80) has a_i =1, a_j=2, b_i=90, b_j=80, and that's a valid inversion (since 1<2 and 90>80) and indeed (1,90) and (2,80) is also a valid pair. But is it guaranteed that there exists at least one adjacent pair with a_i < a_j and b_i > b_j if there is any such pair across groups? Let's think: Suppose we have two groups: group A (a=A) and group B (a=B) with A<B. Suppose there exists a line in A with b = b_a and a line in B with b = b_b such that b_a > b_b. If we sort lines within group A arbitrarily, and within group B arbitrarily, the adjacent pairs across groups will be between the last element of A and the first element of B. If we sort group A by b descending (largest first) and group B by b ascending (smallest first), then the last element of A (the smallest b in A) might be less than the first element of B (the smallest b in B). Actually we can contrive: Suppose group A has b values: 100, 90; group B has b values: 80, 70. If we sort A ascending (90,100) and B ascending (70,80). Then across groups: the last of A is 100, first of B is 70, so adjacent pair (A last, B first): a_A < a_B, b_A=100 > b_B=70, inversion detected. If we sort A ascending (90,100) and B descending (80,70), then across: last of A 100, first of B 80 => 100>80 detection. If we sort A descending (100,90) and B ascending (70,80): last of A 90, first of B 70 => 90>70 detection. If we sort A descending (100,90) and B descending (80,70): last of A 90, first of B 80 => 90>80 detection. So it seems that across groups, the last element of A and first element of B always have a_i < a_j; and if there exists any pair with b_a > b_b, then specifically the maximum b in A is > some b in B; but that does not guarantee that the minimum b in B is less than the maximum b in A? Actually we need to guarantee that the last element of A (which depends on sorting within A) and the first element of B (depending on sorting within B) will satisfy b_last_A > b_first_B if there exists any b_a > b_b. Is it always true? Let's try to find a counterexample: Suppose group A: b values: 100, 60. Group B: b values: 70, 50. There exists a pair: (A:100) > (B:50) and also (A:100) > (B:70). However, if we sort A ascending: [60,100], B ascending: [50,70]. Then last of A=100, first of B=50 -> 100>50 detection. If we sort A ascending [60,100] and B descending [70,50]: last of A=100, first of B=70 -> detection. If we sort A descending [100,60] and B ascending [50,70]: last of A=60, first of B=50? Actually with A descending: [100,60], B ascending: [50,70] => last of A=60, first of B=50 => 60>50 detection. If A descending [100,60], B descending [70,50] => last of A=60, first of B=70 => 60 > 70? false. In this case, the last element of A is 60, first of B is 70. There is no adjacent inversion. Yet there exists a valid pair: from A: 100 > 50 (and also 100 > 70). But the pair (A:100, B:50) is not adjacent; however there is also pair (A:60, B:50) which is valid? Check: A:60 (a=A) and B:50 (a=B) has b_A=60 > b_B=50, and a_A < a_B, so that is a valid pair. But is (A:60, B:50) adjacent? In the ordering A descending [100,60], B descending [70,50], the order is: index0:100, index1:60, index2:70, index3:50. Adjacent pairs: (100,60): a equal? both A? Actually 100 and 60 both belong to group A, so a equal, ignore. (60,70): a_i = A (since 60 is last of A), a_j = B (since 70 is first of B). So (60,70): a_i < a_j? Yes (A<B). b_i=60, b_j=70 => 60 > 70? false, 60 < 70, so not inversion. (70,50): both in group B? Actually 70 and 50 both in B, a equal, ignore. So no adjacent inversion. However, there exists a non-adjacent valid pair: (60,50) or (100,50) or (100,70). Specifically (60,50) is a valid pair: a_i = A (60 from A), a_j = B (50 from B) and b_i=60 > b_j=50. But they are not adjacent: indices 1 and 3. So if we only check adjacent pairs, we would miss it. So we cannot rely on adjacent only.
Therefore the scanning with max_b_prev approach is safe because it checks all lines against the maximum b from previous groups.
Thus the algorithm I described earlier is correct.
But maybe we can also sort by a and then compute the minimum b suffix or something. But scanning with maintaining max is easier.
Now, any subtlety about integer sign? The condition b_i > b_j is strict; we use b < max_b_prev to detect inversion. If b equals max_b_prev, then b_i == max_b_prev and b_j = b; equality is not a valid inversion. So we need strict less.
But there is nuance: Suppose max_b_prev is exactly equal to b_j (current b). That would correspond to a previous line with b_i = b_j and a_i < a_j; that would give intersection at x2, not valid. So we must not count equality. So condition b_j < max_b_prev is correct.
Now what about the case where the maximum b from previous groups is from a line that has the same a as the current group? That's impossible because we only update max_b_prev after finishing processing the group. When processing group, we first check against max_b_prev which only contains lines from strictly smaller a. So good.
Now, what about multiple groups? The algorithm works.
But is there a possibility of overflow when computing a and b? Using 64-bit is fine. However, we need to be careful with multiplication: k * x1 may be up to 1e6 * 1e6 = 1e12, but k and x1 could be negative, so product can be as low as -1e12. Sum with b up to ±1e6, so total range about -1e12-1e6 to 1e12+1e6. That fits within signed 64-bit (range ±9e18). So int64_t (long long) is safe.
Now let's consider another approach: Perhaps we can avoid sorting and use geometry? But sorting is fine.
Now test some more cases.
Case: n=2, k1=0,b1=0, k2=1,b2=0. x1=-1,x2=1. Lines: y=0 and y=x. Compute a1 = 0*(-1)+0=0, b1 = 01+0=0. a2 = 1(-1)+0=-1, b2 = 1*1+0=1. Sorted by a: line2 a=-1,b=1; line1 a=0,b=0. Scan: first group a=-1: max_b_prev after group =1. Next group a=0: b=0 < max_b_prev=1 => inversion YES. Intersection at x=0, which is between -1 and 1? Actually intersection of y=0 and y=x is at x=0, which is inside. So YES, correct.
Case: intersection exactly at x1: x1=0,x2=1, lines: y=x and y=2x. Actually both pass through (0,0). Compute a1=0,b1=0? Wait: y=x: k=1,b=0 => a=10+0=0, b=11+0=1. y=2x: k=2,b=0 => a=20+0=0, b=21+0=2. Sorted: both a=0. Groups: first group a=0 with b=0? Actually compute: line1 a=0,b=1; line2 a=0,b=2. So group a=0. max_b_prev initially? i=0 group first, no check. Then update max_b_prev=2. No more groups. So no inversion => NO. Intersection at 0 (x1) not counted. Good.
Case: intersection at x2: x1=0,x2=1, lines: y=0 and y=1-x? Actually to have intersection at x2=1, need lines such that at x=1 they are equal. Example: y=0 (k=0,b=0) and y=1-x (k=-1,b=1). At x=1, both y=0. At x=0, y=0 and y=1. Compute a1: 00+0=0, b1=01+0=0. a2: -10+1=1, b2: -11+1=0. Sorted by a: line1 a=0,b=0; line2 a=1,b=0. Scan: first group a=0: max_b_prev=0. Next group a=1: b=0 < max_b_prev=0? false. So NO. Good.
Case: intersection strictly inside but a values descending? Already handled.
Now, we need to ensure that lines with same a but different b, and there is a line with larger a but b smaller than some of those a-equal lines, inversion will be detected. Good.
Now think about performance: O(n log n) with n=100k is fine.
Now confirm constraints: n up to 100000, so memory fine.
Now corner case: All lines have same a? Then no group after first, no inversion. Correct.
Now could there be a valid intersection that doesn't appear as inversion? Let's try to find a counterexample. Suppose we have three lines: L1: a1=0, b1=10; L2: a2=1, b2=5; L3: a3=2, b3=6. Sorting by a: (0,10),(1,5),(2,6). Inversion exists? Check pairs: (0,10) vs (1,5): 0<1, 10>5 => valid. So YES. Scanning: after first group max_b=10; second group (1,5): b=5<10 => detection. So fine.
What about (0,10),(1,9),(2,8)? detection.
What about (0,5),(1,10),(2,3)? Here valid pairs: (1,10) vs (2,3): a1< a2? Actually (1,10) and (2,3): a1=1< a2=2, b1=10>3 => valid. Also (0,5) vs (2,3): 0<2, 5>3 => valid. Scanning: after first group (0,5): max_b=5; second group (1,10): b=10<5? false; then update max_b = max(5,10)=10; third group (2,3): b=3<10 => detection. So fine.
What about (0,5),(1,6),(2,4)? Valid: (1,6) vs (2,4): 1<2,6>4 => valid. Scanning: after (0,5) max_b=5; (1,6): b=6<5? false; update max_b=6; (2,4): b=4<6 => detection.
Thus detection works.
Now consider scenario where the inversion exists but only between lines that are not in consecutive groups? Our method with max_b_prev works because it maintains the global maximum b from all previous groups, regardless of distance.
Thus algorithm stands.
But wait: there is also possibility of lines with same a but one has b less than max_b_prev, and that's a valid inversion if max_b_prev comes from a previous group. We check each line in the group against max_b_prev, so that is covered.
Now we also need to handle negative values. max_b_prev initialization could be something like -10**18. Since b can be as low as -1e12-1e6, we can use LLONG_MIN or -1e18. But careful: if we initialize max_b_prev = -inf, for first group (i==0) we don't check, then after group we set to max_b_in_group. So no problem.
Now we should also consider alternative approach: Use sorting by a and then compute prefix maximum of b. Then for each line, compare its b to the maximum b among earlier lines with smaller a. But because of ties, we need to ensure we only use earlier lines with strictly smaller a. So we can sort by a, and for each line, we want to know if there exists earlier line with a < current a and b > current b. To do this, we can process lines in order of increasing a, and maintain the maximum b seen so far for all lines with a strictly less than current a. The grouping approach accomplishes that.
But could we also simply sort by a and then iterate, and for each line i from 0 to n-1, if i>0 and a[i] > a[i-1] (meaning strictly greater), then we can update a global maximum b among all previous lines (including those with same a) before checking? Actually if we want to ensure we only consider lines with strictly smaller a, we could maintain a variable max_b_sofar that gets updated only after we have processed all lines with a given value? But if we process line by line, and we have two consecutive lines with same a, for the second line, the previous line with same a is not allowed. But if we have already updated max_b_sofar to include the first line's b, then when we process the second line, we would incorrectly consider that first line as having a smaller a? But they have same a, not smaller. So we need to not include lines with equal a. So we need to update max_b_sofar only after finishing the entire group. So grouping is safer. So we'll implement grouping.
Implementation details in code:
Read n, x1, x2.
Create vectors of pairs (a, b) with type long long.
For each line, compute a = kx1 + b; b_val = kx2 + b; push {a, b_val}.
Sort by a (first element).
Initialize long long max_b_prev = -4e18 (or LLONG_MIN).
Loop i from 0 to n-1:
start = i
while (i+1 < n && L[i+1].a == L[start].a) i++? Actually better: for i loop controlled by while, but we can use index variable.
Use int j = i;
while (j < n && L[j].a == L[i].a) j++;
// now group [i, j-1]
// first check: if i>0 (meaning there is previous group)
for k = i to j-1:
if (L[k].b < max_b_prev) { cout << "YES"; return 0; }
// then compute max_in_group = max(L[k].b) for k in group
long long max_in_group = L[i].b;
for k = i+1 to j-1: if (L[k].b > max_in_group) max_in_group = L[k].b;
// update max_b_prev = max(max_b_prev, max_in_group)
if (max_in_group > max_b_prev) max_b_prev = max_in_group;
// set i = j
i = j-1; // because outer loop will increment; so set i=j-1 and after loop increment, i becomes j.
After loop, print "NO".
But note: In C++ for loop with increment, we can do: for (int i = 0; i < n; ) { ... i = j; } without decrement.
Simplify: i = 0; while (i < n) { int j = i; while (j < n && L[j].a == L[i].a) ++j; // group [i, j-1] ... i = j; }
Now ensure we use long long.
Now we should test with random data to verify. But given reasoning, seems solid.
Now, there is a nuance: When we compute a = k*x1 + b, we use integer arithmetic; but slopes and intercepts are integers, x1, x2 integers. So a and b are integers. So sorting and comparisons are exact.
Now think about whether any pair with intersection exactly on the boundary could be mis-identified as valid? Our condition b_j < max_b_prev uses strict inequality. If there is a pair with a_i < a_j and b_i == b_j, then b_j == max_b_prev? Could max_b_prev be exactly b_i? Possibly. In that case, b_j < max_b_prev is false, so we don't count. Good.
But what if there is a pair with a_i < a_j and b_i > b_j, but the line with the maximum b in previous groups is not the same as the pair but the maximum b is exactly equal to b_j? Could that happen? Suppose we have lines: previous group: only one line with b = 5. Current group: two lines with b = 4 and b = 6? But we are checking each line individually: For line with b=4, 4 < max_b_prev=5 -> detection. For line with b=6, 6 < 5 false. So detection still occurs because at least one line in group will have b less than max_b_prev if there exists any line in group with b less than some previous b. However, it's possible that the only line with b less than some previous b is exactly equal to some previous b? That would be b_j = previous b, which is not less, so not detection. But that would correspond to intersection at x2? Let's consider: Suppose previous group has a line with b=5; current group has a line with b=5 as well, and also a line with b=6? But we need a valid pair: previous line with a smaller a and b=5, and current line with larger a and b=4? Wait, if the only line in current group with b less than 5 is nonexistent, but there is a line with b=5 (equal), then there is no pair with b_i > b_j? The pair (prev b=5, curr b=5) gives equality => intersection at x2 (if they have different slopes). That's not allowed. But what about other lines in previous group? If the previous group has max b=5, but also maybe another line with b=6? But max b is 6 then. So the condition using max_b_prev would be 6. So if the current group has b=5, then 5<6 detection. So only if the maximum b from previous groups is exactly equal to some b in current group, and all b in current group are >= that max, and there is no b strictly less, then no detection. But does that mean there is no valid pair? Let's try to construct: Suppose previous group lines: only one line Lp with a=1,b=5. Current group lines: Lc1 with a=2,b=5 (equal b), and Lc2 with a=2,b=6 (larger). Is there any pair (Lp, Lc) with a_p < a_c and b_p > b_c? b_p=5, b_c1=5 not >, b_c2=6 not >. So no valid pair. So indeed no intersection strictly inside. Good.
Now what about previous group having multiple lines: Lp1 a=1,b=5, Lp2 a=1,b=4 (same a). Then max_b_prev = 5. Current group: Lc a=2,b=5 (only). Then check: b_c=5 < max_b_prev=5? false. Is there a valid pair? Lp1 (b=5) and Lc (b=5) -> equality => intersection at x2 if slopes differ; Lp2 (b=4) and Lc (b=5) -> b_p=4 < b_c=5, so not >. So no valid pair. So no detection, correct.
Now what about previous group having lines with b values [5,3] and current group having b=4. max_b_prev =5, and b_c=4 <5 => detection. The valid pair could be Lp with b=5 and Lc with b=4 (since 5>4). Yes.
Thus the algorithm is correct.
Now, we should also consider the possibility of lines with the same a but with a line that has b less than max_b_prev from earlier groups. That detection is correct.
Now we should verify that the condition a_i < a_j and b_i > b_j is both necessary and sufficient for intersection strictly inside. But is there any scenario where lines have a_i < a_j and b_i > b_j but intersection lies exactly at x1 or x2? Let's check: If a_i < a_j, then at x1, line i is below line j (since a_i < a_j). At x2, b_i > b_j, so line i is above line j. So they cross between x1 and x2 (by intermediate value property for linear functions). Since both are linear, the crossing is unique and lies strictly between because the difference function is linear and changes sign. So yes, intersection is strictly between.
Now what about the case where a_i < a_j and b_i > b_j but the lines are parallel? They can't be parallel because slopes would be equal; then a_i < a_j would imply (kix1+bi) < (kix1+bj) => bi<bj; but then b_i > b_j would be (kix2+bi) > (kix2+bj) => bi>bj, contradiction. So slopes differ.
Thus condition exactly captures.
Now we have a solution.
But there may be an alternative perspective: Consider the lines as points (a,b) in plane. The condition a_i < a_j and b_i > b_j means that the points are in "inversion". So we are checking if the sequence of points sorted by a has b in decreasing order anywhere. Equivalent to checking if the permutation of b relative to a has an inversion.
Thus the problem reduces to checking for an inversion in an array of pairs. That's O(n log n) using a Fenwick tree if we compress b. But our method with scanning after sorting by a is O(n log n) due to sorting, but the scan is O(n). So overall fine.
Now confirm that we don't need to consider pairs with intersection outside but still satisfy the inversion? No, inversion implies intersection inside.
Now double-check the necessity: If there is an intersection inside, then we have a_i < a_j and b_i > b_j (or vice versa). So yes.
Thus algorithm is correct.
Now we just need to write code.
But wait: There's a subtlety: The condition a_i < a_j and b_i > b_j is derived assuming that x1 < x2. And we have x1 < x2. So it's fine.
Now note: The values of a and b are computed with x1 and x2. Could there be overflow in multiplication? Use 64-bit.
Now implement:
In C++:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
ll x1, x2;
cin >> n >> x1 >> x2;
vector<pair<ll, ll>> lines(n); // (a, b)
for (int i = 0; i < n; ++i) {
ll k, b;
cin >> k >> b;
lines[i].first = k * x1 + b;
lines[i].second = k * x2 + b;
}
sort(lines.begin(), lines.end()); // by a, then b? Actually default sort by first, then second. But we need to group by a only, order of b within group doesn't matter.
ll max_b_prev = -1e18; // sufficiently low
int i = 0;
while (i < n) {
int j = i;
while (j < n && lines[j].first == lines[i].first) {
j++;
}
// check
if (i > 0) {
for (int k = i; k < j; ++k) {
if (lines[k].second < max_b_prev) {
cout << "YES\n";
return 0;
}
}
}
// update max_b_prev
for (int k = i; k < j; ++k) {
if (lines[k].second > max_b_prev) {
max_b_prev = lines[k].second;
}
}
i = j;
}
cout << "NO\n";
return 0;
}
But we need to be careful: max_b_prev initial value: for the first group, i==0, we skip checking, then we update max_b_prev to the max b of that group. So initial can be anything, but we want it to be less than all b. Since b can be as low as -1e12-1e6 ≈ -1e12, we can set max_b_prev = -1e18 (or LLONG_MIN). But using LLONG_MIN might cause overflow? No, just comparison. But if we set to LLONG_MIN, and later update to actual values. So better: use LLONG_MIN from . Or -4e18.
Now, is there any case where we need to consider pairs where a_i > a_j and b_i < b_j? Our scan only considers lines with larger a relative to previous lines. That covers all cases because for any valid pair (i,j) with a_i < a_j and b_i > b_j, when we encounter the later line (with larger a), we will compare its b with max_b_prev which includes the earlier line's b (since earlier line had smaller a). So it covers.
Thus solution complete.
But can we do it without sorting? Maybe we could compute something like checking if the lines sorted by intercept? But sorting is fine.
Now verify with some tricky cases:
Case: n=3, lines all intersect at a single point inside the strip. For example, x1=0,x2=2, lines: y=x, y=2x? That intersect at 0 only. Not inside. Let's try: y=0, y=x, y=-x+2? Actually find three lines that all cross at (1,1): y=1, y=x, y=2x-1. Compute a and b: For x1=0,x2=2.
Line1: k=0,b=1 => a=1,b=1.
Line2: k=1,b=0 => a=0,b=2.
Line3: k=2,b=-1 => a=-1,b=3.
Sorted by a: line3 a=-1,b=3; line2 a=0,b=2; line1 a=1,b=1. Scan: first group a=-1, max_b_prev=3. Second group a=0: check b=2 < 3 => YES. So intersection exists (line3 and line2 intersect at x=1). Actually line2 and line3 intersect? Solve: x = (b3-b2)/(k2-k3) = (-1-0)/(1-2)=(-1)/(-1)=1, inside. So YES. So detection.
Now what about scenario where there are multiple lines, but the only crossing inside is between two lines that have the same a? That can't be, as argued.
Now what about scenario where the inversion is only present between lines that have a difference but the b comparison is not caught because max_b_prev might be from the same a? We process groups, so we only compare with lines from strictly smaller a. So if the inversion is between a line from group A and line from group B, and group B is after group A, we will catch when processing group B. So covered.
Now consider scenario where the inversion is between a line from group A and line from group B, but the line from group A is not the one that gives max_b_prev. However, we only need existence; if max_b_prev is not from that specific line, but still if there is any line in group A with b > b_B, then max_b_prev (maximum of all previous groups) will be at least that b, so it will be > b_B (since b_B < that b). So detection occurs. Good.
Thus algorithm works.
Now complexity: Sorting O(n log n). For n=100k, fine.
Now we might need to think about potential pitfalls: Input reading, large numbers, use 64-bit.
Now we produce final answer.
But wait: The problem statement says "determine whether there is at least one point of intersection of two of these lines, that lays strictly inside the strip". Could there be a scenario where two lines intersect at a point that is inside the strip, but the ordering of a and b does not produce an inversion? Let's re-derive to be absolutely certain.
Given two lines i and j, with slopes ki and kj distinct. Compute x0 = (bj - bi)/(ki - kj). Condition: x1 < x0 < x2.
For x0 to be strictly between, both differences must be positive. So (a_j - a_i) and (b_i - b_j) must have the same sign as (ki - kj). Let's analyze sign.
Let D = ki - kj.
Case D > 0: Then need a_j - a_i > 0 => a_j > a_i, and b_i - b_j > 0 => b_i > b_j.
Case D < 0: Then need a_j - a_i < 0 => a_j < a_i, and b_i - b_j < 0 => b_i < b_j.
Thus either (a_i < a_j and b_i > b_j) or (a_i > a_j and b_i < b_j). So indeed.
Now, what about the possibility that a_i = a_j or b_i = b_j? That leads to equality of one of the expressions, making x0 = x1 or x2 respectively. So not inside.
Thus necessary and sufficient.
Hence the inversion condition is correct.
Now we should also consider if there is any hidden assumption about the lines being non-vertical? They are given as y = kx + b, so they are not vertical, fine.
Now implementation done.
But wait: The algorithm described uses sorting by a. However, we might also need to consider if there is any pair with a_i < a_j but b_i = b_j? That's not inversion, we ignore. But could it be that such pair still has intersection inside? Let's test: If b_i = b_j but a_i < a_j, then at x2 they are equal, so intersection at x2. So not inside. So ignore.
Now what about lines that are the same? Problem says all lines are pairwise distinct, so no identical lines.
Now we have a solid solution.
Now I'll produce final answer.
But I'm going to double-check with random tests mentally.
Let's generate some random lines and compute intersection. Write a small script mentally? Not needed.
Now, consider the scenario where many lines have the same a. Our grouping loop may be O(n^2) if we have many groups? Actually the inner loops within groups sum to total n, so O(n). So fine.
Now we might also use an alternative: after sorting by a, we could find the minimum b in a suffix and compare to maximum b in prefix, but grouping ensures strictness.
Thus final code as above.
Now we need to output "Yes" or "No"? The problem statement says "Yes" and "No" in output examples: "YES" and "NO"? Actually examples use "NO", "YES". In the problem statement: Print "Yes" (without quotes), but examples show "NO" and "YES". The sample output: "NO" in first, "YES" in others. The problem statement says "Print "Yes" (without quotes), if there is at least one intersection... Otherwise print "No" (without quotes)." But examples use uppercase? In the provided examples, they wrote "NO" and "YES". Actually the text: "Output Print "Yes" (without quotes) ..." but then the examples show "NO" and "YES". Probably they want exactly "Yes" and "No" case-insensitive? But typical Codeforces problems output exactly "YES" or "NO". Let's check the sample:
Sample Input 1:
4
1 2
1 2
1 0
0 1
0 2
Output:
NO
So they used uppercase. The problem statement might have a typo. I'll output "YES" or "NO" as per examples.
Thus final code output "YES" or "NO".
Now we can finalize.
But one more nuance: We assumed that the ordering of a is the key. However, what if there are lines with extremely large a values and we sort, that's fine.
Now think about potential pitfalls: If we use default sort on vector of pairs, it sorts by first then second. That's fine. For grouping, we only care about first being equal.
Now what if we have duplicate a but different b? We treat them as group.
Now we should consider the possibility of overflow when computing a = k*x1 + b. For k up to 1e6, x1 up to 1e6, product up to 1e12, b up to 1e6, sum up to ±1e12±1e6, fits in 64-bit. For negative, similar.
Now finalize.
Now I think we are done.
But wait: Could there be an intersection inside but a_i = a_j? As we argued, no. But what about the case where the intersection is exactly at x1 but due to rounding? Not applicable, integers.
Thus answer.
Now we can write the solution.
But perhaps there is an even simpler method: Sort by a, then compute the minimum suffix b and compare to prefix maximum b. However, grouping is simpler.
We'll implement as described.
Now I will produce the final answer with explanation.
But before concluding, let's ensure that we haven't missed the case where the intersection point is inside but the lines have slopes such that the denominator D is zero? That's parallel, no intersection.
Now one more test: n=2, x1=-1000000, x2=1000000, k1=1000000, b1=0, k2=-1000000, b2=0. Then a1 = 1000000 * (-1000000) + 0 = -1e12, b1 = 10000001000000 = 1e12. a2 = -1000000(-1000000)+0 = 1e12, b2 = -1000000*1000000+0 = -1e12. Sorted by a: a1=-1e12,b1=1e12; a2=1e12,b2=-1e12. Scan: first group max_b=1e12; second group b=-1e12 < 1e12 => YES. Intersection: solve: (0-0)/(2e6?) Actually slopes 1e6 and -1e6, intersection at x = (0-0)/(1e6 - (-1e6))? Actually (b2 - b1)/(k1 - k2) = (0-0)/(1e6 - (-1e6)) = 0/2e6 = 0, which is inside. So YES.
Now test negative large numbers: fine.
Thus code should work.
Now let's think about memory usage: vector of pairs, n=100k, each pair 2*8 bytes = 1.6MB, fine.
Now we produce final answer.
But note: The problem might be from Codeforces Round # something and the intended solution uses sorting and checking for inversion. Indeed there's a known problem "Anton and Lines" maybe.
Now, is there any chance that the condition could be simplified to checking if the sequences (kix1+bi) and (kix2+bi) are similarly sorted? Yes, as we did.
Now, I'll finalize.
But before finalizing, I want to consider if there is any subtlety about using integer comparisons versus using rational comparisons. The intersection x0 = (bj - bi)/(ki - kj) may not be an integer; but our condition using a and b only uses the sign of differences, not the actual value, so it's safe.
Now code.
Now, we could possibly simplify by sorting by a and then checking if there exists i<j with a_i < a_j and b_i > b_j by using a Fenwick tree to count inversions where b_i > b_j, but ignoring equal a. However, our method is simpler.
Now, just to be thorough, we could also consider a scenario where there is a line with a very large positive a and a very large negative b, etc. Works.
Thus final answer.
Now I'll produce final solution in C++.
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
code
int n;
ll x1, x2;
cin >> n >> x1 >> x2;
vector<pair<ll, ll>> vals(n); // (a = k*x1 + b, b_val = k*x2 + b)
for (int i = 0; i < n; ++i) {
ll k, b;
cin >> k >> b;
vals[i].first = k * x1 + b;
vals[i].second = k * x2 + b;
}
// Sort by a
sort(vals.begin(), vals.end());
ll max_b_prev = LLONG_MIN; // maximum b among lines with strictly smaller a
int i = 0;
while (i < n) {
// find group with equal a
int j = i;
while (j < n && vals[j].first == vals[i].first)
++j;
// check lines in this group against all previous groups
if (i > 0) {
for (int k = i; k < j; ++k) {
if (vals[k].second < max_b_prev) {
cout << "YES\n";
return 0;
}
}
}
// update max_b_prev with this group
for (int k = i; k < j; ++k) {
if (vals[k].second > max_b_prev)
max_b_prev = vals[k].second;
}
i = j;
}
cout << "NO\n";
return 0;