Find the Playing Note

Interview

Time limit1sMemory limit128 MB

Summary
Given note durations that partition a timeline, answer queries asking which note covers a given beat by locating the prefix sum that brackets it.
Level

Medium4 of 10

Topics
Prefix sum, Binary search, Array, Sorting
Solved
No attempts yet

Problem

Farmer John is teaching his cows to play a song. The song consists of NN notes (1≤N≤10,0001 \le N \le 10{,}000), and the ii-th note lasts BiB_i beats (1≤Bi≤1201 \le B_i \le 120), so the whole song is at most 1,200,0001{,}200{,}000 beats long.

Playing starts at time 00. Note 11 is played from time 00 up to just before time B1B_1, note 22 from time B1B_1 up to just before B1+B2B_1 + B_2, and in general note ii occupies the half-open interval [B1+⋯+Bi−1, B1+⋯+Bi)[B_1 + \dots + B_{i-1},\ B_1 + \dots + B_i).

To keep the cows paying attention, Farmer John asks QQ questions (1≤Q≤50,0001 \le Q \le 50{,}000). Each question gives a time TT and asks: during the interval from time TT up to just before time T+1T + 1, which note should be playing? Every query satisfies 0≤T<B1+⋯+BN0 \le T < B_1 + \dots + B_N, so exactly one note is being played. Report its 11-based index.

Input

  • Line 1: two space-separated integers NN and QQ.
  • Lines 2 to N+1N+1: line i+1i+1 contains the single integer BiB_i.
  • Lines N+2N+2 to N+Q+1N+Q+1: line N+i+1N+i+1 contains the single integer TiT_i, the time asked by the ii-th query.

Output

  • QQ lines: line ii contains a single integer, the 11-based index of the note being played during the ii-th query's interval.

Examples2

  1. Example 1

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

    Input
    5 6
    3
    1
    4
    1
    5
    0
    2
    3
    7
    8
    13
    
    Expected output
    1
    1
    2
    3
    4
    5