2021CCPC网络赛(重赛)——1005.E.Monopoly

离散化 + 二分 639ms

#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);
#define endl '\n'
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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Musyoku

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值