CF简单优化

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 nm (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.

Sample test(s)
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

AC代码:

速度也还可以

302B - Eugeny and Play List GNU C++ Accepted 125 ms 3100 KB

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <vector>
#include <stack>
#include <map>
#include <queue>
#include <set>
#include <iostream>
#include <stdlib.h>
#include <cctype>
#define rep1(i,s,e) for(int i = (s);i<(e);++i)
#define rep2(i,s,e) for(int i = (s);i<=(e);++i)
#define maxn 100100
#define INF 1000000007
#define ls (root<<1)
#define rs (root<<1|1)
#define M  L+(R-L)/2
using namespace std;

int n,m;
struct song
{
    long long c;
    long long t;
    long long start;
    long long  end;
}s[maxn];
//有一个简单的优化。。。
//因为题目告诉我们:It is guaranteed that vi < vi + 1 (i<m)
//所以我们每次没必要从第一首歌开始找start和end而是保存上一次查找的结果。。从上一次的结果处开始查找
int main()
{
    //freopen("in.txt","r",stdin);
    while(scanf("%d%d",&n,&m)==2)
    {
        int sum = 0;
        for(int i = 1;i<=n;++i)
        {
         s[i].start = sum;

         scanf("%I64d%I64d",&s[i].c,&s[i].t);
         sum +=  s[i].c*s[i].t;

         s[i].end = sum;
        }
        int b = 1;
        for(int i = 0;i<m;++i)
        {
            int time;
            scanf("%I64d",&time);
            for(int j = b;j<=n;++j)
            {
                if(time>=s[j].start&&time<=s[j].end)
                {
                    b = j;
                    printf("%d\n",j);
                    break;
                }
            }

        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值