Eugeny loves listening to music. He has n songs in his play list. We know that song number i has the duration of ti minutes. Eugeny listens to each song, perhaps more than once. He listens to song number i ci times. Eugeny's play list is organized as follows: first song number 1 plays c1 times, then song number 2 plays c2 times, ..., in the end the song number n plays cn times.
Eugeny took a piece of paper and wrote out m moments of time when he liked a song. Now for each such moment he wants to know the number of the song that played at that moment. The moment x means that Eugeny wants to know which song was playing during the x-th minute of his listening to the play list.
Help Eugeny and calculate the required numbers of songs.
Input
The first line contains two integers n, m (1 ≤ n, m ≤ 105). The next n lines contain pairs of integers. The i-th line contains integers ci, ti (1 ≤ ci, ti ≤ 109) — the description of the play list. It is guaranteed that the play list's total duration doesn't exceed 109 .
The next line contains m positive integers v1, v2, ..., vm, that describe the moments Eugeny has written out. It is guaranteed that there isn't such moment of time vi, when the music doesn't play any longer. It is guaranteed that vi < vi + 1 (i < m).
The moment of time vi means that Eugeny wants to know which song was playing during the vi-th munite from the start of listening to the playlist.
Output
Print m integers — the i-th number must equal the number of the song that was playing during the vi-th minute after Eugeny started listening to the play list.
Examples
Input
1 2
2 8
1 16
Output
1
1
Input
4 9
1 2
2 1
1 1
2 2
1 2 3 4 5 6 7 8 9
Output
1
1
2
2
3
4
4
4
4
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to solve the problem: Given a playlist with n songs, each song i has duration ti minutes, and is repeated ci times consecutively. So the total timeline: for i from 1 to n, we have ci * ti minutes of song i (since each play of the song lasts ti minutes). But note: When a song plays, it occupies ti minutes. For each minute, we want to know which song is playing. The playlist order: song 1 plays c1 times (i.e., c1 repetitions, each of length ti) then song 2 plays c2 times, etc.
We have m queries: given moments v1, v2, ..., vm (sorted increasing), we need to output the song number at that minute (1-indexed minute). v_j up to maybe 1e9, but total duration of playlist is <= 1e9, and m,n up to 1e5.
We can precompute cumulative durations: Let prefix[i] = total minutes up to finishing all repetitions of song i (i.e., sum_{k=1..i} ck * tk). Then for a given minute v, we find the smallest i such that prefix[i] >= v. That i is the song playing at minute v. However careful: The minute v falls within the segment of song i. But we need to confirm: If prefix[i-1] < v <= prefix[i], then during minute v, we are within song i. That's correct because the timeline is continuous minutes. So we can binary search for each v in the prefix array. Since m up to 1e5 and n up to 1e5, O(m log n) is fine.
But note: total duration can be up to 1e9, but prefix array length n up to 1e5, storing cumulative sums is fine.
Implementation: read n,m. For i from 1 to n, read ci, ti. Compute total = ci * ti (could be up to 1e18? Actually ci, ti up to 1e9, product up to 1e18, but total duration guaranteed <= 1e9? Wait problem says: "It is guaranteed that the play list's total duration doesn't exceed 109". Actually it says: "the play list's total duration doesn't exceed 10^9" but ci, ti up to 1e9, but product may exceed 1e9 if both large; but they guarantee total sum of ci * ti across all songs does not exceed 1e9. So each product may be large but sum is <=1e9, so we can use 64-bit to be safe (long long) but 1e9 fits in 32-bit int. However cumulative sum may be up to 1e9, so 32-bit ok. But product ci * ti may exceed 2^31 if we compute product individually, but we only care about cumulative sums; we can accumulate as total += ci * ti using 64-bit to avoid overflow. Actually if ci and ti up to 1e9, their product up to 1e18, but sum of all such products <=1e9, so each product individually cannot be >1e9? Wait if sum of all is <=1e9, then each product must also be <=1e9. So ci * ti <= 1e9, so both ci and ti cannot be both large; but they are given as up to 1e9 but with the total constraint, they will be such that product <=1e9. So we could use int (32-bit) but to be safe, use long long.
Thus we store prefix array of size n, each prefix[i] = sum_{k=1..i} ck * tk. Then for each query v, find first prefix >= v. Since prefix is non-decreasing and n up to 1e5, we can binary search manually or use lower_bound. We'll do for each v in m queries, print the index (1-indexed) of the found song.
Edge Cases: If v exactly at boundary where prefix[i-1] = v? But minutes start at 1. Actually if total minutes is T, v is from 1 to T. For minute v = prefix[i-1], that means after exactly prefix[i-1] minutes, what song plays? Let's define: At minute 1, we are in the first minute of the first song. At minute prefix[1] = c1 * t1, we are in the last minute of song 1. Next minute (prefix[1] + 1) is first minute of song 2. So if v equals prefix[i-1], then it is the last minute of song i-1 (if i>1) or the last minute of song i? Actually prefix[i-1] is total up to song i-1. So v = prefix[i-1] lies within song i-1 (its last minute). So condition: v <= prefix[i-1] => song i-1 or earlier. So to find song for v, we want smallest i such that prefix[i] >= v. Then song i is the one. For v = prefix[i-1], we have prefix[i-1] >= v, so i = i-1 is the first index with prefix >= v, so we correctly get song i-1. So using lower_bound (first >=) works.
Implementation details:
Read n, m.
Initialize vector pref(n).
long long sum = 0.
For i=0..n-1: read ci, ti; sum += ci * ti; pref[i] = sum.
For each query v (read as int, but maybe up to 1e9), use lower_bound(pref.begin(), pref.end(), v) to get iterator it. Index = it - pref.begin() + 1 (since 1-indexed). Print index.
Complexity O((n+m) log n) which is fine.
But note that m can be 1e5, but using lower_bound for each query is O(log n) each, total 1e5 * log(1e5) ~ 1.7e6, fine.
Alternative: Since v sorted increasingly, we could do a two-pointer scan: i pointer over songs, current cumulative time, and for each v, advance i until v <= cumulative, output i+1. That would be O(n+m). However both are fine. But we need to be careful that v sorted: they said vi < vi+1, so we can do the two-pointer scan which might be faster. But O(m log n) is fine.
We'll implement the two-pointer scan for efficiency and simplicity: It avoids binary search overhead.
Implementation of two-pointer:
Initialize idx = 0 (0-indexed for songs), cum = 0, next_cum = pref[0]? Or we can compute on fly.
For each query v:
while v > cum and idx < n:
// Actually we want to ensure that v falls within current song segment [cum+1, cum+len]? Better: maintain current segment's end.
Let's maintain current_song = 1 (1-indexed), current_end = c1t1.
Then for v, while v > current_end: move to next song: current_song++, current_end += citi.
Then output current_song.
Since v sorted, we can just move forward.
But careful: v can be equal to current_end, then it's within current song. So condition v > current_end to move to next.
Pseudo:
read n, m.
long long cur_end = 0;
int song_idx = 0; // index in songs array, 0-based.
read first song's ci, ti: total length = ci * ti.
Store maybe an array of lengths? Or process on fly.
But we need to handle all songs: we can read all songs first into arrays ci, ti to compute lengths. But we can also store lengths array.
Simplify: read all songs into vectors c, t. Compute len[i] = c[i] * t[i].
Then set cur_end = len[0]; song_idx = 0.
For each query v in sorted list:
while (v > cur_end) {
song_idx++;
cur_end += len[song_idx];
}
print song_idx+1.
This is O(n+m) and simple.
But note: v is guaranteed not to exceed total duration.
Edge Cases: When v is exactly cur_end, we don't move, output song_idx+1 correct.
We must ensure that we correctly handle the first query: if v=1, while (1 > cur_end?) cur_end initially = len[0] which is positive, so likely 1 <= len[0] unless len[0] is zero? Not possible because ci, ti >=1, so len[0]>=1, so fine.
Implementation in Python? The problem expects C++? But we are to provide final answer in code? The user asked: "Write the solution in python." So we need to provide Python code.
Thus we need to produce Python solution. However careful about input size and speed: m up to 1e5, n up to 1e5, total duration up to 1e9, but Python can handle using sys.stdin.read.
We'll implement using sys.stdin.buffer.read to parse ints quickly.
Approach: Two-pointer scan.
Pseudo:
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
it = iter(data)
n = next(it); m = next(it)
lens = []
for _ in range(n):
c = next(it); t = next(it)
lens.append(c * t)
queries = [next(it) for _ in range(m)]
code
ans = []
song_idx = 0
cur_end = lens[0]
for v in queries:
while v > cur_end:
song_idx += 1
cur_end += lens[song_idx]
ans.append(str(song_idx+1))
sys.stdout.write("\n".join(ans))
if name == "main":
main()
Check with examples:
Example 1:
n=1, m=2
song: c=2,t=8 => lens[0]=16
queries: 1,16
v=3: while 3>2 => song_idx=1, cur_end=4; 3>4? false output2
v=4: 4>4? false output2
v=5: while 5>4 => song_idx=2, cur_end=5; 5>5? false output3
v=6: while 6>5 => song_idx=3, cur_end=9; 6>9? false output4
v=7: 7>9? false output4
v=8: 8>9? false output4
v=9: 9>9? false output4
Matches expected.
Thus solution is correct.
Now we need to produce final answer: Provide explanation and code. User asked: "Write the solution in python." So we output Python code.
But also the conversation suggests they want a response: "You are a helpful assistant. Provide solution in python." So final answer should include code and maybe brief explanation. But given the context, the user asked to write the solution in python. So we should produce code with explanation.
Thus produce final message containing Python solution.
But let's also consider potential pitfalls: The two-pointer scan requires that queries are sorted. They are. However if they were not sorted, we'd need binary search. But spec says vi < vi+1, so sorted.
Thus solution is fine.
Now write final answer with explanation and code.
We'll produce the answer in the final channel. We can solve the problem by simulating the timeline of the playlist. Each song i occupies c_i * t_i minutes in total. Because the queries v_j are given in increasing order, we can scan through the songs while keeping track of the current cumulative end minute. For each query, we advance to the next song until the query minute falls within the current song's interval, and then output the song number.
Time complexity: O(n + m), which is efficient for the given constraints.
python
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
it = iter(data)
n = next(it)
m = next(it)
# length of each song block in minutes
lengths = []
for _ in range(n):
c = next(it)
t = next(it)
lengths.append(c * t)
queries = [next(it) for _ in range(m)]
ans = []
song_idx = 0 # 0‑based index of current song
cur_end = lengths[0] # last minute of the current song block
for v in queries:
# move forward until v lies within the current song block
while v > cur_end:
song_idx += 1
cur_end += lengths[song_idx]
ans.append(str(song_idx + 1)) # convert to 1‑based song number
sys.stdout.write("\n".join(ans))
if __name__ == "__main__":
main()