Codeforces Beta Round #80 (Div. 1 Only) D. Time to Raid Cowavans 分块

本文深入探讨了代码优化技术及算法应用的核心概念,通过实例分析了如何在实际编程中提高效率并解决复杂问题。

D. Turtles

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/103/problem/D

Description

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.

 

Sample Input

3
1 2 3
2
1 1
1 2

Sample Output

6
4

HINT

 

题意

给你N个数,然后每次询问给你(x,y),求a[x]+a[x+y]+a[x+2*y]+a[x+3*y]的和是多少

 

题解:

分块做,把cy[i]分成sqrt(n),把小于的都预存起来,大于的就直接暴力就好

小的,我们就可以利用 dp的思想搞定

然后复杂度O(sqrt(n))

详情见:2014年国家集训队论文《根号算法,不只是分块》

代码:

 

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)  
#define maxn 400001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************

ll a[maxn],n,m;
int cx[maxn],cy[maxn];
ll ans[maxn];
ll dp[maxn];
int kiss=600;
vector<int> d[600];
int main()
{
    //test;
    n=read();
    for(int i=1;i<=n;i++)
        a[i]=read();
    int m=read();
    for(int i=1;i<=m;i++)
    {
        cx[i]=read(),cy[i]=read();
        if(cy[i]<kiss)
            d[cy[i]].push_back(i);
        else
        {
            ll ans1=0;
            for(int j=cx[i];j<=n;j+=cy[i])
            {
                ans1+=a[j];
            }
            ans[i]=ans1;
        }
    }
    for(int i=1;i<kiss;i++)
    {
        if(d[i].size())
        {
            for(int j=n;j;j--)
            {
                if(j+i>n)
                    dp[j]=a[j];
                else
                    dp[j]=dp[j+i]+a[j];
            }
            for(int j=0;j<d[i].size();j++)
                ans[d[i][j]]=dp[cx[d[i][j]]];
        }
    }
    for(int i=1;i<=m;i++)
        cout<<ans[i]<<endl;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值