We need to find a subset of exactly k coupons such that the length of the intersection of their intervals is maximized. The intersection length is defined as max(0, min_r - max_l + 1) for integer intervals? Actually intervals are [li, ri] inclusive. So intersection length = max(0, min_{i in chosen} ri - max_{i in chosen} li + 1). We want to maximize this.
We have n up to 300k, k up to n.
We need to output the maximum length and the indices of k coupons (any subset achieving that).
This is classic: select k intervals maximizing the overlap length. Equivalent to: for each interval as the "left bound" maybe we consider the maximum of (x_r - x_l + 1) where we have at least k intervals covering [x_l, x_r]. The maximum overlap length with at least k intervals is the answer. But we need specifically to select exactly k intervals, not necessarily all intervals covering a point, but the intersection of selected intervals. That's exactly: given any set of intervals, their intersection length is determined by the maximum left bound among them and the minimum right bound among them. So we want to choose k intervals to maximize min_r - max_l + 1.
So we can think of sorting intervals by left endpoint. For each interval considered as the one with maximum left bound (i.e., the k-th largest left among selected? Actually if we sort by left ascending, then if we pick k intervals, the max_l is the maximum of their lefts, and min_r is the minimum of their rights. To maximize min_r - max_l, we want to ensure min_r is as large as possible relative to max_l? Actually we want to maximize min_r - max_l. So we need to choose intervals such that the smallest right among them is as large as possible, while the largest left among them is as small as possible? But both are determined by the set. Better approach: consider sorting intervals by left endpoint. If we fix the left bound L (as the max left among chosen), then we need to pick k intervals all with left <= L (so that L is the maximum left), and we want to maximize min_r - L. The min_r among chosen will be the k-th smallest right? Actually we need to pick exactly k intervals. If we restrict that the maximum left among them is exactly L (or at most L but we consider L as the actual max left), then the chosen intervals must all have left <= L, and at least one has left = L (or else max left is less, but we can consider that case separately). For a given L, to maximize intersection length, we would choose k intervals among those with left <= L that have the largest possible minimum right? Actually intersection = min_r - L + 1 (if min_r >= L). So we want to maximize min_r - L. Among intervals with left <= L, the min_r of a set of k intervals will be the k-th smallest right? Wait: we choose k intervals, and their min_r is the smallest right among the chosen. To maximize min_r, we want the smallest right to be as large as possible. So we want to pick k intervals with the largest possible minimum right. That is equivalent to choosing k intervals with the largest possible smallest right. Given a set of intervals with left <= L, the best we can do is to take the k intervals with the largest right endpoints? Actually if we take the k intervals with the largest rights, then the smallest among those (the k-th largest right) is as large as possible. So the min_r among the chosen will be the k-th largest right among intervals with left <= L. But careful: we need exactly k intervals, and we want to maximize min_r. So yes, we sort rights descending and pick the top k, then min_r = the k-th largest right. That yields intersection length = max(0, (kth_largest_right(L)) - L + 1). Then we can iterate L over possible left endpoints (maybe over each interval's left as candidate max left). However, we might also consider that max left could be from an interval not among the chosen? Actually if we pick k intervals, the max left among them is some left value from one of the intervals. So it's safe to consider each interval's left as candidate max left, because the optimal set will have some interval with the maximum left. So we can do: for each interval i (or each possible left value), let L = li. Consider all intervals with left <= L. Among them, we need to select k intervals including possibly interval i to ensure L is actually max left? But if we pick k intervals all with left <= L, the max left could be less than L. That's okay because we are considering L as an upper bound; the actual intersection length for that set would be min_r - (actual max left) + 1, which is >= min_r - L + 1. So when we compute using L as the bound, we might be underestimating the actual intersection if the actual max left is smaller. But we are trying to maximize min_r - max_l. If we fix L as a threshold and we consider sets where max_l <= L, then the best intersection among such sets could be better than min_r - L because max_l might be less than L. However, we want to find the best set overall. So we can treat L as the max_l of the set; to guarantee that L is indeed the max_l, we should include at least one interval with left = L. But maybe the optimal set has max_l = some left that is not exactly equal to any interval's left? Since left values are discrete (integers), the max_l will be one of the li. So we can iterate over intervals as the one that provides the maximum left. However, if we include intervals with left < L, the max_l is still L only if we include an interval with left = L. So to use L as max_l, we must include an interval with left = L. But maybe the optimal set has max_l not equal to any specific li? Actually max_l is the maximum of the chosen intervals' li, so it will be exactly equal to one of those li. So we can consider each interval as the one that gives the max_l. So for each interval i, we want to consider sets that include i and any other k-1 intervals with left <= li (since otherwise max_l would be > li). And among those, we want to maximize min_r. The min_r will be min( ri, and the smallest right among the other k-1 chosen intervals). To maximize the intersection, we want the min_r to be as large as possible. So we should choose the other k-1 intervals with left <= li and with the largest possible right endpoints (but also we need to consider that min_r takes the minimum of ri and others). So the best min_r we can achieve when including i is: let R = ri, and we need to pick k-1 intervals from the set of intervals with left <= li (excluding i) that have the largest possible right endpoints. Let the (k-1)-th largest right among those be R_k (if we have at least k-1 such intervals). Then the min_r = min(R, R_k). Actually if we pick the top k-1 intervals by right, then the smallest right among those (the (k-1)-th largest) is the minimum of those k-1 rights. So overall min_r = min(ri, that smallest among top k-1). To maximize this, we want to pick intervals that make this min as large as possible. So the best we can get is the maximum possible value of min(ri, (k-1)-th largest right among intervals with left <= li excluding i). But we can also not include i? But we are considering i as the max left. If we don't include i, the max left could be some other interval with left less or equal. So we need to consider all possibilities. So overall we need to compute for each possible L (which is li of some interval), the value: best = max over sets of k intervals with left <= L, of (min_r - max_l). But max_l might be less than L. So we need to consider sets where the maximum left is any value <= L. But the set's max_l is determined by the chosen intervals. So a direct way: sort intervals by left ascending. Then iterate over the sorted list, maintaining a data structure of rights for intervals seen so far. When we have at least k intervals seen, we can consider the current left as a candidate for the maximum left? Actually if we process left values in increasing order, then at step i (considering interval i after adding it), the set of intervals seen so far all have left <= current left. The best intersection using exactly k intervals from this set would have max_l equal to the maximum left among the chosen, which is <= current left. But we don't know which one. However, we can compute the maximum possible min_r - max_l for any subset of size k from the intervals seen so far. How to compute efficiently? The common trick: For any selection of k intervals, the intersection length = max(0, x - y + 1) where y = max_l, x = min_r. This is like we want to maximize x - y. This is similar to: if we sort intervals by right endpoint? Another approach: Consider all possible candidates for min_r (the smallest right). For a given candidate R (the min_r), we need to choose k intervals with right >= R? Actually if min_r is R, then all chosen intervals must have ri >= R, and the intersection's right bound is R? Wait min_r is the smallest right among chosen, so that means the intersection's right endpoint is min_r. So we want min_r as large as possible, but also max_l as small as possible. So maybe we can sort intervals by right descending, and consider adding intervals. But we need to pick exactly k intervals. Another known solution: Use a priority queue to maintain k intervals with largest left? Hmm.
Let’s think differently. The problem is to find a set of k intervals maximizing the overlap. This can be solved by sorting intervals by left, then using a min-heap of rights for the current set of k intervals where we iterate over left as potential max_l. Classic problem: "Given n intervals, select k intervals to maximize the common intersection length." There is known technique: sort intervals by left, use a data structure to keep track of the largest k rights among intervals processed so far. When we have at least k intervals, the current left (which is the left of the current interval) can be considered as the max_l if we include this interval as the one with max left? Actually we might not include current interval. More common: For each i after sorting by left, we consider the set of intervals with index <= i (left <= L_i). If we pick k intervals among them, the max_l will be at most L_i, and the intersection length using these k intervals (with max_l possibly less) will be at least (k-th largest right among them) - L_i + 1? No, that would be if we set max_l = L_i. But if we can make max_l smaller, the intersection could be larger. However, note that if we fix the set of k intervals, their max_l is some left value L* which is the left of the interval among them with the largest left. In the sorted order, when we have processed up to the index of that interval, we have all intervals with left <= L* in our data structure. So if we iterate over sorted intervals and at each step treat the current interval's left as the max_l (i.e., we enforce that the chosen set includes this interval as the one with the largest left), then we are covering all possibilities because the max_l must be some interval's left. So we can compute for each interval i (as the one with max left), the best possible intersection when interval i is included and all other intervals have left <= li. This yields candidate answer = max(0, min(ri, (k-1)-th largest right among intervals with left <= li excluding i) - li + 1). We'll also need to consider cases where max_l comes from an interval that is not the one with the largest left among all intervals? Actually it is the largest left among the chosen, so it's exactly some interval's left. So covering each interval as candidate is complete.
But we must ensure that we have at least k intervals with left <= li (including i). If total such intervals < k, then we cannot form a set of k intervals where max_l <= li and includes i? Actually we need exactly k intervals including i. If there are fewer than k intervals with left <= li, we cannot pick k intervals with max_l <= li (since any interval with left > li would cause max_l > li). So we need at least k intervals with left <= li. So we only consider i where count of intervals with left <= li >= k.
Now the challenge: For each i, we need to compute the (k-1)-th largest right among intervals with left <= li excluding i. Since n up to 300k, we need O(n log n) or similar.
We can process intervals sorted by left ascending. We'll maintain a data structure that holds rights of the k-1 largest among the processed intervals (excluding current?). As we iterate, we need to query for each i: given all intervals with left <= li, we need to know the (k-1)-th largest right among them, but excluding i itself. Then compute min(ri, that value) - li + 1. Actually we also have to consider that we could choose other intervals that might have left exactly li as well, but as long as we include i, it's fine even if there are other intervals with same left. However, the max_l remains li if all chosen intervals have left <= li, and at least one has left = li (i). So that's fine.
But we need the (k-1)-th largest right among the set S_i = { intervals j such that lj <= li and j != i }. But careful: We need to choose k-1 other intervals to maximize min_r. The best min_r we can achieve is: let R_other be the (k-1)-th largest right among S_i? Actually we want to pick the k-1 other intervals such that the smallest right among all k (including i) is as large as possible. Since we are free to choose which k-1, we should pick the k-1 intervals with the largest rights (subject to left <= li). Then min_r = min(ri, the smallest right among those k-1). So if we take the top (k-1) rights, let the smallest of those be R_min_other (i.e., the (k-1)-th largest right overall in S_i). Then min_r = min(ri, R_min_other). So the candidate = max(0, min(ri, R_min_other) - li + 1).
But note: If there are fewer than k-1 intervals in S_i, we cannot form a set of size k, so skip.
Thus we can compute for each i after sorting, if we have at least k intervals with left <= li, we need to compute R_min_other = the (k-1)-th largest right among intervals with left <= li, but excluding i. However, if i is among the top (k-1) rights, then excluding i changes the set. So we need to handle that.
A simpler approach: Instead of fixing i as max left, we can consider the set of k intervals that achieve the optimal. In that optimal set, let L* be the max left (largest li among chosen) and R* be the min ri among chosen. Then L* is some interval's left, and R* is some interval's right. The optimal value is R* - L* + 1 (if positive). Now, if we know L*, we need to find k intervals with left <= L* such that the k-th largest right is as large as possible? Actually among intervals with left <= L*, we pick k intervals to maximize min_r - L*. But the actual optimal might have max_l less than L*? No, L* is defined as the max left of the optimal set. So for the set, L* is the maximum left. So it's consistent: For the optimal set, if we take L* = max left, then all intervals have left <= L*, and at least one has left = L*. The min_r is some value R*. We want to maximize R* - L*. So if we iterate L* over all li, we can compute the best possible R* for sets that include an interval with left = L* (to enforce max_l exactly L*). But maybe the optimal set's max_l is L*, but there might be multiple intervals with left = L*, or the interval with left = L* might not be the one that gives the largest R*. However, we can always adjust: If the optimal set includes some intervals, and the one with max left is interval i, then L* = li. And R* is the min of rights. So candidate = min(ri, others) - li + 1. That is covered when we consider i as the max left. However, it's possible that there exists an optimal set where the interval that provides the max left is not the one with the largest ri among the set? That's fine. In our candidate for i, we compute best possible min_r given i is included. That will be at least as good as the actual R* if the optimal set's i is the max left? Actually if optimal set includes i as the max left, then the min_r is some value R_opt. For that set, the other k-1 intervals are a subset of intervals with left <= li, and the min of rights among all k is R_opt. Since we are choosing the top k-1 rights among all intervals with left <= li (excluding i), we will get a min_r that is at least as large as R_opt? Not necessarily: The optimal set might not use the top k-1 rights because maybe using some intervals with smaller rights could lead to a larger min_r? That's counterintuitive: We want min_r as large as possible. To maximize min_r given we must include i, the best is to choose other intervals with the largest possible rights, because min_r will be limited by the smallest among all. So picking intervals with larger rights can only increase or keep the min. However, if we pick intervals with very large rights, the min might still be limited by ri if ri is smaller. So indeed, the best possible min_r when i is included is min(ri, (k-1)-th largest right among S_i). So the optimal set's min_r cannot exceed that value, because it uses some other intervals which might have rights less than or equal to the top k-1. So candidate computed for i is an upper bound for any set that includes i as max left? Actually it's the maximum possible min_r when i is included. So the optimal set's value, if it includes i as max left, is ≤ that candidate. And we can achieve that candidate by actually picking the intervals with top k-1 rights (assuming they have left <= li). However, does picking those top k-1 intervals guarantee that they all have left <= li? Yes, because we only consider intervals with left <= li. So that set is feasible, and includes i. Then the min_r = min(ri, the smallest of those top k-1). So we can achieve candidate value. Thus the optimal set's value will be exactly the maximum over i of that candidate. So this approach yields the correct maximum.
Edge Cases: What if the best set does not include any interval as the unique max left? That is, there may be multiple intervals with the same max left. Then we can choose any one of them as i; our candidate will consider each. In the set, if we have multiple intervals with same max left, picking one of them as i and the others possibly among the top k-1 rights might conflict because i is excluded from the top k-1 calculation; but we can always choose a set that achieves the candidate by taking i and the top k-1 rights among others (excluding i). If i itself is among the top k-1 rights, then when we exclude i, the top k-1 rights may be slightly lower, but the candidate still computes correctly. Also, the set we construct may include another interval with left = li as part of the top k-1; that's fine, because then max left remains li (since both have left li). So the candidate set is valid.
Thus algorithm: Sort intervals by left ascending. Then we need a data structure that can quickly, for each i, give the (k-1)-th largest right among all intervals with left <= li, excluding i. Since we process in increasing left, we can maintain a structure of rights for all intervals seen so far. However, at each i, we need the value of the (k-1)-th largest right among the set of intervals with left <= li (which includes i). But we need excluding i. We could precompute for each i, the k-th largest right among intervals with left <= li? Let's think.
We can process as follows: After sorting by left, we will iterate over intervals. We'll maintain a data structure (like a min-heap) of the largest (k-1) rights among the intervals seen so far (excluding the current? but we need to consider the set of seen intervals up to current i). Actually we want to answer query: given a prefix of sorted intervals (by left), what is the (k-1)-th largest right? That can be answered using a data structure that can maintain the top (k-1) elements as we add elements. However, when we are at interval i (with left li), the prefix includes intervals 1..i (since sorted by left, and left values can be equal; but we need to include intervals with left <= li, which are exactly those indices up to i, because sorting is by left, and if there are equal left values, order doesn't matter; but careful: if there are multiple intervals with same left, we need to decide ordering. We'll process in some order; but we need to consider that when we are at interval i, the prefix includes intervals with left <= li. Since we process in order, by the time we process interval i, we have added intervals with left < li, and also intervals with left == li that come before i (depending on ordering). That might cause some intervals with same left not yet added when we query for i. To avoid missing, we could process intervals in batch: sort by left, then for each distinct left, we first compute for all intervals with that left using the state after adding all intervals with left < current left, then after processing queries, we add all intervals of this left to the structure. That way, when we consider an interval as max left, we need intervals with left <= current left, but we must exclude the current interval? Actually if we process in batch, before adding intervals with left = L, the structure contains intervals with left < L. Then for each interval i with left = L, the set of intervals with left <= L (including i and others with left = L) is not fully in structure because we haven't added any with left = L yet. But we need the set including other intervals with left = L (except i) to compute the (k-1)-th largest right. So we need a way to include all intervals with left = L except i. One approach: after adding all intervals with left = L to the structure, we can then process queries for each i with left = L? But then the prefix includes all intervals with left <= L, including i. However, we need to exclude i. So we need to temporarily remove i from the structure to compute the top (k-1) rights among others. That is doable if we can support removal. Alternatively, we could compute the top k rights among the prefix including i, then adjust if i is among them. Let's think about computing candidate for i:
Let S = set of intervals with left <= li. Let |S| = m (should be >= k). We need the (k-1)-th largest right among S \ {i}. Equivalent to: if we take the top k rights from S, then depending on whether i is in those top k, the (k-1)-th largest among S{i} might be the k-th largest overall (if i is among top k) or the (k-1)-th largest overall (if i is not among top k). More precisely, let R_k be the k-th largest right in S (i.e., the k-th order statistic from largest). Let R_{k+1} be the (k+1)-th largest. But we need the (k-1)-th largest after removing i. Actually we need the value such that when we take the top (k-1) rights from S{i}, the smallest of those (the (k-1)-th largest) is the answer. That is equivalent to: if we consider the sorted descending rights of S, and remove i, then the (k-1)-th element in that list is what? If the index of i in the sorted list is pos (1-indexed from largest), then after removal, the (k-1)-th largest is:
- If pos <= k-1, then originally the (k)-th largest becomes the (k-1)-th after removal (since we removed one of the top (k-1)). Actually careful: Suppose we have sorted rights r1 >= r2 >= ... >= rm. Remove one element at position pos. Then the new list has length m-1. The (k-1)-th largest in the new list corresponds to:
- If pos <= k-1, then the original r_{k} becomes the (k-1)-th (since we removed one of the first k-1, the element at position k shifts to position k-1).
- If pos >= k, then the original r_{k-1} remains the (k-1)-th (since removal after that doesn't affect the top k-1). So the desired value is:
if rank_of_i (in descending rights) <= k-1: then value = the k-th largest right in S (i.e., the element at position k).
else: value = the (k-1)-th largest right in S.
Thus, to compute for each i, we need to know:
- The k-th largest right among intervals with left <= li.
- The (k-1)-th largest right among intervals with left <= li.
- The rank of i's right among those.
But note that S includes i itself. So we can compute these if we can maintain order statistics over rights in a prefix. That suggests using a data structure like a balanced BST or a Fenwick tree after coordinate compression of rights, to support order statistics and queries of k-th largest. Since n is up to 3e5, we can compress all right values. We can process intervals sorted by left, and as we go, we add intervals to a Fenwick tree (BIT) that counts frequencies of rights. Then for a prefix (up to current left), we can query the k-th largest right via binary search on BIT (if we store counts). We also need to know the rank of a specific interval's right (i.e., how many rights are >= ri). Since we want descending order, it's easier to work with ascending order and convert: rank in descending = number of rights >= ri. That's count of rights in [ri, max] which we can get from BIT. So we can compute:
Let total = number of intervals with left <= li.
If total < k: skip.
Else:
cnt_ge = query(ri, max) // number of intervals in S with right >= ri
Now, we need:
- R_k = k-th largest right in S. That is the smallest value r such that count of rights >= r is at least k. We can find it via BIT: we want the smallest r such that prefix from r to max (or suffix) >= k. Since BIT typically does prefix sums from 1 to x, we can compress and sort ascending. To get count of rights >= x, we can compute total - prefix(x-1). Or we can store in BIT where indices correspond to sorted rights ascending, and we want to find the value where suffix sum >= k. That can be done by binary search on BIT using total - prefix(mid-1) >= k. So we can find R_k.
- Similarly, R_{k-1} = (k-1)-th largest right in S, found similarly.
Now, we need to compute value = min(ri, X) - li + 1, where X is:
if cnt_ge (including i) <= k-1 (i.e., i's rank in descending <= k-1) then X = R_k (since i is among top k-1? Actually rank <= k-1 means pos <= k-1. So we need to check if cnt_ge >= k? Let's be precise:
The rank of i in descending order = number of intervals in S with right > ri plus those with right = ri? Actually we need the count of intervals with right >= ri. That includes i and possibly others with same right. So if cnt_ge is the count of intervals with right >= ri. Then the position (1-indexed) of i in sorted descending list is somewhere between (cnt_ge - (count_eq -1)) and cnt_ge depending on ties. But ties cause ambiguity: if there are multiple intervals with same right value ri, then they are not ordered, but the rank is not uniquely defined; we need to know whether i is among the top (k-1) when sorting by right descending? For ties, if there are duplicates, the (k-1)-th largest value could be equal to ri. The rule for computing X after removing i depends on whether i is considered among the top (k-1) rights. But since we can choose any set, ties are not an issue: we can include any intervals with that right. So the candidate value we compute should be the maximum achievable min_r when i is included, and that depends on whether we can "use" other intervals with same right to fill the top (k-1). However, our formula using rank might be delicate with duplicates.
Let's think more directly: For a given i, we need to find the largest possible min_r achievable by selecting k-1 other intervals from S (excluding i). That min_r = min(ri, min_of_selected_rights). The optimal selection is to take the k-1 intervals with the largest rights (excluding i). Let T be the set of top (k-1) rights from S \ {i}. Let R_min_other = the smallest right in T (i.e., the (k-1)-th largest right in S \ {i}). Then candidate = min(ri, R_min_other) - li + 1.
So we need to compute R_min_other efficiently.
We can compute the top k rights from S. Let those be the largest k rights. If i is not among the top k rights, then the top k-1 rights from S \ {i} are just the top k-1 rights from S (since i is not in the top k, it doesn't affect the top k-1). In that case, R_min_other = the (k-1)-th largest right in S.
If i is among the top k rights, then we need to consider:
- If i is among the top (k-1) rights, then the top k-1 from S \ {i} will be the top k-1 from S but with i removed, so they become: the top (k-2) from S plus the k-th largest from S. So the smallest among those (the (k-1)-th largest in S \ {i}) is the k-th largest in S.
- If i is exactly the k-th largest? Then i is in the top k, but not in the top (k-1). Then after removing i, the top k-1 from S \ {i} are still the top k-1 from S (since i was not among them). So R_min_other = (k-1)-th largest in S. So only when i is among the top (k-1) rights (i.e., its right is strictly greater than the k-th largest? Actually ties complicate). Let's define sorted descending rights list: r1 >= r2 >= ... >= rm. Suppose we have many equal values. If ri is such that there are at most k-1 intervals with right > ri (i.e., strictly greater), then i is not in the top k? Wait, we need to consider whether i is among the top (k-1) when we pick top k-1 after removal. The condition for needing to use the k-th largest is if i is among the top (k-1) positions in the sorted list (ignoring ties? But ties cause ambiguity because we can choose which intervals to include. In the set S, there may be multiple intervals with the same right value. For the purpose of selecting top k-1 rights, if there are ties at the boundary, we have flexibility: we can choose any of the intervals with that right value to be part of the top set. So when computing the maximum possible min_r, we want to maximize the resulting min. So if there are many intervals with the same right, we can choose which ones to include to get the best min. Therefore, for a given i, we should consider the best-case selection of other intervals. So we need to compute the best achievable R_min_other, not necessarily uniquely determined by sorted order with arbitrary tie-breaking; we can choose any combination.
Thus, maybe it's easier to directly compute candidate as follows: Let R_kth be the k-th largest right among S (including i). Then:
- If ri >= R_kth, then i is among the top k? Not exactly: if ri >= R_kth, then i's right is at least as large as the k-th largest. It could be that there are at least k intervals with right >= ri? Actually R_kth is the k-th largest value. If ri >= R_kth, then ri is at least that value. So i qualifies to be in the top k. But we need to see whether we can avoid having i being the limiting factor (the min). The min_r overall = min(ri, R_min_other). Since we can choose other intervals, we want R_min_other as large as possible. If ri is relatively large, then the limiting factor may be R_min_other. So we want to know what is the largest possible R_min_other given that we cannot use i. That is essentially the (k-1)-th largest right among S \ {i}. This value is:
- If there are at least k intervals with right > ri? That would mean i is not among the top (k-1) in terms of value? Let's derive:
Let a = number of intervals in S with right > ri. (strictly greater)
Let b = number of intervals in S with right = ri (including i). So total with right >= ri = a + b.
Now, we need to select k-1 other intervals. To maximize the minimum of their rights, we should pick those with largest rights. The (k-1)-th largest right among S \ {i} will be:
- If we can pick k-1 intervals all with right > ri, then R_min_other > ri, but then min(ri, R_min_other) = ri. So the min would be ri. However, if a >= k-1, then we can pick k-1 intervals all with right > ri, so the min would be ri (since ri is smaller). In that case, candidate = ri - li + 1.
- If a < k-1, then we must include some intervals with right <= ri. Actually we need to pick k-1 intervals; we have a intervals with right > ri, and (b-1) other intervals with right = ri (since i excluded), and others with right < ri. To maximize the minimum, we will pick all a intervals with right > ri, then the remaining (k-1 - a) from the next best, which would be those with right = ri (if available). So then the smallest right among selected others would be ri (if we can fill with ri). So R_min_other = ri if we can get enough with right >= ri. That is, if a + (b-1) >= k-1 (i.e., total intervals with right >= ri excluding i is >= k-1), then we can select k-1 others all with right >= ri, so R_min_other >= ri, but the min with ri is still ri. Actually if we include some with right = ri, the minimum is ri. So candidate = ri - li + 1.
- If a + (b-1) < k-1, then we cannot get enough others with right >= ri; we will have to include some with right < ri, so R_min_other < ri. Then min(ri, R_min_other) = R_min_other, which is less than ri. In this case, we need to find the (k-1)-th largest right among S \ {i}, which is the value such that there are exactly k-1 intervals with right >= that value. Since a + (b-1) < k-1, the (k-1)-th largest will be some value less than ri. That value is essentially the (k-1)-th order statistic from the largest after removing i. This can be computed as the (k)-th largest in S if i is among the top a+b? Actually let's find.
In general, candidate = min(ri, X) - li + 1, where X = the (k-1)-th largest right in S{i}. X can be computed as:
Let R = the (k)-th largest right in S if i is among the top (k) and its rank (based on >=) is such that after removal, the (k)-th largest becomes the (k-1)-th? Hmm.
Better to compute X directly via order statistics on S{i}. Since we have BIT over rights, we could query the (k-1)-th largest from the set S excluding i by temporarily removing i, query, then add back. But doing that for each i would be O(n log n) with removals? We can do it efficiently by processing intervals in order of left, and for each i, we need to query the (k-1)-th largest in the set of intervals with left <= li, excluding i. Since we are processing in order, we could maintain two BITs: one for counts of rights for intervals with left < current left (i.e., already processed previous groups), and another for intervals with left == current left that are not i. But handling duplicates of left might be messy.
Alternative approach: Instead of iterating over each interval as max left, we can do a binary search on answer length? Another classic solution: Use a segment tree or sweep line with a priority queue to find maximum intersection of k intervals.
Another known solution: Sort intervals by left ascending. Use a min-heap to maintain the rights of the current k intervals being considered. For each interval in sorted order, we push its right into a min-heap. If heap size > k, we pop the smallest right (which is the min among the k+1? Actually typical problem: Given n intervals, find maximum number of overlapping intervals at any point. That uses sorting by left and min-heap of rights. But here we need exactly k intervals maximizing intersection length. There is a known technique: For each interval as the left endpoint (or as we iterate, we consider the current interval as the left bound? Actually we can use a sliding window over sorted by left, maintaining the top k largest rights? Let's think.
We want to select k intervals to maximize min_r - max_l. If we sort by left ascending, and consider a sliding window of intervals: for each possible max_l (i.e., the left of the interval at position i), we want to consider intervals with left <= L (i.e., from start to i). Among these, we want to pick k intervals with the largest rights to maximize the min_r (which will be the k-th largest right among those? Actually if we pick k intervals from the prefix, the maximum possible min_r (i.e., the maximum possible lower bound of the intersection) is the k-th largest right among the prefix. Because to maximize min_r, we should pick the k intervals with the largest rights; then the smallest right among them is the k-th largest in the prefix. However, we also need to ensure that the max_l is not necessarily the left of the i-th interval; it could be smaller. But if we fix the set of chosen intervals, max_l is the maximum left among them. If we pick k intervals from the prefix (with indices up to i), the max_l among them could be any left <= L_i. The intersection length if we pick those k intervals would be (k-th largest right among the chosen) - (max left among chosen) + 1. But if we simply take the k intervals with the largest rights from the prefix, their max left might be less than L_i. In fact, their max left would be the maximum left among those selected intervals, which might be smaller than L_i. That would give a larger intersection than if we forced max left to be L_i. So to find the overall optimum, we need to consider all subsets, not necessarily those where max left equals the current prefix bound.
But perhaps we can use the following: The optimal set of k intervals, when sorted by left, will have some leftmost and rightmost? Another approach: Sort intervals by left. For each interval i, consider it as the one with the smallest right? Not exactly.
Alternate viewpoint: The intersection length = max(0, min_r - max_l). To maximize this, we can consider pairs (L, R) such that at least k intervals cover [L, R] (i.e., have left <= L and right >= R). Because if there are at least k intervals covering [L, R], then we can choose k among them, and their intersection will contain [L,R] at least, but the actual intersection might be larger. However, the maximum possible min_r - max_l is the maximum over L,R of (R - L) such that there are at least k intervals covering [L,R]. Actually if we have k intervals, their intersection is [max_l, min_r]. So the length is min_r - max_l +1. So it's like we want to maximize the gap between a left bound L and a right bound R such that there are at least k intervals covering [L,R]. This is like the "maximum k-covered segment" problem. But we need to pick exactly k intervals, not necessarily all covering a common segment? Wait: If there are at least k intervals covering [L,R], we can select k of them, and their intersection will be at least [L,R]? Not exactly: The intersection of the selected k intervals is the intersection of their individual intervals. If each selected interval covers [L,R], then the intersection will cover [L,R] (since each contains it). But the intersection might be larger if some intervals extend further. However, the intersection's left endpoint is the maximum of their lefts, which could be > L if some intervals have left > L. If we choose intervals that all cover [L,R], but they might have left > L, then the max left could be > L, so the intersection might be [max_left, min_right] which could be a subset of [L,R] or maybe not contain [L,R] entirely. Actually if each interval covers [L,R], it means left <= L and right >= R. So each interval's left <= L, right >= R. Then the max left <= L and min right >= R. So intersection contains [L,R] and possibly more? Actually max left could be less than L? Wait: left <= L, so max left could be less than or equal to L, maybe less. That means the intersection's left bound is ≤ L. Similarly, min right ≥ R. So the intersection contains [max left, min right] which definitely contains [L,R] if max left ≤ L and min right ≥ R. So indeed intersection includes [L,R]. So length ≥ R-L+1. So if we find a segment [L,R] covered by at least k intervals, we can achieve length at least R-L+1. But the optimal might be larger because the intersection could be larger than any particular [L,R] that is covered. However, the intersection itself is a segment [max_l, min_r]; it is covered by all k intervals, obviously. So the optimal length equals the maximum over all segments [L,R] such that there are at least k intervals covering it? Actually given the optimal set with intersection [L*, R*], the segment [L*, R*] is covered by each of the k intervals. So it is a segment that is covered by at least k intervals (the selected ones). But there could be other intervals covering it as well, but at least k. So L*,R* satisfy that condition. So the maximum possible length is exactly the maximum length of a segment that is covered by at least k intervals. Because any segment that is covered by at least k intervals gives us a set of k intervals (those covering it) whose intersection contains that segment, so length is at least that. And the intersection of any k intervals is a segment covered by those k intervals, so its length is a candidate. So maximizing over k intervals' intersection is equivalent to maximizing over segments the length of segments covered by at least k intervals, and then picking the k intervals that achieve that coverage? But careful: The intersection of the chosen k intervals might be larger than any segment that is covered by at least k intervals? Actually if there is a segment covered by at least k intervals, the intersection of those intervals might be larger than that segment. But the length of the intersection is the length of the maximal segment that is common to all k intervals. That segment itself is covered by at least k intervals (the chosen ones). So it's a segment that is covered by at least k intervals. So the optimal length is the maximum possible length of a segment that is covered by at least k intervals. So we can rephrase: Find L,R that maximize R-L+1 such that there are at least k intervals with li <= L and ri >= R. This is a classic problem: maximum length of a segment covered by at least k intervals. This can be solved by scanning with a segment tree or using a priority queue.
But note: The intervals are arbitrary integer ranges up to 1e9, but we can compress coordinates. The coverage condition is two-dimensional: li <= L and ri >= R. So for a given L,R, count of intervals covering [L,R] = number of intervals with li <= L and ri >= R. So we want to maximize R-L subject to count >= k.
We can treat this as: For each possible L (taken from li values, or maybe all integer points?), we want to find the largest R such that count(L,R) >= k. Then answer is max over L of (R-L+1). This is similar to "maximum overlapping segment with at least k intervals" problem.
Standard solution: Sort intervals by left ascending. Use a data structure (like a segment tree over right endpoints) to maintain counts of intervals that have left <= current L and have right >= some value. As we increase L, we remove intervals? Actually more common: Process events: For each interval [l, r], we can think of it as covering all points (L,R) with L <= l and R >= r? Not directly.
Alternative: Use sweep over possible left bound. Sort intervals by left. Use a min-heap of rights to keep track of intervals that have left <= current L. As we increase L, we add intervals that start at that L. But we need to count intervals that also have right >= R. If we fix L, we want the maximum R such that at least k intervals in the current set have right >= R. The current set contains intervals with left <= L. Among them, we need at least k with right >= R. So for a fixed L, the maximum R is the k-th largest right in the current set (i.e., the value such that there are at least k intervals with right >= R). Actually if we have a set of rights, the condition count of intervals with right >= R >= k is equivalent to the k-th largest right >= R. So the maximum R that satisfies is exactly the k-th largest right (if there are at least k intervals). So for each L, the best possible R is the k-th largest right among intervals with left <= L. Then the candidate length = max(0, (k-th largest right) - L + 1). But is this correct? Let's test: Suppose we have intervals with left <= L, and we take the k intervals with the largest rights. Their intersection is [max left among them, k-th largest right]. The max left among them could be less than L. So the intersection length is at least (k-th largest right) - (max left) + 1, which could be > (k-th largest right) - L + 1. However, we are not forced to set L as the left bound; we are computing candidate as if left bound is L. But the actual set's intersection left bound is the maximum left among the chosen intervals, which could be lower than L. So the candidate we compute might be less than the actual intersection length we could achieve with that set of intervals. But we are trying to find the maximum possible length over all choices of k intervals. If we iterate over L as the left bound (max left) and compute candidate as (k-th largest right) - L + 1, we are effectively restricting that the max left is exactly L. That could be lower than the true optimum if the optimal set has max left smaller than the L we are considering. However, if we take L as the current L (some value) but choose k intervals with left <= L, the max left among them might be less than L, so the intersection length could be larger than (k-th largest right) - L + 1. So the candidate we compute by using L as the max left is an underestimate. But if we consider all L, the maximum over these underestimates might still be the true optimum because the true optimum will have some max left L*; then when we consider L = L* in this iteration, we will compute candidate = (k-th largest right among intervals with left <= L*) - L* + 1. However, the optimal set's min_r is the k-th largest right among the chosen intervals? Actually if we pick the optimal set of k intervals with max left = L*, then min_r is at most the k-th largest right among intervals with left <= L* (since the optimal set is a subset, its min_r is the smallest right among them, which is ≤ the k-th largest right among the whole set? Wait, the k-th largest right among all intervals with left <= L* is the k-th largest value in that superset. The optimal set's min_r might be larger if we choose intervals with particularly large rights? Actually the k-th largest right among the whole set is the maximum possible value for the k-th largest right among any subset of size k? Because the k-th largest right in the whole set is the maximum possible min_r for a subset of size k? Let's reason: Among all subsets of size k from the set of intervals with left <= L*, the largest possible min_r (i.e., the maximum value we can achieve for the smallest right) is achieved by selecting the k intervals with the largest rights. Because we want to maximize the minimum right; we do that by picking the intervals with the largest rights, making the smallest among them as large as possible. So the maximum possible min_r for any subset of size k from that superset is exactly the k-th largest right in that superset. So for a fixed L* (which is the max left of the optimal set), the optimal set's min_r cannot exceed that value, and the best we can do is exactly that value if we pick the top k rights. However, if we pick the top k rights, their max left might be less than L*. But if we also require that the max left is exactly L* (i.e., we include an interval with left = L*), then the max possible min_r might be lower than the k-th largest right, because including an interval with left = L* might force us to not take some interval with a very large right if that interval's left is > L*? Actually all intervals considered have left <= L*, so they are all eligible. Including a specific interval (the one with left = L*) might reduce the min_r if that interval's right is smaller than the k-th largest right. But the optimal set we are considering for overall optimum might not include that specific interval; its max left is L* but it might be achieved by some interval that also has a large right. Indeed, if we pick the top k rights, the max left among them could be some value L' <= L*. If L' < L*, then the max left of that set is L', not L*. But we are looking for sets with max left exactly L*? The overall optimum might have max left L' which is smaller, giving larger intersection. So we need to consider sets where max left can be any value. So the candidate for L = L' would be (k-th largest right among intervals with left <= L') - L' + 1. Since L' is the actual max left of the optimal set (if we pick top k rights with left <= L'), that candidate equals the intersection length achieved by that set. So the overall optimum is max over L of (k-th largest right among intervals with left <= L) - L + 1, where L is any possible max left? But note: In that expression, L is the current "left" we are considering as the max left, but when we compute (k-th largest right) - L + 1, we are implicitly assuming that the max left is L. However, if we pick the top k rights from intervals with left <= L, their max left may be less than L, so the actual intersection length would be larger than this expression. But if we set L to be the actual max left of that set, then the expression gives the true length. So we need to consider L as the max left of the chosen set, not as a bound. Therefore, if we iterate over all possible values of L that could be the max left of some set of k intervals (i.e., the left endpoint of some interval), then the maximum over these L of (k-th largest right among intervals with left <= L) - L + 1 is actually the answer. Because for each L, the best set with max left exactly L yields candidate = min_r_opt(L) - L + 1, where min_r_opt(L) is the maximum possible min_r among subsets of size k from intervals with left <= L that also include an interval with left = L? Wait, we need to ensure that max left is exactly L. The expression (k-th largest right among intervals with left <= L) does not guarantee that the selected set includes an interval with left = L. If we simply take the top k rights, the max left among them might be less than L. Then the set's max left is less than L, so its intersection length is (k-th largest right) - (actual max left) + 1, which is > (k-th largest right) - L + 1. So the candidate (k-th largest right) - L + 1 could be smaller than the actual length of that set. However, the set with max left less than L would be considered when we set L to its actual max left, not the current L. So the maximum over L of (k-th largest right among intervals with left <= L) - L + 1 might not capture sets where the max left is less than the left we are using as bound? Actually, if there exists a set with max left = L0 and min_r = R0 (the k-th largest right among intervals with left <= L0? But it might not be the k-th largest right among all intervals with left <= L0 if the set is not the top k rights. The optimal set for a given L0 (as max left) would be to include an interval with left = L0 and then choose the other k-1 intervals with largest rights among those with left <= L0 (excluding that interval). That yields min_r = min(ri_of_chosen_with_left_L0, (k-1)-th largest right among others). This is not necessarily equal to the k-th largest right among intervals with left <= L0. For example, suppose we have many intervals with left very small and very large rights, and one interval with left = L0 but small right. The k-th largest right among all intervals with left <= L0 might be huge, but if we need to include the interval with left = L0 (to have max left exactly L0), the min_r might be limited by that interval's small right. So the best set with max left exactly L0 might have a smaller min_r than the k-th largest right overall. So the expression (k-th largest right) - L + 1 is not correct for max left exactly L unless we enforce inclusion of an interval with left = L. So we need to include that constraint.
Thus, the earlier approach of iterating each interval as max left and computing best min_r given inclusion of that interval is correct. So we need to compute for each interval i: candidate_i = max(0, min(ri, X_i) - li + 1), where X_i = (k-1)-th largest right among intervals with left <= li excluding i. Then answer = max_i candidate_i, and we also need to output the indices.
Now we need to compute X_i efficiently. We can do this by processing intervals in increasing left order, using a data structure that can give order statistics and handle removal for the current interval. Since n is 300k, we can afford O(n log n) with a BIT and perhaps two passes: one forward to compute prefix order stats, and another backward? But removal for each i would be O(log n) if we temporarily remove i from BIT and then add back, leading to O(n log n) total, which is fine (300k * log 300k ~ 5e6). However, we need to be careful with ordering: When processing intervals sorted by left, we need for each i the set of intervals with left <= li, which includes intervals with same left as i that may not have been added yet. If we add all intervals with left < li first, then for intervals with left = li, the set includes all intervals with left = li (including i). To compute X_i for i, we need the set S = {j: lj <= li}. This includes all intervals with left = li. So we need to have them all in the BIT when computing for each i of that left group. But we also need to exclude i itself. So we could first add all intervals with left = li to the BIT, then for each i in that group, we temporarily remove i, compute X_i, then re-add i. That would work, as long as we process the group correctly. However, note that when we compute X_i for an interval i in the group, we need to consider other intervals in the same group as well, because they have left = li and are part of S. So adding all first then temporarily removing each i is fine. But careful: For