| 
 | 
вернуться в форумIf you want a solution The problem is quite evil.  The problem is actually very easy.  It is hard to understand that the problem is that easy. Possible solution. First ,  eliminate all such segments I=[l;r],  that l>M or r<0, because of such I's can't appear in the answer. Second,  build your dp[]. Your dp[x]  contains rightmost right end of all such segments [l;r],  that l<=x. Finally,  just start from segment whose right end is dp[0],  jump to a  segment whose right end is dp[dp[0]] and so on.   How to build dp[x]? I have used an additional array right_end[y].  Where right_end[y] is the right end of a segment with maximal length from all segments whose left end is y. dp[x] =max(dp[x-1], right_end[x]).  |  
  | 
|