We need to find an array a of length n (non-negative integers) that maximizes the minimal mex among m given subarrays. We have constraints n,m up to 1e5. Each subarray [l_i, r_i]. We want to assign values to a[1..n] such that for each subarray, its mex is at least some value k, and we want maximize the minimum mex. Essentially we want to find the largest k such that we can assign a values (each between 0 and 1e9, but we can use small numbers) so that for every subarray, its mex >= k. Then the minimal mex among them will be at least k. Actually we need minimal mex to be as large as possible. So we want to assign a to maximize min_i mex_i. Let M = min_i mex_i. We want max M.
Observation: mex of a set is the smallest non-negative integer not present. So for mex to be at least k, the subarray must contain all numbers 0,1,...,k-1. Because if any number from 0..k-1 is missing, the mex would be that missing number, which is < k. Conversely, if it contains all numbers 0..k-1, then mex could be at least k (it could be exactly k if k is not present, or larger if k is missing but all smaller present). Actually mex >= k means the smallest missing integer is at least k, so indeed all numbers 0..k-1 must be present. So condition: For each subarray, it must contain all integers 0,1,...,k-1. But we also have to ensure that the mex is not forced to be exactly k? Actually if subarray contains all 0..k-1, mex is at least k. So that's sufficient.
Therefore, to achieve min mex >= k, we need for each subarray, it contains all numbers from 0 to k-1. That is, for each j in [0,k-1], the subarray must include at least one occurrence of j. So we need to assign values to positions so that each subarray covers all those numbers.
Our goal: maximize k such that we can assign values (with possible repeats) to satisfy: for each given interval [l_i, r_i], the set of values in that interval includes all numbers from 0 to k-1. Also, we can assign any values (including > k-1) arbitrarily.
We need to output the maximum possible minimum mex and the array.
Now we must also consider that the subarray's mex could be larger than k if also includes k, but that's fine. So the minimal mex among them will be at least k. But we want to maximize the minimum, so we want largest k achievable.
Thus we need to determine the maximum k such that there exists an assignment of values from 0..(some large) to positions such that each interval contains at least one occurrence of each number in 0..k-1.
This is reminiscent of covering constraints. For each number j (0 ≤ j < k), we need to ensure that each interval [l_i, r_i] contains at least one position where a[pos] = j. This is like each interval must contain at least one point from a set S_j (the positions assigned value j). So S_j must be a hitting set for the collection of intervals: every interval must intersect S_j. So we need to assign k disjoint (they could overlap? Actually positions can only have one value, so S_j for different j must be disjoint: a position can have only one value, so if we assign a position value j, it cannot also be value j' for j'≠j. So the sets S_j for j=0..k-1 must be pairwise disjoint (since each position gets one number). And they must each be a hitting set for all intervals. But we also can assign other values > k-1 arbitrarily, so those positions are not used for the required numbers.
Thus we need to partition (some) positions into k sets S_0,...,S_{k-1} (each position belongs to at most one of these sets, possibly none if assigned value >=k) such that each S_j is a hitting set: every interval [l_i, r_i] contains at least one element of S_j. This is equivalent to: For each interval, its intersection with S_j is non-empty.
Now we want to know maximum k for which such a partition exists given n positions and m intervals.
Observations: The condition that each S_j is a hitting set for all intervals means that the complement of S_j (positions not containing j) cannot contain an entire interval. So each interval must contain at least one position where a = j. So for a fixed j, S_j must be a set that "covers" all intervals.
This is similar to the concept of "interval hitting number" or "piercing number". The minimum size of a hitting set for a collection of intervals is something like: maximum number of disjoint intervals? Actually to hit all intervals, the minimal hitting set is equal to the maximum number of intervals that are pairwise non-overlapping? Wait: For intervals on a line, the minimum number of points needed to hit (intersect) all intervals can be found by greedy: sort intervals by right end, then pick the rightmost point to cover as many as possible. That number is the minimum cardinality of a hitting set. But we need to assign distinct hitting sets for each j, and they must be disjoint.
Thus we have k hitting sets that are pairwise disjoint. So we need to find if we can select k disjoint sets each of which hits all intervals. This is like we need to color the positions with colors 0..k-1 (or leave uncolored) such that each interval contains all colors.
Alternatively, we can think of assigning each position a value, and the constraint that each interval contains all numbers 0..k-1. So we need to ensure that within each interval, the set of values assigned includes these k numbers. Since each number must appear at least once per interval, and numbers are distinct values, it's akin to having a "rainbow" covering each interval with k distinct values, but not necessarily distinct across intervals; same position can serve multiple intervals for same number. But each interval must have its own occurrence of each number.
So the problem reduces to: Given intervals, what's the maximum number of distinct required values (0..k-1) such that we can assign each position a number (or maybe a set?) but each position can hold only one number, so each interval must contain positions with all k numbers. This is like: can we assign to each position a "color" from 0..k-1 (or -1 for others) so that each interval sees all k colors? However, we can also assign values >=k, which are like "unused" colors. But for the requirement, we need at least one occurrence of each of the k colors in each interval. So we can treat the values 0..k-1 as colors, and any other values as "neutral". So the question: What is the maximum k such that we can color some positions with colors 0..k-1 (each position at most one color) and possibly leave others uncolored (or colored with >=k) so that every given interval contains at least one position of each color?
We must ensure that the sets of positions assigned each color are disjoint. So each position can contribute to at most one required number.
We need to maximize k. This is reminiscent of the "interval coloring" or "interval covering with constraints" and might be related to the maximum number of intervals that are "nested" or something. Possibly the answer is the minimum length of any interval? But not exactly.
Let's consider simple cases:
If we have a single interval [1,n], then we need it to contain all numbers 0..k-1. Since we can assign up to n distinct numbers, the maximum k is n (if we assign each position a distinct number from 0..n-1, then the mex of the whole array would be at least n? Actually mex of whole array would be n if all 0..n-1 present; but we only need that the interval contains all 0..k-1. With n positions we can have up to n distinct required numbers, so k can be n. But is there any other constraint? We must assign each position a single value, so if we want k = n, we need to assign each of the n positions a distinct number from 0..n-1. That's possible. So max k = n.
If we have two intervals: [1, n-1] and [2, n]. For k=2, can we assign? We need each interval to have both 0 and 1. So interval1 must contain a 0 and a 1; interval2 must contain a 0 and a 1. Since the intervals overlap, we can place 0 at position 1 and 1 at position n, then interval1 contains 0 but not 1 (since interval1 is 1..n-1, missing position n). So fails. We need each interval to contain both. Let's try: Place 0 at pos1, 1 at pos2. Interval1 (1..n-1) contains both 0 and 1. Interval2 (2..n) contains 1 but not 0 (since pos1 not in interval2). Place 0 at pos1 and 1 at pos n. Interval2 contains 1 but not 0. Place 0 at pos2, 1 at pos1: Interval1 has both? Yes pos1 and pos2 in interval1; interval2 contains pos2 (0) but not 1? pos1 not in interval2. So fails. Place 0 at pos1 and also 0 at pos n? But we need both 0 and 1. Each position can hold only one value. So we could assign 0 to multiple positions, but we need at least one 1. The issue is that interval2 must contain a 0, but interval1 must also contain a 0. The positions that can serve as 0 for both intervals must lie in the intersection of the intervals? Actually for a given value j, each interval needs at least one occurrence of j. So the set S_j must intersect every interval. That's a hitting set. For multiple values, the sets must be disjoint. So the problem reduces to: can we find k disjoint hitting sets? That is like partitioning a subset of positions into k sets each of which hits all intervals. Since positions are limited, the maximum k is at most the minimum size of a hitting set? Actually each hitting set must have at least one element (unless there are no intervals, but m≥1?). But disjointness implies that total positions used across all sets is sum of |S_j|. Since S_j must intersect every interval, the minimal size of a hitting set (the minimum number of positions needed to hit all intervals) is some value h. But we don't require each S_j to be minimal; they can be larger. However, the sets must be disjoint, so the union of S_j can have at most n positions. So if we have k hitting sets, each of size at least h_min (the minimum possible size for a hitting set), then we need k * h_min ≤ n? But h_min is the smallest possible cardinality of a set that hits all intervals. But we are not forced to use minimal hitting sets; we can use larger sets, but they still must be disjoint. So the condition is: there exist k pairwise disjoint sets each of which is a hitting set for all intervals. This is equivalent to: the maximum number of pairwise disjoint hitting sets? Or the "hitting set packing" problem.
But maybe there's a simpler approach: The condition that each interval contains all numbers 0..k-1 implies that for any interval, its length (number of positions) must be at least k, because you need at least k distinct numbers (each number must appear at least once, but they can be repeated? Wait, numbers 0..k-1 are distinct. But a single position can only provide one number. So to have k distinct numbers within an interval, the interval must contain at least k positions (since you need at least k positions, each assigned a distinct number from 0..k-1). However, note that it's not necessary that each of the k numbers appears exactly once; a number could appear multiple times, but we still need at least one occurrence of each, so at least k positions are needed (since each position can contribute at most one of the required numbers). But we could have extra positions that are not required. So necessary condition: For each interval, length (r_i - l_i + 1) ≥ k. So k cannot exceed the minimum interval length. That's a simple bound.
Is that also sufficient? Not necessarily. Consider intervals [1,2] and [2,3] with n=3. Minimum length is 2, so k ≤ 2. Can we achieve k=2? Need each interval to contain both 0 and 1. With 3 positions: we can assign a = [0,1,?]. Interval [1,2] has 0 and 1 => mex at least 2. Interval [2,3] has 1 and ?. Need a 0 as well. So position 3 must be 0? But then interval [2,3] would have 1 and 0, ok. But position 3 is 0, but position 1 is also 0, that's fine (we can repeat numbers). Wait, we don't require distinctness across intervals; we just need each interval to contain at least one 0 and at least one 1. So with a = [0,1,0], interval [1,2]: 0,1 -> contains both. Interval [2,3]: 1,0 -> contains both. So k=2 works. So min length sufficed.
Consider intervals [1,2], [2,3], [1,3] with n=3. Min length = 2. Can we achieve k=2? Need each interval to contain 0 and 1. Let's try to assign values. Position 1,2,3. Interval [1,2] needs 0 and 1 => so among positions 1 and 2 we must have both 0 and 1. So one is 0, the other 1. Interval [2,3] needs 0 and 1 => positions 2 and 3 must have both 0 and 1. So position2 is either 0 or 1; then position3 must be the opposite. Now interval [1,3] needs both 0 and 1; but given above, we will have both because positions 1,2,3 contain both? Let's test: if pos1=0, pos2=1, pos3=0 then [1,3] has 0,1,0 -> contains both. Works. So k=2 works.
Consider intervals that are disjoint? For example n=4, intervals: [1,2] and [3,4]. Min length =2. Can we achieve k=2? Need each interval to contain both 0 and 1. Each interval length 2, so we need to assign both positions in each interval to 0 and 1 respectively, but each interval uses its own positions. Since intervals are disjoint, we can assign [0,1] to first, and [0,1] to second? But careful: we need disjoint sets S_0 and S_1? Wait, we are assigning values to positions; positions can have values 0 or 1. But S_0 is set of positions with value 0; S_1 is set with value 1. These sets are disjoint. For each interval, we need it to contain at least one position from S_0 and at least one from S_1. For interval [1,2], if we assign pos1=0, pos2=1, then it's fine: S_0 includes pos1, S_1 includes pos2, both within interval. For interval [3,4], we similarly need one 0 and one 1. But if we assign pos3=0, pos4=1, then S_0 includes pos3, S_1 includes pos4. But note S_0 now has two positions (pos1, pos3). That's fine; sets can have multiple elements. The disjointness is between S_0 and S_1, but they are still disjoint because no position has both 0 and 1. So it's allowed. So k=2 works. So min length condition seems sufficient in many examples.
But is it always sufficient? Let's try a more tricky case: intervals: [1,3], [2,4], [3,5] with n=5, min length = 3? Actually each length is 3. So k can be up to 3? Let's test k=3. Need each interval to contain 0,1,2. Each interval length 3, so to contain three distinct numbers, the three positions in each interval must be assigned exactly 0,1,2 (each once) because there are exactly three positions; if any repeats, then some number missing. So each interval must be a permutation of {0,1,2} on its positions. But intervals overlap. This becomes a constraint satisfaction: For interval [1,3], positions 1,2,3 must be a permutation of 0,1,2. For interval [2,4], positions 2,3,4 must be a permutation of 0,1,2. For interval [3,5], positions 3,4,5 must be a permutation of 0,1,2. Can we assign values to positions 1..5 satisfying this? Let's attempt. From [1,3], {a1,a2,a3} = {0,1,2}. From [2,4], {a2,a3,a4} = {0,1,2}. This implies that a4 must be one of {0,1,2} and also since {a2,a3,a4} is a permutation, a4 must be the element not in {a2,a3}? Actually {a2,a3} are two distinct numbers from {0,1,2}, so the third missing number is forced for a4. So a4 is determined as the complement of {a2,a3}. Similarly, from [3,5], {a3,a4,a5} = {0,1,2}, so a5 is the complement of {a3,a4}. So there might be a solution. Let's try to assign: Choose a1=0, a2=1, a3=2. Then [1,3] ok. Then from [2,4], a2=1, a3=2, so a4 must be 0. Then [3,5], a3=2, a4=0, so a5 must be 1. So assignment [0,1,2,0,1] works? Check [3,5]: positions 3,4,5: 2,0,1 => all 0,1,2 present. Yes. So k=3 works.
Now consider intervals that are "nested" but with small lengths? Example: n=4, intervals: [1,2], [1,3], [1,4]. Min length =2. Can we achieve k=2? Need each interval to contain 0 and 1. That seems easy: assign pos1=0, pos2=1, others arbitrary. Then [1,2] has both, [1,3] has both, [1,4] has both. Works.
Maybe the necessary and sufficient condition is simply that the minimum interval length is at least k. But wait, there is also a constraint about disjointness? Actually if we have an interval of length L, to contain k distinct numbers we need L ≥ k. So maximum possible k is at most min_i (r_i - l_i + 1). Let's denote L_min = min length among intervals. So k ≤ L_min. Could it be that we can always achieve k = L_min? Not obvious.
Consider intervals: [1,2] and [1,2] (duplicate). L_min=2, k=2? Need both intervals (same) to contain 0 and 1. That's fine: assign positions 1 and 2 with 0 and 1. Works.
Consider intervals: [1,2], [2,3], [1,3] with n=3, L_min=2, k=2 works as before.
But consider a pathological set of intervals where some positions are heavily constrained such that it's impossible to assign k distinct numbers within each interval while maintaining that each number appears in all intervals? Actually the requirement is per interval: each interval must contain all numbers 0..k-1. This does not require that the numbers appear only once per interval; they can appear multiple times. So the only per-interval requirement is that the interval's set of values includes the entire set {0,...,k-1}. So the condition is that for each interval, for each number j, there is at least one position in that interval assigned value j.
This is reminiscent of a classic problem: Given intervals, we want to assign colors to positions such that each interval contains all colors. The maximum number of colors possible is equal to the minimum length of an interval? Actually there is known result: The maximum number of colors that can be assigned to points on a line such that every given interval contains all colors is equal to the minimum size of a hitting set? Hmm.
Let's think in terms of "rainbow intervals": we want each interval to contain at least one of each color. Equivalent to saying that for each color, the set of positions with that color is a hitting set for the intervals. So we need k disjoint hitting sets. The maximum k such that there exist k pairwise disjoint hitting sets is something like the minimum "transversal number" of the family? This is akin to the concept of "matching number" in hypergraphs? Actually the family of intervals is a hypergraph (edges are intervals, vertices are positions). A hitting set is a vertex cover (in hypergraph terminology, a transversal). We need k pairwise disjoint transversals. This is related to the "packing of transversals" or "colorful hitting sets". There is a known theorem: In an interval hypergraph, the maximum number of pairwise disjoint transversals equals the minimum size of an interval? Not exactly.
Let's try to prove if k = min length is always achievable. Suppose the minimum interval length is L. Can we always construct an array such that every interval contains all numbers 0..L-1? That would be the maximum possible k = L (since k can't exceed L). But is it always possible? Let's attempt to design a construction algorithm.
We need to assign to each position a value from 0..L-1 (or maybe higher) such that for each interval, all numbers appear. Since L is the minimum length, there exists at least one interval of length L; call it I0. For that interval, to contain all numbers 0..L-1, it must have exactly one occurrence of each number (since it has exactly L positions, and it needs L distinct numbers). So within that minimal-length interval, the values must be a permutation of 0..L-1. For other longer intervals, they also need to contain all numbers, but they may have extra positions that could be assigned arbitrarily (maybe repeating numbers or numbers >= L). So the core constraint is that for every interval, it must include at least one of each number from 0..L-1.
Now, can we always assign such that each number j appears at least once in every interval? This is a strong condition: the set of positions labeled j must intersect every interval. So for each j, the positions where we place j must form a hitting set for all intervals. Moreover, these hitting sets must be disjoint (since each position gets one number). The additional twist: We have exactly L numbers to place (0..L-1). For the minimal interval of length L, it forces that within that interval, the hitting sets S_j are exactly one position each, and they partition the interval. So in that interval, each S_j contains exactly one distinct position. So that's a natural partition.
Now consider any other interval J. It must contain all numbers, so for each j, J ∩ S_j must be non-empty. So essentially, each S_j must be a hitting set. The question reduces to: Given a collection of intervals on a line of points 1..n, with min length L, can we partition the set of positions into L hitting sets? I.e., can we color the positions with L colors such that each interval contains all colors? This is similar to the concept of "interval graph coloring" but reversed: Usually we color intervals so that overlapping intervals get different colors. Here we color points so that each interval sees all colors.
This is reminiscent of the "Gallai's theorem" or "perfect graphs"? Let's consider the complement: For each color j, the positions not having color j must not contain any interval entirely. That is, the complement of S_j must be an interval-free set? Actually, if an interval is entirely in complement of S_j, then that interval lacks color j. So complement of S_j must be a set that does not fully contain any given interval. This is equivalent to saying that the set of positions not colored j is not a superset of any interval. But maybe we can characterize by the condition that the intervals have "covering number" at least L? Something like the "interval's hitting number" being at least L? Actually, to have L disjoint hitting sets, the minimum size of a hitting set must be at least L? Let's think: If we have L disjoint hitting sets, each hitting set must contain at least one point from each interval (by definition). The union of these L hitting sets can have at most n points. But also, consider the minimal interval of length L. Since it must contain L distinct points each from a different hitting set (because it needs all colors), the hitting sets are forced to have exactly one point inside that interval, and those points are all distinct. So the hitting sets each have at least one point in that interval. But does that impose any global constraints?
Maybe there is known result: In an interval hypergraph, the maximum number of pairwise disjoint transversals is equal to the minimum size of an edge (interval). I recall something about "system of distinct representatives" for intervals? Actually, think of the dual: For each interval, we need to select a representative point for each color? Not exactly.
Let's try to construct a counterexample where min length is L but it's impossible to have L colors such that each interval contains all colors. Need to design intervals where some position is forced to carry two colors because of overlapping constraints.
Suppose we have n=3, intervals: [1,2], [2,3]. min length = 2, L=2. Can we do 2 colors? Yes, as we did.
Suppose n=4, intervals: [1,2], [2,3], [3,4]. min length = 2 (since each length 2). Can we do 2 colors? Need each interval to contain both 0 and 1. Let's try: assign colors to positions 1,2,3,4. Interval [1,2]: must have both colors. So among pos1,pos2 we need one 0 and one 1. Interval [2,3]: pos2,pos3 must have both colors. Interval [3,4]: pos3,pos4 must have both colors. Let's see if we can assign. Suppose pos1=0, pos2=1 => [1,2] ok. Then [2,3] needs both colors, so pos3 must be 0 (since pos2 is 1). Then [3,4] needs both colors: pos3=0, pos4 must be 1. So assignment: 0,1,0,1 works. So k=2 works.
Consider n=5, intervals: [1,3], [2,4], [3,5]. min length=3, L=3. Can we do 3 colors? We already did an assignment [0,1,2,0,1]? Wait that had 0,1,2 in each interval? Check [2,4]: positions 2=1,3=2,4=0 -> contains all 0,1,2 yes. [3,5]: 2,0,1 yes. So works.
Consider intervals: [1,3], [3,5], [1,5]. min length = 3? Actually [1,3] length3, [3,5] length3, [1,5] length5. L=3. Can we do 3 colors? Try assignment: For [1,3] need all three colors, assign positions 1,2,3 as 0,1,2. For [3,5] need all three, so positions 3,4,5 must be a permutation. Position 3 already 2, so we need 0 and 1 for 4 and 5. So assign 4=0,5=1. Then [1,5] contains all 0,1,2? Yes positions 1,2,3,4,5: 0,1,2,0,1 -> all colors present. Works.
Now consider something more tricky: intervals that are not contiguous? Actually all intervals are contiguous subarrays by definition. So it's a set of intervals on a line.
Maybe the condition min length is always sufficient. But before concluding, let's test a more complex scenario with overlapping intervals that force a contradiction for k = min length. Suppose we have three intervals that together cover all points, but each of length exactly L, and their overlapping pattern may require a point to be two colors. For L=2, consider intervals: [1,2], [2,3], [1,3] with n=3 we already did and it works. For L=2, consider intervals: [1,2], [2,3], [3,4], [1,4] with n=4. min length=2? Actually [1,2] len2, [2,3] len2, [3,4] len2, [1,4] len4. L=2. Can we assign? Let's try: need each interval to have both 0 and 1. So [1,2]: assign 0,1 (positions 1,2). Suppose pos1=0, pos2=1. [2,3]: pos2=1, so pos3 must be 0. [3,4]: pos3=0, so pos4 must be 1. Then [1,4] contains 0,1,0,1 -> both present. Works.
What about L=2 with intervals: [1,2], [2,3], [1,3], [2,4] with n=4. min len=2? [1,2] len2, [2,3] len2, [1,3] len3, [2,4] len3. So L=2. Try assign: [1,2] need both: pos1=0, pos2=1. [2,3] need both: pos2=1, so pos3=0. [1,3] contains both. [2,4] needs both: currently pos2=1, pos3=0, but pos4? [2,4] includes pos2,3,4. Already have both 0 and 1 from pos2 and pos3, so pos4 can be arbitrary, say 0. Works.
Seems always possible? Let's try L=3 with more complex overlapping. Suppose intervals: I1=[1,3], I2=[3,5], I3=[5,7], I4=[1,7] with n=7. min len=3. Need colors 0,1,2. I1 forces {a1,a2,a3} = permutation. I2 forces {a3,a4,a5} = permutation. I3 forces {a5,a6,a7} = permutation. I4 forces entire interval must contain all colors. Let's try to assign: Choose a1=0,a2=1,a3=2 for I1. Then I2: a3=2, so a4,a5 must be {0,1}. Suppose a4=0, a5=1. Then I3: a5=1, so a6,a7 must be {0,2}. Choose a6=0, a7=2. Now I4 [1,7] contains all? We have 0,1,2,0,1,0,2 -> all colors present. Works.
Try a more irregular set: intervals: [1,4] length4, [2,5] length4, [3,6] length4, [4,7] length4, with n=7, min len=4? Actually each length 4. So L=4. Need colors 0,1,2,3. Intervals overlap. Can we assign? Might be possible. But could there be a contradiction? This resembles a sliding window of length 4 over 7 positions. The condition that each window of length 4 contains all 4 colors essentially means the sequence of length 7 is such that every contiguous block of length 4 is a permutation of 0..3. This is a known problem: a sequence of length n over alphabet of size k such that every window of length k is a permutation. For n=7, k=4, is it possible? This is like a "de Bruijn sequence" or "rolling window" property. I recall that such sequences exist if and only if n >= k and maybe some parity? Actually, consider a sequence of length n where each window of length k contains all k symbols. This is equivalent to the sequence being a "k-ary de Bruijn sequence" of order 1? Not exactly. For k=4, a sequence where every consecutive 4 are a permutation of {0,1,2,3}. This is quite restrictive. Let's try to construct for n=7: positions 1..7. Window 1-4 must be a permutation. Window 2-5 must be a permutation, etc. This implies that the multiset of positions differences... Actually we can think: Since window 1-4 is a permutation, the set {a1,a2,a3,a4} = {0,1,2,3}. Similarly, {a2,a3,a4,a5} = {0,1,2,3}. Therefore, a5 must equal a1, because replacing a1 with a5 while keeping the set same implies that a5 must be the element that was removed when sliding. More precisely, if two sets of size 4 are equal and differ by one element (a1 vs a5), then a5 must equal a1. Wait, check: If S1 = {a1,a2,a3,a4} = full set, and S2 = {a2,a3,a4,a5} = full set, then since S1 and S2 both equal the full set, the element that is in S1 but not in S2 is a1, and the element in S2 but not in S1 is a5. For the sets to be equal, we must have a1 = a5. Because if a1 ≠ a5, then S1 would contain a1 not in S2, and S2 would contain a5 not in S1, so the sets would differ. But since both are the full set, they must both contain all elements, so a1 must equal a5. Actually careful: S1 = full set = {0,1,2,3}. S2 = full set = {0,1,2,3}. S1 \ S2 = {a1} \ {a2,a3,a4,a5} = {a1} if a1 not in {a2,a3,a4,a5}. But since S2 already contains all four numbers, it must contain a1 as well because a1 is one of 0,1,2,3. So a1 must be among a2,a3,a4,a5. But that doesn't force a5 = a1; a1 could equal a2, for example. However, S1 and S2 both being the full set means that each of the numbers 0,1,2,3 appears at least once in each window. It does not require that the multisets are sets; duplicates could appear but the set of distinct values is full. So it's possible that a1 = a2, and then a5 could be something else, as long as the set of {a2,a3,a4,a5} still covers all four numbers. For example, let a1=0, a2=0, a3=1, a4=2, a5=3. Then window 1-4: {0,0,1,2} missing 3, fails. So to cover all four, we need each window to contain each number at least once. So for window1-4, we need a set containing all four numbers. Since there are four positions, that means each of the four numbers appears exactly once (no duplicates) because if any number appears twice, some other number must be missing. Indeed, if we have four positions and need to cover four distinct numbers, it must be a permutation (each exactly once). So indeed each window of length 4 must have all four distinct numbers, i.e., a permutation. So duplicates are impossible within a window because then there would be only 3 distinct numbers for 4 slots. Therefore, each window of length 4 is a permutation. So as argued, if window1-4 is a permutation, then a1,a2,a3,a4 are all distinct. For window2-5 to also be a permutation, a2,a3,a4,a5 must be all distinct. Since a2,a3,a4 are distinct, a5 must be the unique element not among {a2,a3,a4} but also must be the one that is a1? Actually, the set {a2,a3,a4} is three distinct numbers. The missing number from the full set is the one not in {a2,a3,a4}. That missing number must be a5. Meanwhile, a1 is the number not in {a2,a3,a4} as well because a1,a2,a3,a4 are all distinct and cover all four. So a1 is exactly the number not in {a2,a3,a4}. Therefore a5 must equal a1. So indeed, sliding window of length k forces a5 = a1. Then by induction, a6 = a2, a7 = a3, etc. So the sequence is periodic with period k. For n=7, k=4, we would have a1,a2,a3,a4,a1,a2,a3. Check windows: 2-5: a2,a3,a4,a1 -> permutation; 3-6: a3,a4,a1,a2 -> permutation; 4-7: a4,a1,a2,a3 -> permutation. Works. So it's possible. So the condition of all windows of length k containing all k numbers leads to a periodic assignment with period k. That is possible as long as n >= k. So indeed for the specific set of intervals that are all windows of length k (and possibly longer intervals?), the construction works.
Now consider intervals that are not all windows of length L, but some are longer. Longer intervals are easier to satisfy because they have more positions, so the constraints are less restrictive. So the hardest constraints are the intervals of minimal length L. So perhaps if we can satisfy all minimal-length intervals (maybe many of them), we can satisfy all.
But is it always possible to color with L colors such that every interval of length exactly L (the minimal length) contains all L colors? Not all intervals might be length L; we only have intervals of length at least L. But we need all intervals, including longer ones, to contain all L colors. However, if we satisfy that every interval of length L contains all L colors, does it automatically guarantee that longer intervals also contain all L colors? Not necessarily. Consider intervals: [1,2] length2, and [1,3] length3. If we assign colors to positions 1,2,3 such that [1,2] contains both colors 0 and 1, but [1,3] might be missing one of them if the third position is something else? For L=2, we need both 0 and 1 in [1,3]. If [1,2] already contains both, then [1,3] certainly contains both because it includes [1,2]. So if a longer interval contains a minimal-length interval that already has all colors, then it automatically contains all colors. But a longer interval might not contain any minimal-length interval as a subset. However, if we have a set of intervals, the condition that all intervals (including longer) contain all colors might impose extra constraints beyond the minimal-length ones. For example, suppose we have intervals: [1,2] (len2) and [3,4] (len2), and also [2,3] (len2) maybe? Actually consider intervals: [1,2] (len2) and [2,4] (len3). L_min = 2. If we satisfy [1,2] contains both 0 and 1, then [2,4] includes position2 but maybe not position1. So [2,4] needs both 0 and 1. Could we assign such that [1,2] has both and [2,4] also has both? Possibly. Let's try: assign pos1=0, pos2=1. Then [2,4] currently has pos2=1, need a 0 in positions 2,3,4. So pos3 or pos4 must be 0. That's possible: pos3=0, pos4 arbitrary. Works. So not a problem.
But could there be a scenario where satisfying all minimal-length intervals forces a coloring that makes some longer interval miss a color? Let's attempt to construct a counterexample. Suppose we have intervals: [1,3] (len3) and [2,4] (len3) and also [1,4] (len4). L_min=3. If we assign to satisfy the two len3 intervals, we saw earlier it forces a pattern like we did: [1,3] and [2,4] require? Let's derive: [1,3] must have colors 0,1,2 distinct. [2,4] must have colors 0,1,2 distinct. This yields constraints: positions1,2,3 are a permutation; positions2,3,4 are a permutation. As before, that forces a4 = a1. So we could have a1=0,a2=1,a3=2,a4=0. Then [1,4] contains 0,1,2,0 -> all colors present. So fine.
What about intervals [1,3] and [3,5] and [1,5]? We already did. Works.
Maybe the condition is that the family of intervals is such that the "intersection graph" or something ensures that if all minimal-length intervals are covered, all larger ones are automatically covered because they contain a minimal-length interval? But that's not necessarily true: consider an interval of length L+1 that does not contain any interval of length L from the given set. However, it might still be forced to contain all colors because of overlapping constraints from other intervals? Actually, if there is no interval of length L inside it, we might not have a direct constraint that forces all colors to appear within it. But we still need it to contain all colors. However, we can choose the assignment to ensure it does. The difficulty is whether the constraints from the minimal intervals collectively force a pattern that prevents a longer interval from containing all colors. Let's try to design a counterexample with L=2. Suppose intervals: [1,2] (len2) and [3,4] (len2) and also [2,3] (len2) maybe? Actually we want a longer interval that doesn't contain a len2 interval from the set. For instance, intervals: [1,2] (len2), [3,4] (len2), and [2,4] (len3). L_min=2. We need to satisfy [1,2] contains both colors, [3,4] contains both, and [2,4] contains both. Can we assign? [1,2] requires both 0,1 on positions1-2. [3,4] requires both on positions3-4. [2,4] requires both on positions2-4. So we need at least one 0 and one 1 among positions2,3,4. Suppose we set pos1=0, pos2=1. For [3,4], set pos3=0, pos4=1. Then [2,4] has pos2=1, pos3=0, pos4=1 -> contains both (0 and 1). Works. So fine.
What about intervals: [1,2], [3,4], [1,4] (len4). L_min=2. [1,2] need both; [3,4] need both; [1,4] need both. Assign pos1=0,pos2=1; pos3=0,pos4=1 gives [1,4] contains both. Works.
Maybe always possible? Let's think more abstractly: We need to assign L colors to positions (some positions may get colors outside 0..L-1, but those don't help). Actually we can assign values > L-1 to some positions, but they don't contribute to covering the required colors. So the key is to ensure that for each interval, for each color j, there is at least one position within the interval that is assigned j. This is equivalent to: for each color j, the set of positions with color j is a hitting set for all intervals. So we need to partition a subset of positions into L hitting sets.
Now, is it always possible to have L hitting sets when the minimum interval length is L? This resembles a classic result: In an interval hypergraph, the transversal number τ (minimum hitting set size) is equal to the maximum number of pairwise disjoint intervals? Actually, for intervals, the minimum number of points needed to hit all intervals is equal to the maximum number of pairwise disjoint intervals (by the Helly-type property or the "interval stabbing" problem). That's a standard result: The minimum size of a hitting set (also called piercing number) for a set of intervals is equal to the maximum size of a set of pairwise disjoint intervals (this is the dual of the interval scheduling problem, and it's true because intervals are a "interval graph" and the complement of a maximum independent set, etc.). More precisely, given a collection of intervals on a line, the minimum number of points needed to intersect every interval is equal to the maximum number of intervals that are pairwise non-overlapping (i.e., the size of a maximum set of intervals with no common point). This is a classic theorem (often referred to as the "interval covering" or "interval stabbing" problem, and it's a consequence of the fact that intervals are a "perfect" hypergraph for this property). Actually, it's the dual of the "maximum independent set" in interval graphs. Yes: The minimum number of points to stab all intervals equals the maximum number of pairwise disjoint intervals. This is a known result: "The interval stabbing number equals the size of a maximum set of pairwise disjoint intervals." So let's denote p = maximum number of pairwise disjoint intervals in the given family.
Now, what does this have to do with our problem? If we want k disjoint hitting sets, each hitting set must contain at least one point from each interval. In particular, consider a maximum set of pairwise disjoint intervals. Each hitting set must contain at least one point in each of those disjoint intervals because they are intervals and hitting all intervals. Since the intervals are disjoint, a single hitting set must have at least one point in each of these p intervals. Therefore, any hitting set has size at least p. That's because you need at least one distinct point per disjoint interval (they cannot share a point since intervals are disjoint). So the minimum possible size of a hitting set is at least p. And indeed, by the theorem, the minimum hitting set size equals p (since you can pick one point from each of the p intervals, but you need to ensure that point also hits any intervals that overlap with those? Wait, the standard result: The minimum number of points needed to intersect all intervals equals the maximum number of pairwise disjoint intervals. And a construction: sort intervals by right endpoint, greedy pick the rightmost point that covers a maximal set, etc. But yes, the minimum size is exactly p.
Now, we need L disjoint hitting sets. The total number of positions used across all hitting sets is at least L * p (since each hitting set must have at least p points, but they could potentially share points? No, they must be disjoint. So total distinct positions used is at least L * p. Since we have only n positions, a necessary condition is n ≥ L * p. But is that also sufficient? Possibly, if we can partition the positions into L hitting sets, each of size exactly p (or at least p). However, our k is L (the min length). So for k = L to be possible, we need n ≥ L * p. But is n ≥ L * p always true given that min length is L? Not necessarily. Consider intervals: Suppose we have many disjoint intervals each of length L. Then p would be the number of such disjoint intervals. If we have p disjoint intervals each of length L, then total n might be less than L * p? Actually, if intervals are disjoint, they occupy distinct segments of the line. Their total length sum is at least L * p (since each has length at least L). But they may be spaced apart, and n is the total number of positions. The positions covered by these disjoint intervals could be overlapping? Disjoint intervals are non-overlapping, so they occupy distinct positions. The total number of positions that are in the union of these p intervals is at least L * p (if each has length exactly L, union length = L * p). Since the entire line has n positions, we must have n ≥ L * p. Actually, if intervals are disjoint, their total length is sum of lengths, which is at least L * p. But they could be placed such that the union spans exactly L * p positions if they don't overlap and each exactly L. Since the whole array is length n, we must have n ≥ total length of union, so n ≥ L * p. So this condition seems to hold automatically because intervals are subintervals of [1,n] and are disjoint, so sum of lengths ≤ n. Wait careful: If intervals are disjoint, they occupy distinct positions, so the total number of positions covered by them is sum of their lengths. Since they are subsets of [1,n], the sum of lengths ≤ n. So we have sum(length_i) ≤ n. Each length_i ≥ L, so p * L ≤ sum(length_i) ≤ n. Thus n ≥ p * L. So the necessary condition n ≥ L * p is automatically satisfied! Because the maximum number of pairwise disjoint intervals p, each of length at least L (since min length is L), so total length of these disjoint intervals is at least p*L, which cannot exceed n (since they are disjoint and within [1,n]). So n ≥ p * L holds automatically. Good.
But is this condition sufficient to guarantee that we can partition the positions into L hitting sets? Not obviously. However, there is a known result about "interval hypergraph has the Kőnig property" or something like that. The hypergraph of intervals is a "balanced hypergraph"? Actually, intervals form a "totally unimodular" matrix, etc. Might be related to the fact that the transversal number equals the matching number, and the hypergraph is "Mengerian" etc. The existence of L disjoint transversals might be equivalent to the condition that the covering number (transversal number) τ = p, and the hypergraph is "τ-wise packable"? There is a concept of "packing of transversals" known as "transversal number" and "matching number". But we need disjoint transversals, not just any sets. However, if we have τ = p, then the hypergraph is "balanced" maybe ensures that we can partition the vertex set into p transversals? Hmm.
Alternatively, maybe we can directly construct an assignment achieving k = L_min using a greedy algorithm: Since the minimum length is L, we can assign numbers 0..L-1 to positions in a way that each interval contains all numbers. Idea: For each position, we can assign a value based on its index modulo L? For example, assign a[i] = i mod L. Does that guarantee that any interval of length at least L contains all residues modulo L? If we take any interval of length L, it will contain a contiguous block of L consecutive indices. If we assign i mod L, a block of L consecutive integers will contain each residue exactly once (if we consider modulo L starting from 0). Indeed, if we number positions from 0 to n-1, a[i]=i mod L. Then any interval of length L (consecutive) will contain indices covering all residues mod L. Because the residues of L consecutive integers are a complete set modulo L. So this ensures that every interval of length exactly L contains all numbers 0..L-1. But what about intervals longer than L? They will also contain all residues because they contain some length-L subinterval, which already has all residues. So any interval of length ≥ L will contain a contiguous subsegment of length L (not necessarily one of the given intervals, but any interval of length ≥ L contains some length-L consecutive block). However, is it guaranteed that the given interval of length > L will contain a contiguous block of L positions that is exactly the positions i..i+L-1 for some i? Yes, any interval [l,r] with r-l+1 ≥ L contains many subintervals of length L, e.g., [l, l+L-1] is within [l,r] if l+L-1 ≤ r. Since length ≥ L, that's true. So that subinterval of length L will have all residues. Therefore the whole interval will contain all residues as well. So the assignment a[i] = i mod L (or i%L) works to ensure that every interval of length at least L contains all residues 0..L-1. But wait: Our intervals are given, and we only care about those intervals. So if each given interval has length at least L (since L is the minimum length), then indeed each interval contains a subinterval of length L (the first L positions of it, for instance). And if we assign a[i]=i mod L, then that subinterval will contain all residues, because any L consecutive integers modulo L yield a complete set. Therefore each given interval will contain all numbers 0..L-1. Great! This gives a simple constructive proof that k = min_length works, and we can achieve it by setting a[i] = i mod L (or something like that). However, careful: The assignment a[i] = i mod L (with numbers 0..L-1) will use only values from 0 to L-1. But we need to ensure that the mex of each interval is at least L. Since each interval contains all numbers 0..L-1, its mex is at least L (could be larger if L is also present, but we are not using L anywhere if we only assign 0..L-1, so mex will be exactly L because L is not present (unless we assign L elsewhere, but we are only using 0..L-1). So the mex of each interval will be exactly L. Therefore the minimal mex among intervals will be L (if all intervals have length at least L). But we need to maximize the minimum mex. So the maximum possible minimum mex is at least L. Could it be larger than L? Since L is the minimum length, can we have k = L+1? That would require each interval to contain numbers 0..L. But intervals of length L cannot contain L+1 distinct numbers because they only have L positions. So k cannot exceed the minimum interval length. So the maximum possible minimum mex is exactly the minimum length among the given intervals. Therefore the answer is simply: maximum possible min mex = min_i (r_i - l_i + 1). And we can construct an array by a[i] = i mod (min_length) (or using 0-based indexing). But we need to output numbers between 0 and 1e9; that's fine.
But wait: Is there any catch? The above reasoning assumes that the intervals are contiguous and we can assign a[i] = i mod L (with L = min length). But what if L=0? Not possible (length at least 1). So L ≥ 1.
Let's test with the sample inputs:
Sample 1: n=5, m=3 intervals: [1,3] length3, [2,5] length4, [4,5] length2. Minimum length = min(3,4,2)=2. So answer should be 2. They output 2. And their array: 1