Codeforces Round #478 (Div. 2)-C. Valhalla Siege【二分】

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 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 (i−1)(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 (1≤n,q≤2000001≤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 (1≤ai≤1091≤ai≤109) that represent the warriors' strengths.

The third line contains qq integers k1,k2,…,kqk1,k2,…,kq (1≤ki≤10141≤ki≤1014), the ii-th of them represents Lagertha's order at the ii-th minute: kiki arrows 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

Note

In the first example:

  • after the 1-st minute, the 1-st and 2-nd warriors die.
  • after the 2-nd minute all warriors die (and all arrows left over are wasted), then they will be revived thus answer is 5 — all warriors are alive.
  • after the 3-rd minute, the 1-st warrior dies.
  • after the 4-th minute, the 2-nd warrior takes a hit and his strength decreases by 1.
  • after the 5-th minute, the 2-nd warrior dies.

题意:有一列n个勇士,他们将会承受q分钟的箭击,第i个人承受值为a[i],第j分钟会有攻击值为k[j]的箭击,当第i个人受到大小为a[i]的箭击后将会倒下,该分钟剩余的攻击将会作用于倒下的人的后面的人身上,当某分钟所有人均倒下时,那么所有人将会在该分钟满血复活,而该分钟剩余的攻击值将会无效,即该分钟后所有人的承受值重新加满,都站着。求对于每分钟攻击后,有多少人存活。

题解:首先求a[i]的前缀和sum[ ],然后对于每分钟都仔细分析:对于当前分钟,根据上一分钟攻击到的位置pos及攻击到的勇士被消耗的值的大小xsu,做这一步的判断,并且更新pos与xsu;具体思路看代码。

 

代码如下:
 

#include
#include
#include
#include
#include
#include
#include
#include
#include
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]);
	}
	
	ll pos=1; 
	ll xsu=0;
	for(ll i=1;i<=q;i++){
		int f=pos;//上一轮攻击到的那个人的位置;xsu:代表那个人已经被消耗的值是多少 
		pos=lower_bound(sum,sum+n+1,kil[i]+sum[pos-1]+xsu)-sum;//此轮攻击到的位置 
		
		
		//根据此轮攻击到的位置,更新下轮要用到的pos值及xsu值,并输出此轮结果 
		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];
			}
		}
		
	}
	
	
	return 0;
 } 

 

你可能感兴趣的:(思维,二分)