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.
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 . Let 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:
- interval wraps around midnight - we split this interval into one that ends at midnight and the other that starts then.
- 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 . In this task, was set to .
Sidenote: There is a faster approach with linear time complexity. Prefix sums are used to keep track of the popularity of each second.
Comments