Editorial for COI '10 #2 Telka


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

One possible solution to this problem consists of dividing an array which represents the popularity of each second into several blocks. The number of seconds in a day is 86\,400. Let A be the selected size of the blocks. Intervals which represent periods when someone has watched TV should be processed one by one and the popularity array should be updated according to the interval data. When we are processing one interval two cases arise:

  1. interval wraps around midnight - we split this interval into one that ends at midnight and the other that starts then.
  2. interval does not wrap around midnight - brute force update is optimized in the following manner: blocks that are fully contained in the processed interval are updated in one operation. A special case are the 'edge' blocks - those blocks are not fully contained, but overlap with the given interval. We solve this issue by updating each of the overlapping elements in the block one by one.

Queries are made in the same fashion as updates described above.

The speed of our algorithm depends on the parameter A. In this task, A was set to 100.

Sidenote: There is a faster approach with linear time complexity. Prefix sums are used to keep track of the popularity of each second.


Comments

There are no comments at the moment.