Music Notes
InterviewTime limit1sMemory limit128 MB
Given note durations that divide a timeline into consecutive intervals, answer queries asking which 1-based note covers a given time. Use prefix sums and binary search.
- Level
Medium4 of 10
- Topics
- Prefix sum, Binary search, Array, Sorting
- Solved
- No attempts yet
Problem
A farmer is teaching his cows to play a song. The song has notes (), and the -th note lasts beats (), so the whole song is at most beats long.
The cows start playing at time . They play note from time up to (but not including) time , note from time up to time , and so on. In general, note is played during the half-open interval .
To keep the cows attentive, the farmer asks questions () of the form: “During the interval from time up to (but not including) time , which note should you be playing?” Every query time () falls within the song, so exactly one note is being played.
For example, consider a song with three notes of durations , , and beats. The timeline looks like this:
Beat: 0 1 2 3 4 5 6 ...
|----|----|----|----|----|----|--- ...
1111111111 : :
22222: :
333333333333333:
Here note covers , note covers , and note covers .
Input
- Line 1: two space-separated integers and .
- Lines : line contains a single integer .
- Lines : line contains a single integer , the -th query time.
Output
- Lines : for each query, print a single integer — the -based index of the note that is being played during that query interval.