CF round#302 B (13.10.17)

Description

Eugeny loves listening to music. He hasnsongs in his play list. We know that song numberihas the duration oftiminutes. Eugeny listens to each song, perhaps more than once. He listens to song numbericitimes. Eugeny's play list is organized as follows: first song number1playsc1times, then song number2playsc2times,..., in the end the song numbernplayscntimes.

Eugeny took a piece of paper and wrote outmmoments 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 momentxmeans 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 integersn,m(1 ≤ n, m ≤ 105). The nextnlines contain pairs of integers. Thei-th line contains integersci, ti(1 ≤ ci, ti ≤ 109)— the description of the play list. It is guaranteed that the play list's total duration doesn't exceed109.

The next line containsmpositive integersv1, v2, ..., vm, that describe the moments Eugeny has written out. It is guaranteed that there isn't such moment of timevi, when the music doesn't play any longer. It is guaranteed thatvi < vi + 1(i < m).

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

Output

Printmintegers — 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 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

题意: 给出n首歌, m个时间点

接下来的n行, 每行两个数, 分别代表该首歌要循环放几次, 一次放多久

然后最后一行输入m个时间点, 求在这m个时间点里, 每个时间点是在放哪首歌?


做法: 直接for循环了, 也有二分法做法


AC代码:

#include<stdio.h>

int n, m;
int songList[311111];
int time[311111];

int main() {
    while(scanf("%d %d", &n, &m) != EOF) {
        songList[0] = 0;
        for(int i = 1; i <= n; i++) {
            int v, c;
            scanf("%d %d", &v, &c);
            songList[i] = v * c + songList[i-1];
        }
        for(int i = 0; i < m; i++)
            scanf("%d", &time[i]);
        int begin = 1;
        for(int i = 0; i < m; i++) {
            for(int j = begin; j <= n; j++) {
                if(time[i] > songList[j-1] && time[i] <= songList[j]) {
                    printf("%d\n", j);
                    begin = j;
                    break;
                }
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值