https://codeforces.com/problemset/problem/1490/G
思路:
luogu上谁翻译的刚好到x...无语题意明明是>=x的第一个位置。
那就是青蛙跳井。多次询问要二分优化,因为如果问一次可以暴力最后On扫一次。多次map上二分优化
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=2e5+1000;
typedef long long LL;
inline LL read(){LL x=0,f=1;char ch=getchar(); while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn];
map<LL,LL>map1;
int main(void){
cin.tie(0);std::ios::sync_with_stdio(false);
LL T;cin>>T;
while(T--){
LL n,m;cin>>n>>m;
map1.clear();
LL mx=0;LL sum=0;LL pos=0;
for(LL i=1;i<=n;i++){
cin>>a[i];
sum+=a[i];
if(sum>mx){
mx=sum;pos=i;map1[mx]=pos;
}
}
map<LL,LL>::iterator it;
while(m--){
LL x;cin>>x;
it=map1.lower_bound(x);
if(it==map1.end()&&sum<=0){
cout<<"-1"<<" ";
continue;
}
else if(it==map1.end()){
LL temp=x-mx;
LL k=(temp+sum-1)/sum;
LL ans=n*k;
map<LL,LL>:: iterator it2;
it2=map1.lower_bound(x-k*sum);
cout<<(it2->second+ans-1)<<" ";
}
else{
cout<<(it->second-1)<<" ";///题目下标对应的时间从0开始
}
}
cout<<"\n";
}
return 0;
}