C. Valhalla Siege
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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 n
warriors, he places them on a straight line in front of the main gate, in a way that the i-th warrior stands right after (i−1)
-th warrior. The first warrior leads the attack.
Each attacker can take up to ai
arrows before he falls to the ground, where ai is the i
-th warrior's strength.
Lagertha orders her warriors to shoot ki
arrows during the i-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 t, they will all be standing to fight at the end of minute t
.
The battle will last for q
minutes, after each minute you should tell Ivar what is the number of his standing warriors.
Input
The first line contains two integers n
and q (1≤n,q≤200000
) — the number of warriors and the number of minutes in the battle.
The second line contains n
integers a1,a2,…,an (1≤ai≤109
) that represent the warriors' strengths.
The third line contains q
integers k1,k2,…,kq (1≤ki≤1014), the i-th of them represents Lagertha's order at the i-th minute: ki
arrows will attack the warriors.
Output
Output q
lines, the i-th of them is the number of standing warriors after the i
-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
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<string>
#include<cmath>
using namespace std;
typedef long long ll;
const ll maxn=200005;
ll a[maxn];
ll sum[maxn];
ll kil[maxn];
int main()
{
ll n,q;
scanf("%lld %lld",&n,&q);
sum[0]=0;
for(ll i=1;i<=n;i++){
scanf("%lld",&a[i]);
sum[i]=sum[i-1]+a[i];
}
for(ll i=1;i<=q;i++){
scanf("%lld",&kil[i]);
}
/*
for(ll i=1;i<=n;i++){
printf("%lld ",sum[i]);
}
printf("\n");
*/
ll pos=1;
ll xsu=0;
for(ll i=1;i<=q;i++){
int f=pos;
pos=lower_bound(sum,sum+n+1,kil[i]+sum[pos-1]+xsu)-sum;
//printf("----\n");
//printf("f==%lld xsu==%lld\n",f,xsu);
//printf("pos==%lld\n",pos);
if(pos==n+1){
printf("%lld\n",n);
xsu=0;
pos=1;
}else if(pos==n&&kil[i]+sum[f-1]+xsu==sum[n]){
printf("%lld\n",n);
xsu=0;
pos=1;
}else{
if(sum[pos]==kil[i]+sum[f-1]+xsu){
printf("%lld\n",n-pos);
pos=pos+1;
xsu=0;
}else{
printf("%lld\n",n-pos+1);
xsu=(kil[i]+sum[f-1]+xsu)-sum[pos-1];
}
}
//printf("-----\n");
}
return 0;
}