CodeForces 103D Time to Raid Cowavans 分块+dp
先对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的更多相关文章
- CodeForces 103D Time to Raid Cowavans 询问分块
Time to Raid Cowavans 题意: 询问 下标满足 a + b * k 的和是多少. 题解: 将询问分块. 将b >= blo直接算出答案. 否则存下来. 存下来之后,对于每个b ...
- CodeForces - 103D Time to Raid Cowavans
Discription As you know, the most intelligent beings on the Earth are, of course, cows. This conclus ...
- 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 ...
- D. Time to Raid Cowavans 分块暴力,感觉关键在dp
http://codeforces.com/contest/103/problem/D 对于b大于 sqrt(n)的,暴力处理的话,那么算出每个的复杂度是sqrt(n),就是把n分成了sqrt(n)段 ...
- 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 Round #278 (Div. 1) D - Conveyor Belts 分块+dp
D - Conveyor Belts 思路:分块dp, 对于修改将对应的块再dp一次. #include<bits/stdc++.h> #define LL long long #defi ...
- CodeForces 103 D Time to Raid Cowavans
Time to Raid Cowavans 题意:一共有n头牛, 每头牛有一个重量,m次询问, 每次询问有a,b 求出 a,a+b,a+2b的牛的重量和. 题解:对于m次询问,b>sqrt(n) ...
- Codeforces 219D. Choosing Capital for Treeland (树dp)
题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...
- Codeforces103D - Time to Raid Cowavans
Portal Description 给出长度为\(n(n\leq3\times10^5)\)的序列\(\{a_n\}\),进行\(q(q\leq3\times10^5)\)次询问:给出\(x,y\) ...
随机推荐
- jQuery 属性操作
1 css操作 2 文本操作 3 属性操作 4 位置 5 尺寸 1.css操作 addClass();// 添加指定的CSS类名. removeClass();// 移除指定的CSS类名. hasCl ...
- 磁盘对齐-windows篇
什么是磁盘分区对齐(Disk Alignment.Partition Alignment): Windows的磁盘有一种结构叫做Master Boot Record(MBR), 它的默认大小为63个B ...
- iOS Xcode 8 快捷键 (注释 失效 处理)
在升级后,好用的VVDocumment 插件不能用了.(但是苹果这次内置了好多好用的插件,也有自己的注释功能了 AddDocumentation) 上网上有查到 传播很广泛的一条信息 "这个 ...
- 面对 to B 业务该如何构建研发管理体系?
未来离我们越来越近,而过去并未走远,我们发现科技公司2B业务兴起,腾讯认为互联网下半场属于产业互联网,需要进行一次重要的战略升级.它们在国庆节最后一天进行新一轮组织架构调整,最亮眼的就是新成立云与智慧 ...
- css系列(5)css的运用(一)
从本节开始介绍css配合html可以达到的一些效果. (1)导航栏: <html> <head> <title>示例5.1</title> ...
- 015_[小插曲]看黄老师《炼数成金Hadoop应用开发实战案例》笔记
1.大数据金字塔结构 Data Source-->Data Warehouses/Data Marts-->data exploration-->Data Mining-->D ...
- 【转载】wget 命令用法详解
wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能和特点:(1)支持断点下传功能:这一点,也是网络蚂蚁和Fl ...
- 断点续传JAVA实现
支持H5 Video标签播放,迅雷下载 /** * 断点续传工具 * @author lxycx_xc * 时间:2017年11月30日 */ public class BreakpointResum ...
- CSS3手风琴下拉菜单
在线演示 本地下载
- php面向对象之克隆对象
在前面的PHP面向对象之对象和引用,我们试图以"$b=$a"的方式复制对象以传递对象的值(内容),结果却是传递对象的地址,在结尾为了解决复制对象这个问题,提到了克隆的方法.接下来讲 ...