We need to solve problem: given n positions, each position i has an interval [a_i, b_i] specifying that the person (label) in that position must be between a_i and b_i inclusive. There is at least one permutation of labels 1..n that satisfies: for each position i, the assigned label p_i ∈ [a_i, b_i]. We need to determine if there is exactly one such permutation (unique). If unique, output YES and the permutation. If not unique, output NO and then two distinct valid permutations.
Constraints: n ≤ 2e5, a_i,b_i ≤ n.
We need to find all possible assignments (permutations) respecting intervals. The problem is essentially: find if the bipartite matching between positions and labels with constraints [a_i,b_i] has exactly one perfect matching. And if not, produce two distinct matchings.
We could think of this as an assignment problem: each label j must go to some position i such that j ∈ [a_i,b_i]. The intervals are per position, not per label.
Observation: The constraints are intervals. This is reminiscent of matching when intervals are nested or something. Could be related to "unique matching in bipartite graph with intervals" or "interval scheduling" but it's a permutation.
Alternate viewpoint: We have positions 1..n, each has allowed labels interval [a_i,b_i]. We need a permutation f: positions -> labels such that f(i) ∈ [a_i,b_i]. Since labels are 1..n, it's a bijection.
We need to determine uniqueness. Could be solved by constructing the unique matching if it exists, or otherwise find two.
One approach: Sort positions by something? There is classic problem: "Unique assignment in bipartite graph with intervals" or "Checking if the greedy matching is unique" but maybe we can use Hall's theorem and consider that uniqueness corresponds to each position having only one possible label after elimination? Not exactly.
Another thought: Since labels are 1..n, and intervals are ranges, the assignment is essentially a permutation that respects these lower and upper bounds. Could be modeled as: assign each label to a position where it fits.
We can consider a bipartite graph with left side positions, right side labels. Edge if j ∈ [a_i,b_i]. This is a convex bipartite graph: for each position i, its neighbors are a consecutive interval of labels. Also labels on right side have intervals? Actually, from the perspective of a label j, its neighbors are positions i such that a_i ≤ j ≤ b_i. That's also an interval (since if we order positions by index, those constraints define an interval? Not necessarily, because a_i and b_i can be arbitrary, so the set of positions where label j is allowed may not be contiguous. However, we can reorder positions arbitrarily? But positions are fixed order (just indices). So the bipartite graph is not symmetric.
Convex bipartite graphs (left side has intervals) have properties: maximum matching can be found by greedy algorithm if we sort positions? Actually, typical algorithm for convex bipartite matching: sort positions by b_i (right endpoints) and assign smallest available label that is ≥ a_i, using a DS. That works if we want any perfect matching, provided existence is given? This is like matching with intervals: if we process positions in order of increasing b_i, and assign each the smallest available label in its interval, we get a matching if one exists, assuming the greedy works? There's a known condition: given intervals for each left vertex, with each right vertex having degree? But we have to assign each label exactly once. The classic "interval scheduling" matching: we have tasks (positions) each requiring a resource (label) from an interval. We need to assign distinct resources. This is similar to "matching with intervals" and a greedy algorithm works: sort positions by b_i, then for each position assign the smallest available label ≥ a_i. This yields a matching if one exists, provided we have the "Hall" condition? Actually, it's known that if a perfect matching exists, then the greedy algorithm that assigns the smallest available label in the interval (or the largest?) will find a matching. I recall a problem: "Assign each interval a point such that points are distinct" (classic interval scheduling with distinct points) - that is exactly: given n intervals, assign each a distinct integer point inside the interval. This is equivalent to matching positions (intervals) to labels (points). Existence can be checked by sorting intervals by right endpoint and greedily assigning the smallest available point that lies in the interval. This is a classic algorithm: if there is a perfect matching, this greedy succeeds. And if it fails, there is no perfect matching. However, is that always true? Let's think: The problem of assigning distinct integer points to intervals, where each interval wants one point. This is like "matching in interval graphs" and I think the greedy algorithm works because of the matroid property? Actually, it's known that the "interval scheduling with resource allocation" problem: if you have intervals [a_i, b_i] and you need to assign distinct integers (or time slots) such that each assignment is within the interval, then a greedy algorithm that processes intervals in order of increasing b_i and assigns the smallest available integer ≥ a_i works, provided that the set of intervals is feasible. This is because of the "exchange argument". But careful: The labels are exactly 1..n. So the set of points is fixed. The existence condition is: after sorting intervals by b_i, for each k, the number of intervals with b_i ≤ k must be ≤ k, and number of intervals with a_i ≥ k+1? Actually Hall's condition reduces to: For any t, the number of intervals that must use numbers ≤ t is ≤ t? There is known condition: For interval scheduling with distinct points, feasibility is equivalent to: for every x, the number of intervals with a_i ≤ x ≤ b_i is at most? Hmm.
Standard problem: "Assign each of n jobs a distinct time slot from 1..n such that each job i can be scheduled in a time slot in [a_i,b_i]". The feasibility condition is: for all k=1..n, the number of intervals with a_i ≤ k must be ≤ k and the number of intervals with b_i ≥ k must be ≤ n-k+1? Something like that. But I think the greedy algorithm (assign smallest available that fits) works if we process intervals sorted by b_i. I recall a Codeforces problem: "Fox And Names" no. There is known "Greedy matching in bipartite graph with intervals" where one side has intervals.
Let's test: n=2, intervals: position1: [1,2], position2: [1,1]. Sorted by b: pos2 (b=1), pos1 (b=2). For pos2, assign smallest available ≥1 => assign 1. For pos1, assign smallest available ≥1 => available {2}? Actually 1 taken, so smallest available ≥1 is 2, fits because 2 ∈ [1,2]. So success. If we had pos1: [1,1], pos2: [1,2], sorted by b: pos1 (b=1), pos2 (b=2). pos1 gets 1, pos2 gets smallest available ≥1 => 2, ok. So works.
Consider a tricky case: intervals: pos1: [2,2], pos2: [1,2]. Sorted: pos1 (b=2? Actually b1=2, b2=2). If we break ties maybe by a? But whichever, say pos1 first (b=2) assign smallest available ≥2 => 2 (fits). Then pos2: smallest available ≥1 => 1 (fits). Works. If pos2 first, assign 1, then pos1 assign smallest available ≥2 => 2, works. So seems fine.
Consider a case where greedy might fail but a matching exists? For intervals, I think the greedy algorithm that sorts by right endpoint and assigns the smallest available integer works if a perfect matching exists, provided we treat the set of labels as 1..n and the intervals are arbitrary. This is essentially a variant of the "matching in bipartite graph with intervals on one side" which is a "convex graph". For convex graphs, the greedy algorithm (matching left vertices in order of increasing b_i, assigning the smallest available right vertex) yields a maximum matching. And if there is a perfect matching, it will find one. This is known: For a convex bipartite graph (where for each left vertex, its neighbors form an interval, and the right vertices are ordered), the greedy algorithm gives a maximum matching. So we can use it.
Thus we can compute one valid permutation via greedy. Now we need to determine if there is exactly one perfect matching.
How to test uniqueness? For a bipartite graph with perfect matching, the matching is unique if and only if for each edge in the matching, it belongs to every perfect matching? Actually uniqueness means exactly one perfect matching exists. Equivalent to: the bipartite graph's perfect matching is unique. We can check by trying to find an alternating cycle? In general, a perfect matching is unique if and only if the bipartite graph has no alternating cycle (with respect to that matching) because an alternating cycle would allow swapping along the cycle to get another matching. More precisely, if we have a perfect matching M, then another perfect matching exists iff there exists an even cycle whose edges alternate between M and E\M. So we could search for an alternating cycle. For convex bipartite graphs, perhaps there is more efficient method.
Given constraints up to 2e5, we need O(n log n) or similar.
One idea: Build the greedy matching M. Then try to see if we can "swap" two labels among positions while respecting intervals. But perhaps we can deduce uniqueness from the structure of intervals.
Another angle: The intervals define constraints per position. Since labels are 1..n, the assignment is a permutation. The uniqueness condition might be that for each position i, the interval [a_i, b_i] is forced to a single possible label after considering all constraints. But it's not that simple: even if each position has multiple allowed labels, the global constraints might force a unique assignment. For instance, n=2: position1: [1,2], position2: [1,2]. Both intervals allow both labels, but the permutations are (1,2) and (2,1). So not unique. However, if we add a third position? For n=3: intervals: pos1: [1,1], pos2: [2,2], pos3: [3,3] obviously unique. If pos1: [1,2], pos2: [1,2], pos3: [3,3] => then two possibilities for first two: (1,2) or (2,1), but both are fine with pos3. So not unique.
So uniqueness depends on existence of alternating cycles.
Given the intervals, we could try to compute the set of labels that can be assigned to each position in any perfect matching. That is the "feasible set" under the global constraints. If for each position, that set is exactly one label, then matching is unique. Conversely, if some position has at least two possible labels, does that guarantee non-uniqueness? Not necessarily: could be that despite a position having two possible labels, the global assignment is forced due to other constraints? Let's think: If a position i can be assigned either label x or y (both in its interval) in some perfect matchings, but maybe no single perfect matching can realize both? Actually, if the set of all perfect matchings, for each position, the set of labels that appear in some perfect matching is the "possible assignments". If for some position there are two possible labels, that means there exist two different matchings where that position gets different labels, which would be two distinct matchings, thus non-unique. However, careful: It's possible that the set of labels that can be assigned to i across all perfect matchings includes two labels, but maybe both labels cannot be realized in the same matching? That's exactly what it means: there exists a perfect matching where i gets x, and there exists another where i gets y. Those are two distinct matchings. So indeed if the set of possible labels for some position has size >1, then there are at least two perfect matchings. So uniqueness is equivalent to each position having exactly one possible label across all perfect matchings. So we could compute for each position the set of labels that can be assigned in some perfect matching. But computing that directly might be heavy.
Alternate approach: Build the greedy matching, then try to find an alternating cycle. Since graph is convex, maybe we can detect uniqueness by checking "tight" constraints. Could be related to "critical intervals" or something like: The matching is unique if and only if the greedy assignment yields the same as a greedy assignment from the other side? Or if the intervals form a "chain" such that each label is forced.
Maybe we can treat this as a problem of constructing the "bipartite graph's matching polytope" and check if the matching matrix is uniquely determined.
Let's think about the structure: We have positions ordered 1..n (by index). But the intervals [a_i,b_i] don't necessarily respect ordering of positions. However, we can reorder positions arbitrarily? No, positions are fixed; but the greedy algorithm we discussed sorts positions by b_i (or by a_i?) Actually, typical greedy for matching with intervals: sort intervals by right endpoint (b_i) ascending, then for each assign the smallest available label >= a_i. This works because it's like scheduling: we want to assign each interval a point, and by picking earliest possible point, we leave more points for later intervals. That yields a matching if one exists. This greedy assignment is often called the "canonical" matching. For convex graphs, it yields the "minimum" matching in the sense of lexicographically smallest assignment? It gives each position the smallest possible label it could receive in some maximum matching? Actually, it gives a matching that is "left-optimal" or "right-optimal"? There is a known property: For convex bipartite graphs, the greedy algorithm that processes left vertices in order of increasing b_i and assigns the smallest available right vertex yields the unique matching that is simultaneously the "minimum" with respect to each left vertex? Perhaps we can also compute a "maximum" matching by assigning the largest available? That might give another matching. If these two matchings are different, then there are multiple matchings. But if they are the same, does that guarantee uniqueness? Not necessarily; there could be other matchings that are not extremal. However, for convex bipartite graphs, the set of all maximum matchings has a structure: it's a lattice, and the greedy matching is the "leftmost" and the "rightmost" (if we reverse order) might be the other extreme. Actually, for bipartite matching where left vertices have intervals, there is the concept of "matching polytope" being a distributive lattice: there exist two extreme matchings: one that gives each left vertex the smallest possible matched neighbor, and one that gives the largest possible matched neighbor, across all maximum matchings. These can be computed by greedy algorithms: one by processing left vertices in order of increasing b_i and assigning smallest available label; the other by processing in order of decreasing a_i (or something) and assigning largest available label. If these two extreme matchings are the same, then all matchings are the same, hence unique. If they differ, then there are at least two. This is a known property: For the problem of assigning points to intervals, the set of feasible assignments forms a lattice under componentwise ordering, with a unique minimal and maximal solution. And if minimal = maximal, then unique. This is reminiscent of the "matching in bipartite graphs that are 'convex'" and the property that the set of maximum matchings is a distributive lattice with two extreme matchings. I think this holds: For a convex bipartite graph (intervals on left side), the set of all maximum matchings (here perfect) is known to have a "greedy" minimal and maximal matching, and they can be computed. Uniqueness is equivalent to these two being equal. Let's confirm.
Consider the typical scenario: We have intervals on the line (positions) and we need to assign distinct points. The set of all feasible assignments (if any) can be characterized: For each point (label) j, the set of intervals that can contain j. But there is a known algorithm to compute the "earliest" assignment: assign each interval the smallest point that fits, scanning by increasing right endpoint. Similarly, the "latest" assignment: assign each interval the largest point that fits, scanning by decreasing left endpoint? Actually, to get the maximum assignment (largest point for each interval), we can process intervals sorted by left endpoint descending, and assign the largest available point ≤ b_i. Let's reason: Suppose we want each interval to get as large a point as possible while still being feasible. Then we should consider intervals with larger a_i? Actually, to maximize the points assigned, we can process intervals in order of decreasing a_i (or increasing b_i reversed). For each interval, assign the largest available point ≤ b_i. That yields a feasible assignment if one exists (by symmetry). This is analogous to "matching from the other side". I recall a known problem "Assign the numbers" where the minimal assignment is greedy by right endpoints, and maximal assignment is greedy by left endpoints (but with largest available). Yes, for interval-point assignment, there is a "canonical" minimal and maximal assignment, and any feasible assignment lies between them componentwise (i.e., for each interval, its assigned point is between the minimal and maximal possible point for that interval across all feasible assignments). This is a known result: For the assignment of distinct integers to intervals, if you sort intervals by right endpoint and assign the smallest available integer, you get the minimal assignment (lexicographically smallest). If you sort intervals by left endpoint descending and assign the largest available integer, you get the maximal assignment. Moreover, the set of all feasible assignments is exactly the set of assignments that are pointwise between these two. I'm not 100% sure but I think it's true due to the theory of "matching in bipartite graphs with convex property" and the existence of a "linear extension" or "gale shapley"? There is known concept: "Greedy algorithm for bipartite matching on convex graphs yields the unique minimum-weight matching for certain weights" but we need to verify.
Let's test with an example where intervals are: pos1: [1,2], pos2: [1,2] (n=2). Minimal assignment: sort by b: both b=2, arbitrary tie-breaking might affect? If we sort by b ascending, both have b=2. If tie break arbitrarily, say pos1 then pos2: pos1 gets smallest available ≥1 => 1; pos2 gets smallest available ≥1 => 2. So minimal assignment: pos1=1, pos2=2. For maximal assignment: sort by a descending? a values: both a=1. If we sort by a descending, they are equal. Then process: assign largest available ≤ b_i. Largest available initially {1,2}. For first interval (say pos1), largest ≤2 is 2, assign 2. Next pos2: largest ≤2 is 1, assign 1. So maximal assignment: pos1=2, pos2=1. They are different. Indeed there are two matchings.
Now consider a case where intervals force uniqueness: pos1: [1,1], pos2: [2,2] (n=2). Minimal: pos1 gets 1, pos2 gets 2; Maximal: pos1 gets 1, pos2 gets 2 (since for pos1, largest ≤1 is 1; for pos2, largest ≤2 is 2). Same.
Another: pos1: [1,2], pos2: [2,2], pos3: [3,3] (n=3). Minimal: sort by b: pos2 (b=2), pos1 (b=2), pos3 (b=3). Actually tie-breaking matters? But let's see possible outcomes: For minimal, maybe pos2 gets smallest ≥2 -> assign 2? Actually smallest available ≥2 is 2. pos1 then gets smallest available ≥1 -> 1? But then pos3 gets 3. So minimal could be (pos1=1, pos2=2, pos3=3). For maximal: sort by a descending: pos1 a=1, pos2 a=2, pos3 a=3? Actually a descending: pos3(3), pos2(2), pos1(1). Process pos3: largest ≤3 is 3 assign 3; pos2: largest ≤2 is 2 assign 2; pos1: largest ≤2? But 2 taken, so largest ≤2 is 1 assign 1. So maximal yields same as minimal: (1,2,3). But is there another matching? Could we assign pos1=2, pos2=1? But pos2 interval is [2,2] so must be 2, so unique indeed.
Now consider intervals: pos1: [1,3], pos2: [2,4], pos3: [1,4], pos4: [3,4] (n=4). Not sure if unique? Let's test minimal: sort by b: pos1 b=3, pos2 b=4, pos3 b=4, pos4 b=4? Actually need to compute. Possibly minimal assignment yields some values; maximal maybe same? Not sure. But maybe it's unique if intervals are such that each label is forced.
Thus if we can compute two extreme assignments (min and max) efficiently, and they are identical, then the matching is unique; else there are at least two. Moreover, if they are different, we can output those two as distinct valid orderings. But we need to ensure that both are valid permutations. Indeed, the greedy minimal and greedy maximal algorithms produce valid matchings if a perfect matching exists. And they are distinct if not unique. But we must be careful: In some cases, the minimal and maximal assignments might be the same even though there are other matchings different from them? Could that happen? That would imply the set of all feasible assignments is a singleton, because the minimal and maximal being equal forces all assignments to be that single one. Since any feasible assignment lies between min and max componentwise? Let's examine that property. If it's true that for any feasible assignment, for each position i, the assigned label is between the min label (smallest possible for that position across all matchings) and max label (largest possible), then min=max would imply only one possible assignment for each i, thus only one matching. So we need to verify that the min and max we compute are indeed the componentwise minimum and maximum across all matchings.
I recall a known theorem: In a bipartite graph that is "convex on one side", the set of maximum matchings forms a distributive lattice. In particular, there exists a unique "leftmost" matching where each left vertex is matched to the smallest possible neighbor, and a unique "rightmost" matching where each left vertex is matched to the largest possible neighbor. Moreover, for any maximum matching, the matched neighbor of each left vertex lies between the leftmost and rightmost values. This is similar to the "stable marriage" property? I think it's true for convex bipartite graphs. Let's reason:
Define the graph: left vertices L (positions), right vertices R (labels 1..n). For each left vertex i, its neighbor set is an interval [a_i, b_i]. This is a convex bipartite graph where the right vertices are ordered.
Claim: There exists a maximum matching that is "left-optimal" meaning that for each left vertex i, the matched label is as small as possible, given the matching is a maximum matching. This can be obtained by the greedy algorithm described.
Proof sketch: The greedy algorithm that processes left vertices in increasing order of b_i, assigning the smallest available right vertex ≥ a_i, yields a maximum matching. Moreover, it's known that this matching is "lexicographically smallest" in terms of the labels assigned to left vertices when ordered by some order? Actually, since we process by b_i, the resulting matching has the property that if we consider any other maximum matching, for the left vertex with smallest b_i (or earliest processed), the greedy assigns the smallest possible label it can have in any maximum matching; then inductively, after fixing that, the rest are minimized. This is typical exchange argument. So indeed the greedy yields the minimal possible label for each left vertex in the sense that for each i, the label assigned is the minimum over all maximum matchings of the label that can be assigned to i. Similarly, processing in the opposite order (by a_i descending, assign the largest available ≤ b_i) yields the maximum possible label for each left vertex across all maximum matchings.
Thus, if these two matchings coincide, then for each i the min and max are equal, meaning only one possible label for each i, thus unique matching. Conversely, if they differ, there are at least two matchings (the min and max themselves are distinct, provided they are both valid). So we can use this approach.
We need to implement:
- Check feasibility (but input guarantees at least one valid ordering, but our algorithms should succeed if it's feasible. However, we need to ensure the greedy algorithms indeed find a perfect matching. Since it's guaranteed at least one exists, they should. But we must be careful: The greedy algorithm might fail if we use a particular order? But the known result: For interval-point assignment, the greedy (by b_i) always finds a matching if one exists. However, we need to ensure tie-breaking doesn't cause failure. Usually, we sort intervals by b_i, and if b_i equal, we might sort by a_i? But it shouldn't matter because any order of intervals with equal b_i works? Let's think: Suppose we have two intervals with same b_i but different a_i. The greedy will assign the smallest available point to the first one. Could that cause later interval to fail even though a matching exists? Example: n=2, intervals: pos1: [1,2], pos2: [2,2]. Both b_i =2. If we process pos1 first: assign smallest >=1 => assign 1. Then pos2: smallest >=2 => assign 2 (since 2 free). Works. If we process pos2 first: assign smallest >=2 => assign 2, then pos1: smallest >=1 => assign 1. Works. So any order works? Another example: intervals: pos1: [2,2], pos2: [1,2]. Both b=2. Process pos1 first: assign smallest >=2 => assign 2; then pos2: smallest >=1 => assign 1. Works. Process pos2 first: assign smallest >=1 => assign 1; then pos1: smallest >=2 => assign 2. Works. So seems fine.
But what about more complex case: intervals: [1,3], [2,3], [3,3] (n=3). Any order works? Greedy by b: all b=3. Suppose we process [3,3] first: assign smallest >=3 => assign 3. Then [2,3]: smallest >=2 => assign 2. Then [1,3]: smallest >=1 => assign 1. Works. If we process [1,3] first: assign 1; [2,3] assign 2; [3,3] assign 3. Works. So any order of equal b_i seems ok.
But is there a scenario where the greedy fails if not sorted by b_i? If we sort by something else, it might fail. So we must sort by b_i (and maybe a_i for tie-break to be deterministic). Similarly, for maximal, we need to sort by a_i descending (or maybe by b_i descending?) Let's derive.
For maximal assignment: We want each left vertex to get as large label as possible. The typical algorithm: sort intervals by a_i descending (or by b_i descending?), and assign to each the largest available point that is ≤ b_i. Let's test: intervals: [1,2], [1,2] (n=2). Sort by a descending: both a=1, tie arbitrary. Process first: largest ≤2 is 2, assign 2; second: largest ≤2 is 1, assign 1. Works, gives maximal. Sorting by b descending: both b=2, assign first: largest ≤2 =2; second gets 1. Same result. But consider intervals: [1,3], [2,3], [2,2] (n=3). Feasible matchings? Let's see: labels 1,2,3. [2,2] must get 2. Then remaining positions: [1,3] and [2,3] get 1 and 3. So two possibilities: assign 1 to [1,3] and 3 to [2,3] or 3 to [1,3] and 1 to [2,3]? But [2,3] cannot get 1 because 1 not in [2,3]. Actually [2,3] interval is [2,3], so it can only take 2 or 3. Since 2 is taken by [2,2], [2,3] must take 3. Then [1,3] takes 1. So unique? Actually [1,3] can take 1 or 3, but 3 is forced to [2,3], so [1,3] gets 1. So unique. Let's test maximal algorithm: sort by a descending? a values: [2,2] a=2, [2,3] a=2, [1,3] a=1. So process first: (a=2) say [2,2]: largest ≤2 is 2 assign 2; next (a=2) [2,3]: largest ≤3 is 3 assign 3; next [1,3]: largest ≤3 is 1 (since 2,3 taken) assign 1. So assignment: [2,2]=2, [2,3]=3, [1,3]=1. That's the only assignment (since [2,2] forced 2, [2,3] forced 3). Good.
Now consider intervals: [1,4], [2,4], [3,4], [4,4] (n=4). Minimal: sort by b: all b=4. Process in any order? Might get e.g., assign 1,2,3,4 respectively. Maximal: sort by a descending? a: [4,4]=4, [3,4]=3, [2,4]=2, [1,4]=1. Process [4,4]: largest ≤4 =4 assign 4; [3,4]: largest ≤4 now 3? Actually 4 taken, so largest ≤4 is 3 assign 3; [2,4]: largest ≤4 is 2 assign 2; [1,4]: largest ≤4 is 1 assign 1. So both yield same assignment. So unique.
Now consider intervals that cause difference: [1,2], [1,2], [3,3] (n=3). Minimal: sort by b: [1,2] b=2, [1,2] b=2, [3,3] b=3. Process first two: assign 1,2 respectively; then [3,3] assign 3. Maximal: sort by a descending? a: both [1,2] have a=1, [3,3] a=3. So process [3,3] first: largest ≤3 =3 assign 3. Then process [1,2] (a=1) in some order: first gets largest ≤2 =2 assign 2; second gets 1. So assignments differ: minimal gives (pos? actually positions matter but we output permutations). So distinct.
Thus the approach seems plausible.
Now, we must implement these two greedy matchings efficiently for n up to 2e5.
Algorithm for minimal matching (leftmost):
- Prepare list of positions with intervals [a_i, b_i], index i (original position). We need to assign each position a label from 1..n.
- Sort positions by b_i ascending (if tie, maybe by a_i ascending or any, but to ensure deterministic and correct, we can sort by b_i, then a_i? Actually tie-breaking might affect the resulting matching, but the property is that any order that respects b_i (i.e., non-decreasing b_i) works? Actually, the greedy algorithm typically processes intervals in order of increasing b_i. If b_i are equal, the order among them doesn't matter because they have the same right endpoint. However, to ensure we get the "minimal" assignment (the one that minimizes each label in a certain sense), we may need to break ties appropriately. But I think any order among equal b_i yields the same set of assignments? Not necessarily, could yield different matchings? Let's test: intervals: [1,2] and [1,2] have equal b. If we process in order posA then posB, we assign 1 to A, 2 to B. If we process B then A, assign 1 to B, 2 to A. These are two different matchings. Both are valid. Which one is the "minimal" matching? The concept of "minimal" might be defined with respect to a particular ordering of left vertices. Usually, the leftmost matching is defined such that the vector of matched labels sorted by some order (maybe left vertices order) is lexicographically minimal. Since our left vertices have fixed indices (positions), we can define the minimal assignment as the one that, for each position i, the label is as small as possible? But we might not be able to simultaneously minimize all positions; there is a trade-off. However, the greedy by b_i gives a matching that is "optimal" in the sense that it assigns to each interval the smallest possible label that can be assigned to it in any feasible assignment, given that earlier intervals (with smaller b) have been assigned the smallest possible. I recall the following property: For the interval assignment problem, if you process intervals in increasing order of b_i and assign the smallest available number ≥ a_i, the resulting assignment is the unique one that is "lexicographically minimal" when considering the labels assigned to intervals in the order of their b_i (or maybe by their indices?). But we need to compute the componentwise minimum across all matchings? Actually, is it true that for each interval i, the label assigned by this greedy is the minimum possible among all feasible assignments? Let's examine with example: two intervals both [1,2]. The feasible assignments are (1,2) and (2,1). For interval 1 (say first interval), the possible labels are 1 and 2. The minimum label that interval 1 can receive is 1. In the greedy assignment, depending on which interval we consider as "first" in processing, interval 1 could get 1 or 2. If we process by b_i and the intervals have same b, then the order among them is arbitrary. So if interval 1 is processed first, it gets 1 (the smallest available). If processed second, it gets 2. So the label assigned to a particular interval is not necessarily the minimum possible across all matchings; it depends on processing order. So we cannot claim that the greedy yields a matching where each position gets the minimum possible label across all matchings, because the "minimum" for a particular position might be 1, but if that position is processed later, it might get 2. However, the existence of multiple matchings means that for some positions, the minimum possible label is 1 but the maximum possible is 2. So we need a consistent definition of minimal and maximal matchings.
The property that there is a unique leftmost matching that is simultaneously minimal for all positions under some ordering? Actually, in convex bipartite graphs, there is a unique "maximum matching that is lexicographically smallest with respect to the order of right vertices" when considering the matching as a set of edges? But more relevant: The set of maximum matchings has a lattice structure where there is a matching M_min such that for every left vertex v, the neighbor matched to v is the smallest among all maximum matchings. And similarly M_max where it's the largest. This is known to be true for "convex bipartite graphs". I recall a result: In a bipartite graph with one side convex, there exists a matching that is "left-optimal" meaning that for each left vertex, it matches to the smallest possible neighbor, and this matching can be found by a greedy algorithm that processes left vertices in order of increasing b_i, and for each, matches to the smallest available neighbor. However, this requires that left vertices are processed in a specific order: not arbitrary, but by increasing b_i. And for left vertices with same b_i, we need to decide order to achieve that property? The claim might be that regardless of order among those with same b_i, the resulting matching will have that property? Let's test with two intervals both [1,2]. If we process in some order, the first gets 1, second gets 2. Does the resulting matching have the property that each left vertex gets the smallest possible neighbor? For the first processed (say interval A), it gets 1, which is indeed the smallest possible for A (since A can be 1). For the second processed (B), it gets 2. But B could also get 1 in another matching (if A gets 2). So the smallest possible neighbor for B is 1. However, in this matching, B got 2, which is not the smallest possible. So the matching is not "each left vertex gets the smallest possible". So the greedy by b_i does not guarantee that each vertex gets its individually smallest possible; it ensures a form of "optimality" but not per-vertex min across all matchings. But maybe the "leftmost" matching is defined such that the sequence of matched labels for left vertices sorted by b_i is lexicographically minimal. That is, if we list left vertices in order of increasing b_i (breaking ties by some rule), the vector of assigned labels (in that order) is lexicographically smallest among all matchings. That yields a unique matching (for a fixed tie-breaking). Then the per-vertex minima are not guaranteed.
Thus, the notion of componentwise min and max across all matchings might not be attained by a single matching. In the example [1,2] and [1,2], the set of possible labels for each interval is {1,2}. So the minimum per vertex is 1 and maximum is 2. But there is no single matching that assigns both vertices 1 (impossible). So the componentwise minima across all matchings (taken pointwise) is not necessarily realizable by a single matching. So the "minimal matching" as a whole, if we try to minimize all labels simultaneously, is not possible. Instead, the concept of a "minimum" matching is usually defined with respect to a total order on matchings, e.g., lexicographic order by the labels assigned to left vertices in a fixed order.
But we need to find two distinct matchings if any exist. The approach of comparing the greedy by b_i and greedy by a_i descending might still produce two different matchings when there are multiple matchings, but could it fail to detect non-uniqueness? That is, could there be multiple matchings while both greedy algorithms (with specific tie-breaking) produce the same matching? Possibly if the greedy algorithms are deterministic and yield the same matching despite there being other matchings. Let's try to find a counterexample.
We need a scenario where there are at least two distinct perfect matchings, but both greedy algorithms (min-style and max-style) yield the same matching. That would mean the "min" and "max" matchings (under those algorithms) coincide, but there exists another matching different from them. However, if the min and max matchings (according to those algorithms) are the same, does it imply uniqueness? Not necessarily, because the algorithms might not explore all extremes. But if we define the "leftmost" and "rightmost" matchings properly (the ones that are truly the extremal in the lattice), they should be unique and if they coincide, the lattice is a singleton. So we need to correctly compute the extremal matchings.
Thus we must ensure that our greedy algorithms indeed produce the extremal matchings: the one that gives each left vertex the smallest possible label in any maximum matching, and the one that gives each left vertex the largest possible label in any maximum matching. I need to confirm that such a notion exists and is computed by specific greedy algorithms.
Research: In a convex bipartite graph, the set of maximum matchings has a distributive lattice structure. There is a "canonical" minimum matching and maximum matching. They can be obtained by the following: Minimum matching: sort left vertices by increasing b_i; for each, match it to the smallest available right vertex ≥ a_i. Maximum matching: sort left vertices by decreasing a_i; for each, match it to the largest available right vertex ≤ b_i. This is known, I've seen it in problems like "Restore the Permutation" (CF) or "Matching with intervals". Let's check with our example [1,2] and [1,2]. Process by increasing b_i (both b=2). If we sort by increasing b_i and then maybe by increasing a_i (or any deterministic tie-break), the order of processing is fixed. Suppose we break ties by index (original order). Then the algorithm will assign to first processed interval the smallest available (1) and to second the next (2). That yields matching M1. For the maximum matching, sort by decreasing a_i (both a=1) and then perhaps by decreasing b_i or by index? Actually decreasing a_i yields same order. Then assign largest available ≤ b_i: for first processed, largest ≤2 is 2, assign 2; second gets 1. So M2 is different. So min and max differ, correctly indicating non-uniqueness.
Now consider a scenario where there are multiple matchings but both greedy algorithms could accidentally yield the same matching due to tie-breaking? Could there be a case where no matter how you break ties, the min and max greedy produce the same matching? If there is a unique matching, they will be same. If multiple matchings, then there must be at least two distinct matchings. The extremal matchings (leftmost and rightmost) are unique; they are the matchings that are lexicographically smallest and largest when considering the sorted order of left vertices by some criterion. If we use different tie-breaking, we might not get the true extremal ones, but we might still get two matchings that are possibly the same even though extremal ones differ? That would require that under our specific tie-breaking, both greedy algorithms produce the same matching, even though the true leftmost and rightmost are different. Could that happen? Possibly if tie-breaking influences the outcome heavily. But if the true leftmost and rightmost are different, then there exist at least two distinct matchings. Our greedy algorithms, if implemented correctly to yield leftmost and rightmost, should output different ones. But we need to ensure our greedy algorithms produce exactly those extremal matchings, not just some arbitrary matchings.
Let's formalize: For a convex bipartite graph, let the left vertices be intervals [a_i,b_i]. Suppose we sort left vertices by b_i ascending. Then define matching M as follows: iterate i in that order, assign to each the smallest available right vertex ≥ a_i. Claim: This yields a maximum matching that is "left-optimal" meaning that for any other maximum matching M', the label assigned to the first vertex in this order (smallest b) is ≤ the label assigned in M'. And among those with equal, the next, etc. So M is the lexicographically smallest vector (in terms of assigned labels) when vertices are ordered by increasing b_i. This is a known property. Similarly, sorting by a_i descending and assigning the largest available ≤ b_i yields the lexicographically largest vector (when ordered by decreasing a_i). These two matchings are unique given the sorting order (with tie-breaking consistent, e.g., by index). If we break ties arbitrarily, we might get different matchings, but still each such matching is a maximum matching. However, for the extremal property to hold, the order must be consistent: For the min matching, we need to process vertices in non-decreasing b_i. If there are ties, we need to process those with smaller a_i first? Or something? Let's reason: To achieve the lexicographically smallest assignment under the order of b_i, when b_i are equal, we need to decide an order among them. The lexicographic order is defined with respect to that order; so any order yields a matching that is lexicographically smallest for that particular ordering? Actually, if we fix an order (like by b_i then by index), then the greedy algorithm that processes in that order and assigns the smallest possible yields the lexicographically smallest matching with respect to that order (the vector of assigned labels in that order). That matching is unique for that order. But if we choose a different tie-breaking, we get a different vector; it's lexicographically smallest for that order but the order is different. So the "leftmost" matching as defined by the property "for each left vertex, the label is the minimum possible among all maximum matchings" might not exist because of conflicts. However, there is a known concept: In a convex bipartite graph, the set of maximum matchings forms a distributive lattice where the partial order is defined by comparing the labels assigned to each left vertex (pointwise). Actually, the lattice is on the set of matchings where for two matchings M and M', we say M ≤ M' if for every left vertex v, M(v) ≤ M'(v). This is a partial order. Does this partial order have a unique minimum? In the example with two intervals [1,2], the matchings are (1,2) and (2,1). Compare them: For vertex 1, 1 ≤ 2; for vertex 2, 2 ≥ 1, so they are incomparable. So there is no unique minimum. So the componentwise minimum does not exist. So the notion of "minimum matching" as pointwise minimum is not defined in general. The extremal matchings in the lattice are not necessarily comparable pointwise; they are extremes under a different order, perhaps a linear extension.
Thus the approach of comparing two greedy matchings may not guarantee detection of multiple matchings. But we might still be able to use these two greedy matchings as witnesses: if they are different, we have two distinct matchings. If they are the same, can we conclude uniqueness? Not necessarily; there could be other matchings different from this greedy one, but maybe the greedy ones are the same. Let's try to construct a case where there are multiple matchings but both greedy algorithms (with some deterministic tie-breaking) produce the same matching. Could that happen? Let's attempt.
We have intervals. The min greedy (by b_i) and max greedy (by a_i descending) use different orders. If there are multiple matchings, it's plausible that these two algorithms yield different outcomes because they are "extremal" in different directions. But I'm not entirely sure.
Consider intervals:
pos1: [1,3]
pos2: [1,2]
pos3: [2,3]
n=3.
Let's label positions 1,2,3 with intervals. Is there a unique perfect matching? Let's list possible assignments. Labels 1,2,3.
We need to assign each label to a position within its interval.
Check possibilities:
- pos2 must get either 1 or 2.
- pos3 must get either 2 or 3.
- pos1 can get 1,2,3.
We need a permutation.
Case: assign pos2=1. Then pos3 could be 2 or 3. If pos3=2, then pos1=3 (since 1,2 taken). Check: pos1:3∈[1,3] ok. If pos3=3, then pos1=2 (2∈[1,3]) ok. So two matchings: (pos1=3,pos2=1,pos3=2) and (pos1=2,pos2=1,pos3=3). Also case: assign pos2=2. Then pos3 cannot be 2, so pos3 must be 3. Then pos1=1 (1∈[1,3]). So third matching: (1,2,3). So there are at least 3 matchings.
Now compute min greedy: sort by b_i ascending. Intervals:
pos1: b=3
pos2: b=2
pos3: b=3
Sort order: pos2 (b=2), then pos1 and pos3 (b=3). For tie-breaking among b=3, we need a rule; suppose we break by index (pos1 then pos3) or maybe by a_i? Let's decide a deterministic rule: we can sort by (b_i, a_i, index) ascending. That is typical. For pos1: (3,1,1); pos2: (2,1,2); pos3: (3,2,3). So order: pos2, pos1, pos3.
Process:
- pos2: interval [1,2]. smallest available >=1: label 1 (available). assign 1.
- pos1: [1,3]. smallest available >=1: now 1 taken, so 2 is smallest available >=1. assign 2. (check 2∈[1,3])
- pos3: [2,3]. smallest available >=2: now available {3}? Actually labels taken: 1,2; smallest available >=2 is 3? 2 is taken, so 3. assign 3. So matching: (pos1=2, pos2=1, pos3=3). That's one matching.
Now max greedy: sort by a_i descending (and maybe b_i descending or index). Let's define: sort by a_i descending, then b_i descending? Actually to get the "largest" assignment, we can process in order of decreasing a_i, and for each assign the largest available ≤ b_i. Alternatively, sometimes algorithm is: sort by a_i descending, and assign the largest available ≤ b_i. Let's test.
Intervals:
pos1: a=1
pos2: a=1
pos3: a=2
Sort by a descending: pos3 (a=2), then pos1 and pos2 (a=1). Tie break among a=1: maybe by b descending? Or by index? Let's try by index descending or something. But to see if we get same matching as min greedy.
Suppose we break ties by index descending or by b descending. Let's try both:
Option 1: sort by (a descending, b descending, index descending). Then order: pos3 (2,3,3), then pos1 (1,3,1) and pos2 (1,2,2). For a=1, b descending gives pos1 (b=3) then pos2 (b=2). So order: pos3, pos1, pos2.
Process:
- pos3: [2,3]. largest available ≤3: label 3. assign 3.
- pos1: [1,3]. largest available ≤3: now available {1,2}. largest is 2 (since 3 taken). assign 2.
- pos2: [1,2]. largest available ≤2: now available {1}. 1 ≤2, assign 1.
Result: (pos1=2, pos2=1, pos3=3). Same as min greedy!
So both greedy algorithms gave the same matching, even though there are multiple matchings. So this is a counterexample to the idea that if min and max greedy produce the same matching, then the matching is unique. Here we have at least 3 matchings, but both algorithms gave the same one (under our tie-breaking). So we cannot rely on just these two greedy matchings to detect non-uniqueness.
But note: In our max greedy, we used a specific tie-breaking. Could we change the tie-breaking to get a different max matching? Possibly, but we need a deterministic algorithm to produce two distinct matchings if they exist. We could try to compute some extreme matchings that are guaranteed to be the "leftmost" and "rightmost" in a sense that they differ if multiple matchings exist. The literature might define them by processing in order of b_i for min and processing in order of a_i for max, but with tie-breaking that ensures they are the two extremes of the lattice of all matchings. But the lattice might have multiple incomparable matchings; the extremes are defined under a different partial order: For convex bipartite graphs, there is a well-known concept of the "greedy matching" that yields the unique matching that is "maximum" with respect to some order, but the set of all maximum matchings can be represented as an interval of matchings when considering the "matching vector" sorted by left vertices order? Actually, there is a known result: If we fix an ordering of left vertices, then the set of maximum matchings forms a distributive lattice under the partial order of the labels assigned to left vertices (pointwise) if the graph is "convex" and the intervals are "nested"? Hmm.
Wait, recall: For the assignment of points to intervals, the set of all feasible assignments can be described by the Hall's conditions; there is a known algorithm to compute the set of all possible assignments: For each interval, the range of labels that can be assigned to it in some feasible assignment is an interval [L_i, R_i]. And these ranges can be computed via a sweep algorithm. Then, the assignment is unique iff for each i, L_i == R_i. And we can construct the unique assignment directly. If not unique, we can produce two assignments by, say, constructing one that assigns each i the lower bound L_i (if possible) and one that assigns each i the upper bound R_i (if possible). But are these always feasible? In many matching problems with intervals, the set of possible labels for each interval is indeed an interval, and there exists a feasible assignment achieving the lower bounds simultaneously, and another achieving the upper bounds simultaneously. I recall a classic result: For the problem of assigning distinct numbers to intervals (a bipartite matching in a convex graph), the set of all feasible assignments forms a "convex set" in the sense that for each interval, the set of possible values is a contiguous range. Moreover, there exist assignments that are "earliest" and "latest" that realize these bounds for all intervals simultaneously. I think this is true because of the matroid intersection structure: It is a transversal matroid? Actually, this is similar to the "scheduling with release times and deadlines" but with distinct times. The constraints are: each job i must be assigned a distinct time slot t_i in [a_i,b_i]. The existence of a feasible schedule is given. Then the set of feasible schedules forms a "polytope" with the property that the earliest possible time for each job (the minimum t_i over all feasible schedules) can be achieved simultaneously (i.e., there is a schedule where each job gets its minimum possible time). Similarly, the latest possible time for each job can be achieved simultaneously. This is known from the theory of "consistent schedules" or "timelines". I think it's related to the "Jackson's rule" or "topological ordering"? Not exactly.
Let's reason: We have n jobs, each with a time window [a_i,b_i], and we need to assign distinct times 1..n. This is equivalent to a bipartite matching. The set of feasible assignments is the set of permutations satisfying a_i ≤ p_i ≤ b_i. The question: For each job i, define L_i = min { p_i over all feasible permutations } and R_i = max { p_i over all feasible permutations }. Is it true that there exists a feasible permutation where each job i gets L_i? And similarly where each gets R_i? If so, then uniqueness is equivalent to L_i = R_i for all i. And we could compute L_i and R_i via some algorithm, then test equality. If not equal, we can construct the two permutations (one using L_i, one using R_i) as two distinct valid permutations. This would solve the problem.
Is this true? Let's test on the counterexample earlier: positions/intervals:
1: [1,3]
2: [1,2]
3: [2,3]
Compute L_i and R_i manually.
All feasible permutations:
- (1,2,3) -> p1=1, p2=2, p3=3
- (2,1,3) -> 2,1,3 (but check pos3=3? Wait p3=3, pos3 interval [2,3] ok; pos2=1 ∈ [1,2]; pos1=2 ∈ [1,3]) Actually (2,1,3) works: p1=2,p2=1,p3=3.
- (3,1,2) -> 3,1,2 (p1=3∈[1,3], p2=1∈[1,2], p3=2∈[2,3]) works.
- (2,? Actually also (1,?,?) There is also (3,2,1)? p1=3,p2=2,p3=1? p3=1 not in [2,3] so no.
- (1,?,?) also (1,2,3) we have; (1,? ) pos2 must be 1 or 2; if p1=1, then p2=2, p3=3 works; if p1=1, p2=1 impossible. So (1,2,3) and (1,?,?) maybe (1,?,?) only one.
- (2,?,?): if p1=2, then p2 could be 1, p3=3 -> (2,1,3). Or p2=?? 2 taken, so p2 must be 1, p3=3; no other.
- (3,?,?): if p1=3, p2 could be 1, p3=2 -> (3,1,2); or p2=2, p3=1 invalid. So total 3 matchings.
Now compute L_i:
For pos1, possible p1 values: 1,2,3. Min =1.
pos2: possible values: 1,2? In all matchings, pos2 gets 1 or 2. Min =1.
pos3: possible values: 2,3. Min =2.
Now can we have a feasible assignment where p1=1, p2=1? No, because distinct. So can we achieve simultaneously p1=1, p2=1, p3=2? No, because p1 and p2 both 1 conflict. So the vector of minima (1,1,2) is not feasible. So there is no assignment where each position gets its individual minimum. So L_i are not simultaneously achievable. Therefore, the claim that there exists a permutation achieving all lower bounds is false. So we cannot simply take L_i as a permutation.
However, note that the minima per position might conflict; but the set of feasible assignments might still have some structure. The earlier thought that there is an "earliest" schedule where each job gets its earliest possible time might be true if the windows are "release times" and "deadlines" and we schedule in order of deadlines, but that algorithm yields a schedule (the "greedy schedule") which gives each job a time that is not necessarily its absolute minimum possible, but it yields a schedule that is "feasible" and in some sense "minimal" in terms of the order of times. But the schedule produced by the greedy algorithm (by deadlines) yields a particular assignment; this assignment may not give each job its absolute minimum possible time, but it gives the "minimal" in a different sense: it minimizes the vector of completion times lexicographically when jobs are sorted by deadlines.
So the idea of using two greedy matchings (by b_i and by a_i descending) gave the same assignment in the example despite multiple matchings. Therefore, we cannot rely on them to detect uniqueness.
We need a different method.
We need to determine if there is exactly one permutation, and if not produce two. n up to 2e5, so we need O(n log n) or O(n) solution.
Let's think more about the structure.
We have a bipartite matching problem in a convex graph. The uniqueness of a perfect matching in a bipartite graph can be characterized by the existence of an alternating cycle. For convex graphs, we might detect an alternating cycle efficiently.
Alternatively, we can use the concept of "critical intervals" and "tight" constraints: The assignment is unique if and only if there is a certain forcing process that fixes each label to a position.
Another approach: Since the graph is convex, we can perhaps use a greedy algorithm to assign labels and then check if any swap is possible. For uniqueness, we need that for every edge in the matching, removing it makes the matching impossible? Not exactly.
Maybe we can compute the set of possible labels for each position using a flow / Hall's theorem and then check if for each position, the allowed set reduces to one label after considering constraints. But that set could be computed via a "matching flexibility" algorithm, like using the concept of "reachability" in the alternating graph.
Given the constraints are intervals, there is known result: In a bipartite graph with intervals on the left, the set of all maximum matchings can be represented as a set of "critical edges". The graph can be reduced to a set of forced assignments if and only if certain conditions hold.
We could also use the "Hungarian algorithm" but too slow.
Observation: Since labels are numbers 1..n, and intervals are ranges, we can think of assigning labels to positions. This is similar to constructing a permutation p such that a_i ≤ p_i ≤ b_i. This is a classic problem known as "constructing a permutation from lower and upper bounds" or "matching with intervals". Uniqueness can be decided by checking if the greedy algorithm yields a unique result irrespective of choices. But we saw that greedy by deadlines might give a unique result even if multiple matchings exist, depending on tie-breaking. But if we define a deterministic algorithm that always produces the same matching (say, by also using indices to break ties), then uniqueness means that this deterministic algorithm's output is the only possible matching. But that's not a necessary condition: there could be multiple matchings, but the deterministic algorithm always outputs one particular matching. However, we could try to also produce a different matching by some other deterministic algorithm (like the reverse greedy). If they produce the same, we cannot conclude uniqueness as shown. So we need a more thorough check.
We need to either find an alternating cycle or prove uniqueness.
Alternate plan: Since the graph is convex, we can use a segment tree or data structure to compute a matching and then test if each matched edge is "essential". For each edge (i, j) in the current matching, we could check if there exists an alternating cycle that avoids that edge, meaning the matching is not forced. But we need overall uniqueness.
Simplify: The problem reduces to: given intervals [a_i,b_i], is there exactly one permutation p with a_i ≤ p_i ≤ b_i? This is reminiscent of the problem of determining if a bipartite graph with intervals has a unique perfect matching. I recall a known result: For convex bipartite graphs, the perfect matching is unique if and only if the greedy algorithm (assign smallest available) yields the same as the greedy algorithm that assigns largest available, when using a specific order: process by increasing b_i for min, and by decreasing a_i for max, but with tie-breaking that ensures that they are indeed the two extremes of the lattice of matchings. However, our example showed they gave the same, but maybe we used the wrong tie-breaking for the max algorithm? Let's reexamine: In the example, we had intervals:
1: [1,3]
2: [1,2]
3: [2,3]
We computed min greedy: sort by b_i (then a_i?). b_i values: pos2 b=2, pos1 b=3, pos3 b=3. Sorting by b_i ascending, then by a_i ascending: pos2 (a=1), then pos1 (a=1,b=3), then pos3 (a=2,b=3). That gave assignment (pos2=1, pos1=2, pos3=3).
Now for max greedy: we need to compute the matching that maximizes the assignment vector in some sense. Usually, to get the "rightmost" matching, you process intervals in order of decreasing a_i, and assign the largest available label ≤ b_i. But is that guaranteed to give the "maximum" matching? Let's try a different order for max: sort by a_i descending, and if tie, by b_i descending? We used that: pos3 (a=2,b=3), pos1 (a=1,b=3), pos2 (a=1,b=2). That gave (pos3=3, pos1=2, pos2=1). Same as min.
What if we sort by b_i descending instead? That is another common algorithm for "latest" assignment: sort by b_i descending, assign largest available ≤ b_i. Let's test that: sort by b_i descending: pos1 (b=3,a=1), pos3 (b=3,a=2), pos2 (b=2,a=1). But to break tie among b=3, maybe by a descending? So order: pos3 (b=3,a=2) then pos1 (b=3,a=1) then pos2. Process: assign largest available ≤ b_i.
- pos3: [2,3], largest available ≤3 is 3, assign 3.
- pos1: [1,3], largest available ≤3 is 2 (since 3 taken), assign 2.
- pos2: [1,2], largest available ≤2 is 1, assign 1. Same matching.
Sort by b_i descending but tie break by a ascending? pos1 then pos3 then pos2: pos1 assign largest ≤3 =3; pos3 assign largest ≤3 =2 (since 3 taken); pos2 assign 1. That yields (pos1=3, pos3=2, pos2=1) which is a different matching! Let's compute: pos1=3, pos3=2, pos2=1 -> check: pos1=3∈[1,3], pos2=1∈[1,2], pos3=2∈[2,3]. That's valid. So if we process by b_i descending with tie-breaking by a_i ascending (i.e., smaller a first), we get a different matching (pos1=3, pos3=2, pos2=1). So this is a distinct matching from the min greedy. So the "max" algorithm might depend on tie-breaking. The algorithm that processes by decreasing a_i might not be the true "rightmost" in all definitions; perhaps the correct "rightmost" is obtained by sorting by a_i descending? But we got two different matchings from that: one gave (2,1,3) another gave (3,1,2)? Actually, we need to verify.
Let's compute the set of all matchings for this example:
- (pos1=2, pos2=1, pos3=3) (our min greedy)
- (pos1=3, pos2=1, pos3=2) (alternate)
- (pos1=1, pos2=2, pos3=3) (another)
Which is the "rightmost" or "max" in some sense? If we think of maximizing the labels for positions in some order (e.g., lexicographically by position index), then we can define a matching that is maximal. But there might be multiple maximal elements under pointwise order. So the notion of a unique "maximum" matching does not exist because the partial order is not total.
Thus, the approach of comparing two greedy matchings with fixed tie-breaking may not guarantee that they are different when multiple matchings exist. As seen, with min greedy we got (2,1,3). With max greedy using b descending and a ascending we got (3,1,2). So those two are different, indicating non-uniqueness. But with our earlier tie-breaking for max (a descending, b descending) we got same as min. So if we choose a deterministic rule, we could accidentally pick tie-breaking that yields the same matching for both min and max even when other matchings exist. So we need a more robust method.
Maybe we can use the idea of constructing two different matchings directly if they exist, without relying on extremal ones. For uniqueness detection, we could attempt to find an alternating cycle in O(n log n) by using some data structure. Since the graph is convex, alternating cycles might have a special structure.
Let's try to characterize when there are multiple matchings. Consider the assigned matching from the greedy algorithm (any one). Then check if we can perform a swap: find two positions i and j such that p_i and p_j can be swapped while staying within intervals. That is, p_i ∈ [a_j, b_j] and p_j ∈ [a_i, b_i]. If such a swap exists, then we have another matching. But is it sufficient? Could there be multiple matchings without a simple 2-cycle (swap)? Possibly a longer cycle. But any alternating cycle of even length yields another matching. If we find any alternating cycle, we can produce another matching by flipping edges. So we can try to detect if there exists any alternating cycle in the current matching M. If yes, then multiple matchings; else M is unique? In bipartite graphs, if there is no alternating cycle, the matching is unique. Because if there are two distinct perfect matchings M and M', their symmetric difference consists of disjoint alternating cycles. So existence of another perfect matching is equivalent to existence of an alternating cycle in M (since M' exists, M ⊕ M' is a set of alternating cycles). So we just need to test if the current matching M (any perfect matching) contains an alternating cycle (i.e., if the graph has another perfect matching). Since we have at least one perfect matching (given), we can pick any one (e.g., from greedy) and then check if there is an alternating cycle. If there is, we can find one and produce M' by flipping along that cycle.
Thus, the problem reduces to: given a convex bipartite graph with a perfect matching M (found via greedy), determine if there exists an alternating cycle, and if so, find one. Since n up to 2e5, we need an efficient method.
Observation: The graph is convex on the left side: for each position i, its allowed labels form an interval [a_i, b