Codeforces Round #478 (Div. 2) C. Valhalla Siege

C. Valhalla Siege

Ivar the Boneless is a great leader. He is trying to capture Kattegat from Lagertha. The war has begun and wave after wave Ivar's warriors are falling in battle.

Ivar has nn warriors, he places them on a straight line in front of the main gate, in a way that the ii-th warrior stands right after (i1)(i−1)-th warrior. The first warrior leads the attack.

Each attacker can take up to aiai arrows before he falls to the ground, where aiai is the ii-th warrior's strength.

Lagertha orders her warriors to shoot kiki arrows during the ii-th minute, the arrows one by one hit the first still standing warrior. After all Ivar's warriors fall and all the currently flying arrows fly by, Thor smashes his hammer and all Ivar's warriors get their previous strengths back and stand up to fight again. In other words, if all warriors die in minute tt, they will all be standing to fight at the end of minute tt.

The battle will last for qq minutes, after each minute you should tell Ivar what is the number of his standing warriors.

Input

The first line contains two integers nn and qq (1n,q2000001≤n,q≤200000) — the number of warriors and the number of minutes in the battle.

The second line contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) that represent the warriors' strengths.

The third line contains qq integers k1,k2,,kqk1,k2,…,kq (1ki10141≤ki≤1014), the ii-th of them represents Lagertha's order at the ii-th minute: kikiarrows will attack the warriors.

Output

Output qq lines, the ii-th of them is the number of standing warriors after the ii-th minute.

Examples
input
Copy
5 5
1 2 1 2 1
3 10 1 1 1
output
Copy
3
5
4
4
3
input
Copy
4 4
1 2 3 4
9 1 10 6
output
Copy
1
4
4
1

题意:现在有n个人站成一排,a[i]代表生命值,有q轮放箭,每一轮k个箭,每轮都从最左边开始射击,直到倒下依次向右继续射击,问你经过本轮射击,还有多少人活着。当本轮所有人倒下之后,同时所有人恢复生命。

思路:求出a[i]的前缀和,每次二分可以击杀的最右端,即可得到剩余活着的人数,不过本题是个动态的二分过程,需要定义一个now代表当前活着的最左边的人,也就是二分的左端点,在定义一个tmp代表已经死掉的,每次二分需要提前减去,每次二分反复更新now和tmp即可。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<map>
#include<vector>
#include<queue>
#include<stack>
#include<set>
#define ll long long
#define exp 1e-8
#define mst(a,k) memset(a,k,sizeof(a))
using namespace std;
ll a[200020],n,q;
int main()
{
    while(~scanf("%lld%lld",&n,&q))
    {
        a[0]=0;
        for(ll i=1;i<=n;i++)   //前缀和
        {
            scanf("%lld",&a[i]);
            a[i]+=a[i-1];
        }
        ll now=1,tmp=0;   //活着的第一个人,已经死掉的人数
        while(q--)
        {
            ll x;
            scanf("%lld",&x);
            ll l=now,r=n+1,mid=(l+r)>>1;
            while(l<r)
            {
                if((a[mid]-tmp)<=x)l=mid+1;
                else r=mid;
                mid=(l+r)>>1;
            }
            now=l;   //更新第一个人
            tmp+=x;   //更新死亡人数
            if(n-l+1==0)printf("%lld\n",n);   //全部死亡时,自动全部复活
            else printf("%lld\n",n-l+1);
            if(l==n+1)   //全部死亡时复活
            {
                now=1;
                tmp=0;
            }
        }
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/s540239976/article/details/80283745
今日推荐