D. Time to Raid Cowavans
time limit per test

4 seconds

memory limit per test

70 megabytes

input

standard input

output

standard output

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
  1. 3
    1 2 3
    2
    1 1
    1 2
output
  1. 6
    4
input
  1. 4
    2 3 5 7
    3
    1 3
    2 3
    2 2
output
  1. 9
    3
    10

题目大意:给你一个长度为n的序列w,求w[x] + w[x+y] + w[x+2y] + ... + w[x + py],其中x+py <= n

思路:

首先,我们可以知道,大于sqrt(n)的询问,我们暴力,那么复杂度就是sqrt(n),所以假设所有的询问都是>=sqrt(n)的,那么我们的复杂度最坏为n*sqrt(n)

假设如果所有的询问都不相同,如果单纯暴力的话,复杂度为n/1 + n/2 + n/3 + n/4 + n/5 + ... + n/n-1 + n/n,所以为n*ln(n)(证明自己去证吧)

那么,如果存在许多的询问的长度都相等,那么我们就分治的思想去解决。

对于>=sqrt(n),我们暴力即可

对于<sqrt(n)的,我们把它全部都放到一个vector里面去,定义vector<pair<int, int>> ve[i]来储存目前y=i的东西,然后就很好做了

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

  1. ///bicat
  2. //看看会不会爆int!数组会不会少了一维!
  3. //取物问题一定要小心先手胜利的条件
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. #pragma comment(linker,"/STACK:102400000,102400000")
  7. #define LL long long
  8. #define ALL(a) a.begin(), a.end()
  9. #define pb push_back
  10. #define mk make_pair
  11. #define fi first
  12. #define se second
  13. #define haha printf("haha\n")
  14. const int maxn = 3e5 + ;
  15. /*
  16. 首先,对于大于sqrt(n)的,我们直接暴力
  17. 对于小于的,我们根据步数的大小,放到vector里面去,每次都进行暴力即可
  18. */
  19. LL w[maxn], sum[maxn], ans[maxn];
  20. vector<pair<int, int> > ve[maxn];
  21. int n, q;
  22.  
  23. int main(){
  24. cin >> n;
  25. for (int i = ; i <= n; i++){
  26. scanf("%lld", w + i);
  27. }
  28. int block = sqrt(n) + ;
  29. cin >> q;
  30. for (int i = ; i <= q; i++){
  31. int x, y; scanf("%d%d", &x, &y);
  32. if (y >= block){
  33. for (int j = x; j <= n; j += y){
  34. ans[i] += w[j];
  35. }
  36. }
  37. else {
  38. ve[y].push_back(mk(i, x));
  39. }
  40. }
  41. for (int i = ; i < block; i++){
  42. if (ve[i].size()){
  43. memset(sum, , sizeof(sum));
  44. for (int j = n; j >= ; j--){
  45. sum[j] = sum[j + i] + w[j];
  46. }
  47. for (int j = ; j < ve[i].size(); j++){
  48. int id = ve[i][j].first, pos = ve[i][j].second;
  49. ans[id] = sum[pos];
  50. }
  51. }
  52. }
  53. for (int i = ; i <= q; i++)
  54. printf("%lld\n", ans[i]);
  55. return ;
  56. }

分治思想 特别常用 Codeforces Beta Round #80 (Div. 1 Only) D的更多相关文章

  1. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  2. 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 ...

  3. 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 ...

  4. Codeforces Beta Round #69 (Div. 2 Only)

    Codeforces Beta Round #69 (Div. 2 Only) http://codeforces.com/contest/80 A #include<bits/stdc++.h ...

  5. Codeforces Beta Round #25 (Div. 2 Only)

    Codeforces Beta Round #25 (Div. 2 Only) http://codeforces.com/contest/25 A #include<bits/stdc++.h ...

  6. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  7. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  8. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  9. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

随机推荐

  1. 20172332『Java程序设计』课程结对编程练习_四则运算第二周阶段总结

    20172313『Java程序设计』课程结对编程练习_四则运算第二周阶段总结 小组成员 20172326康皓越 20172313余坤澎 20172332于欣月 小组编程照片 设计思路 设计一个生成符号 ...

  2. 按照Right-BICEP要求设计的测试用例

    测试用例: 测试方法:Right-BICEP 测试要求: Right-结果是否正确? B-是否所有的边界条件都是正确的? P-是否满足性能要求? 题目是否有重复? 数量是否可定制? 数值范围是否可定制 ...

  3. lintcode-512-解码方法

    512-解码方法 有一个消息包含A-Z通过以下规则编码 'A' -> 1 'B' -> 2 ... 'Z' -> 26 现在给你一个加密过后的消息,问有几种解码的方式 样例 给你的消 ...

  4. bash编程2

    bash基础编程 前言:条件测试语法有两种书写模式,一种时[expression] ,另外一种是[[exprssion]] ,为了在书写条件测试的过程中,不让大家将两种格式互相混淆,那么在这里只讲一种 ...

  5. vsftpd:非常安全的ftp服务端程序

    主程序:/usr/sbin/vsftpd 主配置文件:/etc/vsftpd/vsftpd.conf CentOS 6  /etc/rc.d/init.d/vsftpd chkconfig vsftp ...

  6. CentOS系统iptables防火墙的启动、停止以及开启关闭端口的操作

    CentOS 配置防火墙操作实例(启.停.开.闭端口):注:防火墙的基本操作命令:查询防火墙状态:[root@localhost ~]# service   iptables status停止防火墙: ...

  7. 为何php curl post模式发送数据速度变慢了?我来说说原因

    事例: 今天要向一台服务器上传文件,原版是curl的get模式,现在改用了post模式,按照原本的思想,代码如下 <?php $post['c'] = 'config'; $post['t'] ...

  8. Netty系列学习

    Netty系列之Netty高性能之道 Netty系列之Netty线程模型 Netty系列之Netty 服务端创建 Netty系列之Netty编解码框架分析 Netty系列之Netty百万级推送服务设计 ...

  9. Xmind8破解,以及相关的流程和破解包

    一.下载XMindCrack.jar文件:(传的貌似被屏蔽了:如果需要请留下邮箱,抽空会发给你) 百度云 ,里面破解文件,安装包都给了,但Xmind安装包不一定是最新的,有需求的可自行去官网下载 . ...

  10. C++解析(14):静态成员变量与静态成员函数

    0.目录 1.静态成员变量 2.静态成员函数 3.小结 1.静态成员变量 成员变量的回顾: 通过对象名能够访问public成员变量 每个对象的成员变量都是专属的 成员变量不能在对象之间共享 新的需求: ...