The problem can be reduced to iterating over v from n to 1, each time we delete a lexicographically smallest set of intervals which can be covered with cnt_v elements.
To check this for a certain set of intervals S, a simple greedy will be taking the minimum r of all uncovered intervals, and placing a element with value v at this position. This runs in O(|S|\log|S|) by sorting and will calculate the minimum number of elements needed to cover all intervals in S, we will call this value f(S).
We can run this greedy in both directions, from left to right, each time taking the rightmost one, and vice versa. We name the i-th element on these paths sr_i and sl_i, respectively.
To calculate the maximal prefix that can be covered for some v, we can run brute force check on lengths 2^0,2^1,2^2,\cdots, until we locate that the maximal prefix is in range [2^l,2^{l+1}). Then we can run binary search in the range, our complexity is bounded by O(len\log^2 len), where len is the length of the maximal prefix. This can be further optimized to O(len\log len) if we sort the first 2^{l+1} intervals preemptively, which means that we don't have to sort during the binary search.
Suppose for some v, we have calculated the maximal prefix S which can be covered, then it follows that f(S)=cnt_v (or the trivial case that S contains all intervals). To check whether a new interval can be added to S, we only need to check whether f(S) increases.
Observation 1: f(S) increases iff the new interval does not intersect with any [sl_i,sr_i].
This can be inferred from the definition of sr and sl, with some minor reasoning.
Now, with some extra effort, we will have a solution that runs in O(nm). When n becomes larger, we will need to find an efficient way to maintain sr and sl.
Observation 2: Each interval will only enter and exit sr at most once. The same holds for sl.
This is due to each sr_i being decreasing over time, and that each interval can only enter sr at one position, or else we can deduce that f(S) has increased.
Each time we add an interval to S, we can run binary search to find its place among sr, and try to update one by one. We will need to maintain the "next" of each value with some data structures (I used a segment tree). We will perform linear updates to sr and sl in total.
Using the above, if we do an O(m) check for all v, we can pass a_i\le 5. To optimize, the only task which remains is to locate the next interval that can be added to S efficiently.
If we only need to check for a single [sl_i,sr_i], that is, find the first interval which intersects with [sl_i,sr_i]. This can be solved by keeping two sets on a segment tree over positions 1,2,\cdots,n, which yields a complexity of O(\log^2 n) insertion/deletion, and O(\log n) query.
A simple idea will be keeping a data structure containing the first interval for every [sl_i,sr_i], each time we take the minimum, check if it is not already in S, and add it to S. However, this is incorrect, as an interval can be the first interval for lots of [sl_i,sr_i], and we have no way of updating them all efficiently.
Observation 3: If we take the sr and sl of S, all intervals that contains at least one [sl_i,sr_i] can always be added to S. Adding them will also have no influence on sr and sl.
Therefore, after we calculate the maximal prefix, we take the current sr and sl and add all intervals satisfying the condition above. After this process, any interval will intersect with at most two [sl_i,sr_i]'s in the future, so we will have linear updates to this set in total.
Notice that all our insertions to the sets on segment tree structure is increasing, and only at the beginning. We only need to perform queries and deletions in the main algorithm. Therefore, we can replace sets with linked-lists to optimize it to O(\log n).