Valhalla Siege CodeForces - 975C (前缀合+二分)

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 (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: 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次攻击,每个士兵可以抵抗ai点伤害,每次攻击为bi点伤害,如果所有士兵都死亡,那么,所有士兵会立马复活。问,每次攻击以后,有多少士兵活着。如样例1

5 5
1 2 1 2 1
3 10 1 1 1

第一次攻击为3点伤害,刚好杀死前两个士兵,所以,第一轮剩3个,第二次10点,杀死所有士兵,士兵复活,剩5个,第三次,杀死第一个士兵,第四次和第5次加起来杀死第二个士兵。

思路:前缀和,记录前i个士兵一共可以抵挡多少伤害,然后二分查找一个比伤害值高的值(注意,由于这里的伤害值是累加的,(也就是说,第一次攻击没杀死一个士兵,那么,士兵就消耗相应的承受值)所以,我们查找的伤害值也是累加的,直到把所有的士兵都杀死,重置伤害值)

#include "iostream"
#include "algorithm"
using namespace std;
typedef long long ll;
const int Max=2e5+10;
ll HP[Max];
int main()
{
    ios::sync_with_stdio(false);
   int n,q,x;
   ll k,sum=0;
   cin>>n>>q;
   for(int i=1;i<=n;i++){
       cin>>x;  
       HP[i]=HP[i-1]+x;//计算前缀和
   }
   for(int i=1;i<=q;i++){
       cin>>k;
       sum+=k;//伤害值的累加
       int ans=upper_bound(HP+1,HP+1+n,sum)-HP;
       ans=n-ans+1;
       if(ans==0) sum=0,ans=n;//如果杀死了所有士兵,那么士兵立马复活,伤害值重置
       cout<<ans<<endl;
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41874469/article/details/80601664