题目链接:

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

D. Time to Raid Cowavans

time limit per test:4 seconds
memory limit per test:70 megabytes
#### 问题描述
> 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.
#### 输入
> 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).
#### 输出
> 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**
> 4
> 2 3 5 7
> 3
> 1 3
> 2 3
> 2 2
>
> **sample output**
> 9
> 3
> 10

题意

给你n个数字的数组arr[],q个查询,对每个查询a,b,求数列和sigma(arr[a]+arr[a+b]+...)。

题解

这里用到了分块的思想:

如果所有的b都不相同你直接暴力也是可以做出来的(每次都按定义求和就好了),为什么呢,因为对于b>=sqrt(n)的,我们每次求和的时间<=sqrt(n),时间复杂度为q1 * sqrt(n);而对于小于等于sqrt(n)的b最多只有sqrt(n)个,我们每个查询求和最大的时间为o(n),时间复杂度为O(n * sqrt(n))。

如果很多b相同怎么办?首先,如果b>=sqrt(n),我们完全可以暴力做,所以我们只要考虑很多b相同并且b<sqrt(n)的情况。我们可以考虑离线处理,把相同的b都集中在一起,对所有相同的b我们只要O(n)处理一次步长为b的前缀和就可以了,这样,我们处理b<=sqrt(n)的情况就有能控制在O(n*sqrt(n))了。

代码

  1. #include<iostream>
  2. #include<cstring>
  3. #include<cstdio>
  4. #include<algorithm>
  5. #include<cmath>
  6. #define X first
  7. #define Y second
  8. #define mkp make_pair
  9. using namespace std;
  10. typedef __int64 LL;
  11. const int maxn = 3e5 + 10;
  12. struct Node {
  13. int id, a, b;
  14. LL ans;
  15. bool operator < (const Node& tmp) const {
  16. return id < tmp.id;
  17. }
  18. }que[maxn];
  19. bool cmp(const Node& n1, const Node& n2) {
  20. return n1.b < n2.b;
  21. }
  22. LL arr[maxn];
  23. int n, q;
  24. LL sumv[maxn];
  25. int len;
  26. LL solve(int a, int b) {
  27. if (b == len) {
  28. return sumv[a];
  29. }
  30. else {
  31. for (int i = n; i >= 1; i--) {
  32. if (i + b > n) {
  33. sumv[i] = arr[i];
  34. }
  35. else {
  36. sumv[i] = arr[i] + sumv[i + b];
  37. }
  38. }
  39. len = b;
  40. return sumv[a];
  41. }
  42. }
  43. int main() {
  44. scanf("%d", &n);
  45. for (int i = 1; i <= n; i++) {
  46. scanf("%I64d", &arr[i]);
  47. }
  48. scanf("%d", &q);
  49. for (int i = 0; i < q; i++) {
  50. int a, b;
  51. scanf("%d%d", &que[i].a, &que[i].b);
  52. que[i].id = i;
  53. }
  54. sort(que, que + q, cmp);
  55. len = -1;
  56. for (int i = 0; i < q; i++) {
  57. int a = que[i].a, b = que[i].b;
  58. LL ans = 0;
  59. if (b < sqrt(n)) {
  60. ans = solve(a, b);
  61. }
  62. else {
  63. for (int i = a; i <= n; i += b) {
  64. ans += arr[i];
  65. }
  66. }
  67. que[i].ans = ans;
  68. }
  69. sort(que, que + q);
  70. for (int i = 0; i < q; i++) {
  71. printf("%I64d\n", que[i].ans);
  72. }
  73. return 0;
  74. }

Codeforces Beta Round #80 (Div. 1 Only) D. Time to Raid Cowavans 离线+分块的更多相关文章

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

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

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

  3. 分治思想 特别常用 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 ...

  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 #83 (Div. 1 Only)题解【ABCD】

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

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

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

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

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

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

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

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

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

随机推荐

  1. [leetcode]_Maximum Depth of Binary Tree

    第三道树的题目,我还是不会,我擦,怎么递归算法还是不能很好理解.看来还得好好研究下递归算法. 题目:求一棵树的最大深度. 思路:递归地求取左子树最大深度 和 右子树最大深度,返回较大值即为 整棵树的 ...

  2. php qr生成二维码

    二维码就是用在平面上用特定的几何图形记录数据信息的,QR码是常见的一种二维码.推荐使用生成QR码的php类库PHP QR Code. 例子: <?php   ini_set('display_e ...

  3. C++十进制转换为二进制

    题目内容:将十进制整数转换成二进制数. 输入描述:输入数据中含有不多于50个的整数n(-231<n<231). 输出描述:对于每个n,以11位的宽度右对齐输入n值,然后输出“-->” ...

  4. delphi 基础之一 数据类型和基本语法

    1. 数据类型 特定类型 日期和时间 Delphi 也用实型数表示日期和时间数据.但为了更准确起见,Delphi 特别定义了TDateTime 数据类型,这是一个浮点类型,因为这个类型必须足够宽,使变 ...

  5. 计算两条直线的交点(C#)

    PS:从其他地方看到的源码是有问题的.下面是修正后的 /// <summary> /// 计算两条直线的交点 /// </summary> /// <param name ...

  6. python 类属性和实例属性

    class AAA(): aaa = 10 # 情形1 obj1 = AAA() obj2 = AAA() print obj1.aaa, obj2.aaa, AAA.aaa # 情形2 obj1.a ...

  7. sqlalchemy - day4

    query 此文算是自己的一个总结,不敢说对sqlalchemy有多精通,只能算是入门的总结,免得后面忘记了这些个基本的东西.数据库的增,删,改,查,前面已经介绍了session的增,删,改,现在来介 ...

  8. python 核心编程第二版 课后习题 第11章

    11-3 函数.在这个练习中,我们将实现 max()和 min()内建函数. (a) 写分别带两个元素返回一个较大和较小元素,简单的 max2()核 min2()函数.他们应该可以用任意的 pytho ...

  9. Android SharedPreferences使用以及原理详解

    SharedPreferences的使用非常简单,能够轻松的存放数据和读取数据.SharedPreferences只能保存简单类型的数据,例如,String.int等.一般会将复杂类型的数据转换成Ba ...

  10. hdu 1212 Big Number

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1212 Big Number Description As we know, Big Number is ...