Music Notes

Interview

Time limit1sMemory limit128 MB

Summary
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 NN notes (1≤N≤50,0001 \le N \le 50{,}000), and the ii-th note lasts BiB_i beats (1≤Bi≤10,0001 \le B_i \le 10{,}000), so the whole song is at most 500,000,000500{,}000{,}000 beats long.

The cows start playing at time 00. They play note 11 from time 00 up to (but not including) time B1B_1, note 22 from time B1B_1 up to time B1+B2B_1 + B_2, and so on. In general, note ii is played during the half-open interval [ B1+⋯+Bi−1, B1+⋯+Bi )[\,B_1 + \cdots + B_{i-1},\ B_1 + \cdots + B_i\,).

To keep the cows attentive, the farmer asks QQ questions (1≤Q≤50,0001 \le Q \le 50{,}000) of the form: “During the interval from time TT up to (but not including) time T+1T+1, which note should you be playing?” Every query time TT (0≤T0 \le T) falls within the song, so exactly one note is being played.

For example, consider a song with three notes of durations 22, 11, and 33 beats. The timeline looks like this:

Beat:   0    1    2    3    4    5    6    ...
        |----|----|----|----|----|----|--- ...
        1111111111     :              :
                  22222:              :
                       333333333333333:

Here note 11 covers [0,2)[0, 2), note 22 covers [2,3)[2, 3), and note 33 covers [3,6)[3, 6).

Input

  • Line 1: two space-separated integers NN and QQ.
  • Lines 2…N+12 \ldots N+1: line i+1i+1 contains a single integer BiB_i.
  • Lines N+2…N+Q+1N+2 \ldots N+Q+1: line N+i+1N+i+1 contains a single integer TiT_i, the ii-th query time.

Output

  • Lines 1…Q1 \ldots Q: for each query, print a single integer — the 11-based index of the note that is being played during that query interval.

Examples1

  1. Example 1

    Input
    3 5
    2
    1
    3
    2
    3
    4
    0
    1
    
    Expected output
    2
    3
    3
    1
    1