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> ...
随机推荐
- Spring 控制台运行及RestTemplate实现Eurka负载均衡
spring使用控制台运行方式 spring.main.web-application-type=none新老版本的配置有点差异 Maven的modules只是实现了一个顺序编译,一次多个项目一起生成 ...
- C中常用格式格式码
一.常用printf格式码 二.常用scanf格式码
- learnyounode 题解
//第三题 var fs =require('fs')var path=process.argv[2]fs.readFile(path,function(err,data){ var lines=da ...
- 关于angular导入第三方库的问题
angular-cli使用webpack来将模块打包,在这里配置的scripts和styles会被打包成script.bundle.js和styles.bundle.js文件加载到前台页面. 这样就可 ...
- mac上卸载mysql
在终端输入一下命令 sudo rm /usr/local/mysqlsudo rm -rf /usr/local/mysql*sudo rm -rf /Library/StartupItems/MyS ...
- Linux文件访问流程及磁盘inode和block总结
Linux文件访问流程 inode是文件的唯一标识,文件名和inode的对应关系存放在上一级目录的block中:inode里有指向文件block的指针和文件的属性,从而通过block获得文件数据. 磁 ...
- [ python ] 软件开发规范
在python开发中,我们建议采用如下规范: soft/ ├── bin # 程序执行文件目录 │ ├── __init__.py │ └── start.py # 程序开始执行脚本文件 ├─ ...
- Java基础1,入门基础知识
本文知识点(目录): 1.java简介 2.环境的搭建 3.关键字 4.标识符 5.注释 6.常量 7.进制的转换 8.变量 9.数据类型的转换 ...
- PHP学习笔记之数组游标操作
数组有N个单元,同时只能操作一个单元.比如循环时,只能一个一个单元读取他的值. 那么数组是怎么记住刚才读取的是哪个单元,接着读取下个单元的呢? 在数组内部,有一个指针,指针指向某一个单元. 每循环一个 ...
- 关于函数strtok和strtok_r的使用要点和实现原理
strtok函数的使用是一个老生常谈的问题了.该函数的作用很大,争议也很大.以下的表述可能与一些资料有区别或者说与你原来的认识有差异,因此,我尽量以实验为证.交代一下实验环境是必要的,winxp+vc ...