Editorial for COCI '07 Contest 4 #5 Poklon
Submitting an official solution before solving the problem yourself is a bannable offence.
First sort the intervals in descending order by their lower bound, breaking ties in increasing order of their upper bounds. Let  be the length of the longest sequence starting with interval 
.
We can see that , for each interval 
 completely contained in interval 
. If 
 does not contain any other intervals, then 
.
Because of how we sorted the intervals, interval  cannot contain interval 
 if 
. A direct implementation of the above idea has time complexity 
 and does not get all points.
To efficiently calculate , we introduce a new array of integers 
 containing 
 elements and initialized to zero. The value 
 tells us the length of the longest sequence starting with an interval whose upper bound is exactly 
.
Now the relation  can be expressed as 
. In other words, we find the length of the longest sequence whose upper bound is inside 
. Again, the lower bound of that interval will also be within 
, because the intervals are processed in descending order of lower bounds.
Finally, to efficiently find the largest element in the subsequence of  with indices 
, we can use an interval tree or Fenwick tree.
Comments