B. Eugeny and Play List

来源:互联网 发布:网络迷情金紫阳小说 编辑:程序博客网 时间:2024/05/23 13:21

B. Eugeny and Play List

2000ms
2000ms
262144KB
64-bit integer IO format:%I64d      Java class name: (Any)

Font Size:
Eugeny loves listening to music. He hasn songs in his play list. We know that song numberi has the duration of ti minutes. Eugeny listens to each song, perhaps more than once. He listens to song numberi ci times. Eugeny's play list is organized as follows: first song number1 plays c1 times, then song number2 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 momentx means that Eugeny wants to know which song was playing during thex-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 nextn 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 integersv1, 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 thatvi < vi + 1(i < m).

The moment of time vi means that Eugeny wants to know which song was playing during thevi-th munite from the start of listening to the playlist.

Output

Print m integers — thei-th number must equal the number of the song that was playing during thevi-th minute after Eugeny started listening to the play list.

Sample Input

Input
1 22 81 16
Output
11
Input
4 91 22 11 12 21 2 3 4 5 6 7 8 9
Output
112234444


解题思路:本题为简单题,只要搞懂题目意思,很容易做出来。不过要注意的是不要想着去开个数组做哈希表,没那么大的内存给你折腾。也不必把每首歌的开始结束时间都记录一下,只要按循序记录每首歌的结束时间即可,上首歌结束的时间就是这首歌的开始时间。

有个人要听歌,告诉你歌的首数和他询问的次数,然后给出每首歌的播放次数和歌的播放长度,要你在每次询问(给出询问时间)时,他在听那首歌,输出歌的序号即可。

用数组存储每首歌的结束时间(数组下标就是歌德序号)即可,当输入的时间vi大上首歌结束的时间却又小于这首歌的结束时间时,他就在听这首歌。又“It is guaranteed thatvi < vi + 1(i < m).”所以不必在每次询问输入时都对所有的歌进行搜寻,只要从上次搜寻对比结束时的那首歌开始搜寻即可,一旦搜到满足条件的时间,直接输出当前歌的序号(数组下标)即可。


#include<stdio.h>struct node{    int tim;} song[100005];    //存储歌的结束时间int main(){    int n,m;    int x,y;    int i,j;    while(scanf("%d%d",&n,&m)!=EOF)   //读入歌的首数,询问次数    {        for(i=1; i<=n; i++)    //读入歌的播放信息,第i首歌播放x次,每次时长y分钟        {            scanf("%d%d",&x,&y);            song[i].tim=song[i-1].tim+x*y;   //记录第i首歌播放结束的时间,=上首歌播放结束时间+次数*时长        }        j=0;        for(i=0; i<m; i++)        {            scanf("%d",&x);   //读入询问时间            while(song[j++].tim<x);  //若询问时间大于第j-1首歌结束的时间却小于第j首歌的结束时间,则但是真正播放第j首歌            j--;   //抵掉while里面的j++            printf("%d\n",j);    //输出询问是播放歌曲的序号        }    }    return 0;}