Codeforces Gym100571A Cursed Query

A. Cursed Query

time limit per test: 1 second
memory limit per test: 256 megabytes
inputstandard input
outputstandard output

De Prezer loves movies and series. He has watched the Troy for like 100 times and also he is a big fan of Supernatural series.So, he did some researches and found a cursed object which had n lights on it and initially all of them were turned off.Because of his love to the Troy, he called that object Troy.

He looked and saw a note on it in Khikulish (language of people of Khikuland): “Ma in hame rah umadim, hala migi … ? … Mage man De Prezer am k mikhay mano … ? …. Man se sale … To boro …. beshur manam miram … o mishuram”.

He doesn’t know Khikulish, so just ignored the note and tested the Troy.

He realized that the light number i stays turned on for exactly ai seconds, and then it turns itself off (if it is turned on, in time t, in time t + ai - 1 it will be turned on, but on time t + ai it won’t be) and the next light will be turned on (if i < n, next light is the light number i + 1, otherwise it is light with number 1).

For example if n = 2 and we turn on the first light in time 0, it will be turned on in hole interval [0, a1) and in hole interval [a1, a1 + a2) the second light will be turned on and so on.

In time 0 he turns on the light number 1.

De Prezer also loves query.So he gives you q queries.In each query he will give you integer t (the time a revengeful ghost attacked him) and you should print the number of the light that is turned on, in time t.

Input

The first line of input contains two integers n and q.

The second line contains n space separated integers, a1, a2, …, an .

The next q lines, each line contains a single integer t .

1 ≤ n, q ≤ 105

1 ≤ ai ≤ 109

1 ≤ t ≤ 1018

Output

For each query, print the answer in one line.

Sample test(s)

input

5 7
1 2 3 4 5
1
2
3
7
14
15
16

output

2
2
3
4
5
1
2

题意

第i盏灯亮ai秒,从第一盏灯开始亮,给你一个时间,问此时哪盏灯亮。

题解

维护一个前缀和,A[i]表示第i盏灯第一次熄灭时距0时刻的时间,t对A[n]取余之后二分即可。代码如下

#include <cstdio>
#include <algorithm>

using namespace std;

int n,q;
long long A[100005];
long long ALL,t;
long long *ans;
int main()
{
    scanf("%d%d",&n,&q);
    A[0]=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%I64d",&A[i]);
        A[i]+=A[i-1];
    }
    ALL=A[n];
    for(int i=0;i<q;i++)
    {
        scanf("%I64d",&t);
        t%=ALL;
        ans=lower_bound(A,A+n+1,t);
        if(*ans==t)
            printf("%d\n",ans-A+1);
        else
            printf("%d\n",ans-A);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值