先对b从小到大sort,判断b是不是比sqrt(n)大,是的话就直接暴力,不是的话就用dp维护一下

dp【i】表示以nb为等差,i为起点的答案,可以节省nb相同的情况

#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; int n,m;
ll ans[N],dp[N],w[N];
struct node{
int a,b,id;
}c[N];
bool comp(node x,node y)
{
if(x.b!=y.b)return x.b<y.b;
return x.a<y.a;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%I64d",&w[i]);
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&c[i].a,&c[i].b);
c[i].id=i;
}
sort(c+,c+m+,comp);
int nb=;
for(int i=;i<=m;i++)
{
if(c[i].b>sqrt(0.5+n))
{
for(int j=c[i].a;j<=n;j+=c[i].b)
ans[c[i].id]+=w[j];
}
else
{
if(c[i].b!=nb||dp[c[i].a]==)
{
for(int j=n;j>=;j--)
{
if(j+c[i].b<=n)dp[j]=dp[j+c[i].b]+w[j];
else dp[j]=w[j];
}
}
ans[c[i].id]=dp[c[i].a];
nb=c[i].b;
}
}
for(int i=;i<=m;i++)
printf("%I64d\n",ans[i]);
return ;
}
/****************
5
3 2 4 5 6
8
4 2
3 1
3 5
3 4
3 5
5 5
4 4
5 3
****************/

CodeForces 103D Time to Raid Cowavans 分块+dp的更多相关文章

  1. CodeForces 103D Time to Raid Cowavans 询问分块

    Time to Raid Cowavans 题意: 询问 下标满足 a + b * k 的和是多少. 题解: 将询问分块. 将b >= blo直接算出答案. 否则存下来. 存下来之后,对于每个b ...

  2. CodeForces - 103D Time to Raid Cowavans

    Discription As you know, the most intelligent beings on the Earth are, of course, cows. This conclus ...

  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. D. Time to Raid Cowavans 分块暴力,感觉关键在dp

    http://codeforces.com/contest/103/problem/D 对于b大于 sqrt(n)的,暴力处理的话,那么算出每个的复杂度是sqrt(n),就是把n分成了sqrt(n)段 ...

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

  6. Codeforces Round #278 (Div. 1) D - Conveyor Belts 分块+dp

    D - Conveyor Belts 思路:分块dp, 对于修改将对应的块再dp一次. #include<bits/stdc++.h> #define LL long long #defi ...

  7. CodeForces 103 D Time to Raid Cowavans

    Time to Raid Cowavans 题意:一共有n头牛, 每头牛有一个重量,m次询问, 每次询问有a,b 求出 a,a+b,a+2b的牛的重量和. 题解:对于m次询问,b>sqrt(n) ...

  8. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  9. Codeforces103D - Time to Raid Cowavans

    Portal Description 给出长度为\(n(n\leq3\times10^5)\)的序列\(\{a_n\}\),进行\(q(q\leq3\times10^5)\)次询问:给出\(x,y\) ...

随机推荐

  1. 涉及到复制和二进制日志中的选项和变量-Replication and Binary Logging Options and Variables

    在搭建复制中,有些参数需要我们留意,在这里罗列出来,供大家参考一下,以GTID为基础 --server-id server-id:这是一个全局的可动态调整的变量,取值范围为0-4294967295,也 ...

  2. Linux CentOS使用yum安装Docker

    Docker支持以下的CentOS版本: 目前,CentOS仅发行版本中的内核支持Docker. Docker运行在CentOS7上,要求系统为64位.系统内核版本为3.10以上. Docker运行在 ...

  3. Activity重要函数

    一.onConfigurationChanged 与 android:configChanges Lists configuration changes that the activity will ...

  4. Js前台页面搜索

    $("#filter").on("keyup",function(){$(".aimed_list").hide().filter(&quo ...

  5. End to End 端到端

    在DeepLearning的文章中有看到end2end一次,作者们似乎都比较喜欢这个end2end learning的方式.那么到底啥是end2end? 找了一下相关论文,没找到专门讲这个概念的,看来 ...

  6. 于win2008R2虽然激活,但是一个小时之后就会自动强制关机的问题

    写一个批处理文件: taskkill /f /im wlms.exeping -n  4 127.0.0.1shutdown -a 用记事本写下以上的命令,另存为.bat 批处理文件.双击运行,即可. ...

  7. Java 如何读取resources

    Sample in Github 1.一般使用Maven创建Java工程,代码文件在src/main/java文件夹中,资源文件在src/main/resources文件夹中,Java代码为什么可以读 ...

  8. C#中字符串的内存分配与驻留池

    完全引用http://www.cnblogs.com/instance/archive/2011/05/24/2056091.html 驻留池:是一张记录了所有在代码中使用字面量声明的字符串实例的引用 ...

  9. LeetCode:路径总和【112】

    LeetCode:路径总和[112] 题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例 ...

  10. Python学习进程(8)字符串內建函数

        Python字符串內建函数实现了string模块的大部分方法,并包括了对Unicode编码方式的支持.     (1)capitalize(): 将字符串的第一个字母变成大写,其他字母变小写. ...