Блог пользователя -grace-

Автор -grace-, история, 6 лет назад, По-английски

Hello everyone, I am having a problem in solving this problem from CSES problem set. I could not find any explanation for this problem.

I solved the "Movie Festival" problem which is easier version of this one by simply sorting the intervals by end time and then iterating over all the intervals and comparing begin time of each interval with previous interval's end. But that approach will give O(n^2) in this problem.

I was thinking of applyng Mo's to use the same approach but could not proceed.

Any help would be appreciated. Thanks and keep coding!!

  • Проголосовать: нравится
  • +5
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +10 Проголосовать: не нравится
Spoiler
»
6 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Did you considered using Binary search for each query?

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I hope you understand the greedy for maximum events. You want to choose the one that ends as early as possible. Now, what this means, is that if you choose some event, the next events are fixed. You could alternatively, think of it as, you want any next $$$k$$$ events to end as early as possible too.

Let $$$dp_{i,j}$$$, be the last event with the earliest possible ending time, assuming you visit $$$2^j$$$ events after event $$$i$$$.

To compute $$$dp_{i,j}$$$, we just need to find where $$$dp_{i,j-1}$$$ ends, and find the event that ends the earliest after it, and use $$$dp_{e,j-1}$$$, to get the next $$$2^{j-1}$$$

Now consider, for each query, you need to find the event that ends first in that range. Now you want to start binary lifting. Check if $$$2^k$$$ events are fine. If they are, then you can go that event, and binary lift from there with $$$2^{k-1}$$$, and so on. $$$k$$$ is $$$O(\log n)$$$.