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[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** ll a[maxn],n,m;
int cx[maxn],cy[maxn];
ll ans[maxn];
ll dp[maxn];
int kiss=;
vector<int> d[];
int main()
{
//test;
n=read();
for(int i=;i<=n;i++)
a[i]=read();
int m=read();
for(int i=;i<=m;i++)
{
cx[i]=read(),cy[i]=read();
if(cy[i]<kiss)
d[cy[i]].push_back(i);
else
{
ll ans1=;
for(int j=cx[i];j<=n;j+=cy[i])
{
ans1+=a[j];
}
ans[i]=ans1;
}
}
for(int i=;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=;j<d[i].size();j++)
ans[d[i][j]]=dp[cx[d[i][j]]];
}
}
for(int i=;i<=m;i++)
cout<<ans[i]<<endl;
}
Codeforces Beta Round #80 (Div. 1 Only) D. Time to Raid Cowavans 分块的更多相关文章
- Codeforces Beta Round #80 (Div. 1 Only) D. Time to Raid Cowavans 离线+分块
题目链接: http://codeforces.com/contest/103/problem/D D. Time to Raid Cowavans time limit per test:4 sec ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- 分治思想 特别常用 Codeforces Beta Round #80 (Div. 1 Only) D
D. Time to Raid Cowavans time limit per test 4 seconds memory limit per test 70 megabytes input stan ...
- Codeforces Beta Round #69 (Div. 2 Only)
Codeforces Beta Round #69 (Div. 2 Only) http://codeforces.com/contest/80 A #include<bits/stdc++.h ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
随机推荐
- Appium 1.6.3使用的自动化测试引擎
automationName项的值: Appium:默认值. Selendroid:安卓2.3(API 9)-4.1(API 16)版本使用. UiAutomator2:最新安卓版本. XCUITes ...
- samba中的pdbedit用法
pdbedit用于在samba服务器中创建用户: 它的用法包括 pdbedit -a username:新建Samba账户. pdbedit -x username:删除Samba账户. pdbedi ...
- 制作一棵ztree
我们在做web项目时,常会用到一些树形菜单.在此,我们利用ztree实现树形菜单的效果.zTree 是一个依靠 jQuery 实现的多功能 “树插件”.优异的性能.灵活的配置.多种功能的组合是 zTr ...
- xpath简单应用
相对路径与绝对路径: 如果"/"处在XPath表达式开头则表示文档根元素,(表达式中间作为分隔符用以分割每一个步进表达式)如:/messages/message/subject是一 ...
- scrapy抓取小说
用scrapy建立一个project,名字为Spider scrapy startproject Spider 因为之前一直用的是电脑自带的python版本,所以在安装scrapy时,有很多问题,也没 ...
- EF – 4.CRUD与事务
5.6.1 <Entity Framework数据更新概述> 首先介绍Entity Framework实现CRUD的基本方法,接着介绍了如何使用分部类增强和调整数据实体类的功能与行为特性 ...
- C语言小程序之整除
看到有人要求用C语言写这样一个小程序,就拿来温习一下 需求:输出从1到2015这2015个自然数中,能被4或5整除,但不能被30整除的数,并计算有多少个数. #include<stdio.h ...
- android4.0 锁屏实现(转)
转载请表明出处:http://blog.csdn.net/wdaming1986/article/details/8837023 好了,言归正传,说说锁屏了,其实把锁屏做成apk的形式,会引起很多问题 ...
- IEEEXtreme 10.0 - N-Palindromes
这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - N-Palindromes 题目来源 第10届IEEE极限编程大赛 https://www.hackerra ...
- SGU 206. Roads
206. Roads time limit per test: 0.5 sec. memory limit per test: 65536 KB input: standard output: sta ...