2021CCPC网络赛(重赛)——1005.E.Monopoly
作者:互联网
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define PB push_back
#define ALL(a) begin(a),end(a)
#define mem(a,n) memset(a,n,sizeof(a))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int maxn = 2e5+5;
ll n, m, rd, x;
ll a[maxn], pre[maxn];
map<ll,int> vs;
map<ll,vector<pair<ll,int>>> shu;
void solve()
{
cin>>n>>m;
bool flag = 0;
rd = 0;
for(int i=1;i<=n;i++)
{
cin>>a[i];
pre[i] = pre[i-1] + a[i];
rd += a[i];
}
if(rd < 0)
{
rd = -rd;
flag = 1;
for(int i=1;i<=n;i++) pre[i] = -pre[i];
}
//
if(rd == 0)
{
vs.clear();
for(int i=1;i<=n;i++)
{
if(vs.find(pre[i])==vs.end())
vs[pre[i]] = i;
}
while(m--)
{
cin>>x;
if(x==0)
{
cout<<0<<endl;
continue;
}
if(vs.find(x)==vs.end()) cout<<-1<<endl;
else cout<<vs[x]<<endl;
}
return ;
}
//
shu.clear();
for(int i=1;i<=n;i++)
{
ll yus = (pre[i] % rd + rd) % rd;
shu[yus].PB({-pre[i],i});
}
for(auto it=shu.begin(); it!=shu.end(); it++)
sort(it->second.begin(), it->second.end());
while(m--)
{
cin>>x;
if(flag) x = -x;
if(x==0)
{
cout<<0<<endl;
continue;
}
ll xx = (x % rd + rd) % rd;
if(shu.find(xx)==shu.end())
{
cout<<-1<<endl;
continue;
}
pair<ll,int> ask = {-x, 0};
int pos = lower_bound(ALL(shu[xx]), ask) - shu[xx].begin();
if(pos == shu[xx].size())
{
cout<<-1<<endl;
}
else
{
ll ans = shu[xx][pos].sec;
ans += (x + shu[xx][pos].fir) / rd * n;
cout<<ans<<endl;
}
}
}
int main()
{
IOS;
int _;cin>>_;while(_--)
solve();return 0;
}
标签:pre,rd,shu,cout,cin,2021CCPC,1005,重赛,define 来源: https://blog.csdn.net/qq_45928596/article/details/120694120