B - Time to Raid Cowavans CodeForces - 103D(分块+离线)

在一场关于牛群被外星人劫持的趣味数学竞赛题中,参赛者需运用离线操作和分块思想来解决一系列询问。通过预处理和高效算法,找到特定条件下被劫持牛的总质量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

As you know, the most intelligent beings on the Earth are, of course, cows. This conclusion was reached long ago by the Martian aliens, as well as a number of other intelligent civilizations from outer space.

Sometimes cows gather into cowavans. This seems to be seasonal. But at this time the cows become passive and react poorly to external stimuli. A cowavan is a perfect target for the Martian scientific saucer, it’s time for large-scale abductions, or, as the Martians say, raids. Simply put, a cowavan is a set of cows in a row.

If we number all cows in the cowavan with positive integers from 1 to n, then we can formalize the popular model of abduction, known as the (a, b)-Cowavan Raid: first they steal a cow number a, then number a + b, then — number a + 2·b, and so on, until the number of an abducted cow exceeds n. During one raid the cows are not renumbered.

The aliens would be happy to place all the cows on board of their hospitable ship, but unfortunately, the amount of cargo space is very, very limited. The researchers, knowing the mass of each cow in the cowavan, made p scenarios of the (a, b)-raid. Now they want to identify the following thing for each scenario individually: what total mass of pure beef will get on board of the ship. All the scenarios are independent, in the process of performing the calculations the cows are not being stolen.

Input
The first line contains the only positive integer n (1 ≤ n ≤ 3·105) — the number of cows in the cowavan.

The second number contains n positive integer wi, separated by spaces, where the i-th number describes the mass of the i-th cow in the cowavan (1 ≤ wi ≤ 109).

The third line contains the only positive integer p — the number of scenarios of (a, b)-raids (1 ≤ p ≤ 3·105).

Each following line contains integer parameters a and b of the corresponding scenario (1 ≤ a, b ≤ n).

Output
Print for each scenario of the (a, b)-raid the total mass of cows, that can be stolen using only this scenario.

Please, do not use the %lld specificator to read or write 64-bit integers in С++. It is recommended to use the cin, cout streams of the %I64d specificator.

Examples
Input
3
1 2 3
2
1 1
1 2
Output
6
4
Input
4
2 3 5 7
3
1 3
2 3
2 2
Output
9
3
10

题目大意:给你一个数目为n的数列 输入 a[1]…a[n] 有m个询问 每次给出x,y 问 x,x+y,x+2y …直到不大于n的下标数字的和

题解:采用分块的思想 和离线操作
b>=√n的询问,每次暴力求值的复杂度不超过O(√n),总复杂度为O(m√n)
b<√n 下标放进vector中 离线操作,采用dp,从大到小,i相当于间隔b,将间隔b的所有答案找出存入ans中。

#include<bits/stdc++.h>
#define fi first
#define se second
#define FOR(a) for(int i=0;i<a;i++)
#define show(a) cout<<a<<endl;
#define show2(a,b) cout<<a<<" "<<b<<endl;
#define show3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl;
using namespace std;

typedef long long ll;
typedef pair<int,int> P;
typedef pair<P,int> LP;
const ll inf=1e18;
const int N=4e5+10;
const ll mod=1e3+3;

ll a[N];
ll x[N],y[N],n,m,ans[N],dp[N];
vector<int> v[1005];
double k[N],mx[N];



int main()
{


	scanf("%I64d",&n);
	for(int i=1;i<=n;i++)scanf("%I64d",&a[i]);
	int ff=sqrt(n);
	scanf("%I64d",&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%I64d%I64d",&x[i],&y[i]);
		if(y[i]<ff)
		{
			v[y[i]].push_back(i);
		}
		else
		{
			ll sum=0;
			for(int j=x[i];j<=n;j+=y[i])
				sum+=a[j];
			ans[i]=sum;
		}
	}
	for(int i=1;i<ff;i++)
	{
		if(v[i].size())
		{
			for(int j=n;j;j--)
			{
				if(j+i>n)
					dp[j]=a[j];
				else
					dp[j]=dp[i+j]+a[j];
			}
			for(int j=0;j<v[i].size();j++)
				ans[v[i][j]]=dp[x[v[i][j]]];
		}
	}
	for(int i=1;i<=m;i++)
		printf("%I64d\n",ans[i]);



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值