(分块暴力)Time to Raid Cowavans CodeForces - 103D
题意
给你一段长度为n(1 ≤ n ≤ 3·1e5)的序列,m (1 ≤ p ≤ 3·1e5)个询问,每次询问a,a+b,a+2b+...<=n的和
思路
一开始一直想也想不到怎么分,去维护哪些信息,看了题解才知道 其实分块不仅仅可以将一列序列分块,还可以将数据进行分块,下面讨论具体做法
首先这道题不是在线询问,可以离线做,先读入所有的询问,将询问从小到大排序
①当b<√n时,对于每一个b我们可以预处理出这样的一个数组sum[i],就是以i为起点间隔为b的序列和(可以用一个简单的dp求出来),然后O(1)查询,这么做的好处就是如果不同的询问a不同,b相同,经过排序我们就可以直接使用这个sum数组,时间复杂度为O(n√n)。
②当b≥√n时,直接暴力求和,时间复杂度为O(m√n)
所以总时间复杂度为O((m+n)√n)
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=3e5+;
typedef long long ll;
ll a[maxn];
struct node{
int a,b,id;
bool operator<(const node &s) const
{
return b<s.b;
}
}b[maxn];
ll ans[maxn];
ll sum_b[maxn];
int main()
{
int n,m;
scanf("%d",&n);
int bl=sqrt(n);
for(int i=;i<=n;i++)
scanf("%I64d",&a[i]);
scanf("%d",&m);
for(int i=;i<=m;i++){
b[i].id=i;
scanf("%d%d",&b[i].a,&b[i].b);
}
sort(b+,b++m);
b[].b=;
for(int i=;i<=m;i++){
if(b[i].b>=bl){
ans[b[i].id]=;
for(int k=b[i].a;k<=n;k+=b[i].b)
ans[b[i].id]+=a[k];
}
else{
if(b[i].b==b[i-].b){
ans[b[i].id]=sum_b[b[i].a];
}
else{
for(int j=n;j>=;j--)
if(j+b[i].b>n)
sum_b[j]=a[j];
else
sum_b[j]=sum_b[j+b[i].b]+a[j];
ans[b[i].id]=sum_b[b[i].a];
}
}
}
for(int i=;i<=m;i++)
printf("%I64d\n",ans[i]);
return ;
}
(分块暴力)Time to Raid Cowavans CodeForces - 103D的更多相关文章
- CodeForces 103D Time to Raid Cowavans 询问分块
Time to Raid Cowavans 题意: 询问 下标满足 a + b * k 的和是多少. 题解: 将询问分块. 将b >= blo直接算出答案. 否则存下来. 存下来之后,对于每个b ...
- 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 #13 E. Holes 分块暴力
E. Holes Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/13/problem/E Des ...
- Codeforces 1290D - Coffee Varieties(分块暴力+完全图的链覆盖)
Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 发现自己交互题烂得跟 s ...
- Codeforces#86D Powerful array(分块暴力)
Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...
- CodeForces 103 D Time to Raid Cowavans
Time to Raid Cowavans 题意:一共有n头牛, 每头牛有一个重量,m次询问, 每次询问有a,b 求出 a,a+b,a+2b的牛的重量和. 题解:对于m次询问,b>sqrt(n) ...
- Codeforces103D - Time to Raid Cowavans
Portal Description 给出长度为\(n(n\leq3\times10^5)\)的序列\(\{a_n\}\),进行\(q(q\leq3\times10^5)\)次询问:给出\(x,y\) ...
- CodeForces 103D Time to Raid Cowavans 分块+dp
先对b从小到大sort,判断b是不是比sqrt(n)大,是的话就直接暴力,不是的话就用dp维护一下 dp[i]表示以nb为等差,i为起点的答案,可以节省nb相同的情况 #include<bits ...
- 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 ...
随机推荐
- 51nod 1287加农炮
1287 加农炮 题目来源: Codility 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 一个长度为M的正整数数组A,表示从左向右的地形高度.测试一种加农炮 ...
- Apache工具集
commons-collections-3.2.jar 转自:http://www.open-open.com/lib/view/open1384437456993.html
- phpcms 增加备案号、联系方式等字段
准备好记事本或者dreamweaver或者其它文本编辑器 打开\phpcms\languages\zh-cn\admin.lang.php PHPCMS的中文语言定义文件. 查找“site_manag ...
- Java虚拟机参数,增加虚拟机最大内存,在/etc/profile增加如下: export JAVA_OPTS="-Xms9g -Xmx9g"
一.运行class文件 执行带main方法的class文件,Java虚拟机命令参数行为: java <CLASS文件名> 注意:CLASS文件名不要带文件后缀.class 例如: java ...
- SPOJ - PHRASES Relevant Phrases of Annihilation (后缀数组)
You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages ...
- MySQL Server逻辑架构
1.MySQL基础 MySQL是一个开放源代码的关系数据库管理系统.原开发者为瑞典的MySQL AB公司,最早是在2001年MySQL3.23进入到管理员的视野并在之后获得广泛的应用. 2008年My ...
- 周志华《机器学习》高清电子书pdf分享
周志华<机器学习>高清电子书pdf下载地址 下载地址1:https://545c.com/file/20525574-415455837 下载地址2: https://pan.baidu. ...
- 优雅的使用BeanUtils对List集合的操作
摘要 在业务员流程的时候,我们在Entity.Bo.Vo层数据间可能经常转换数据,Entity对应的是持久层数据结构(一般是数据库表的映射模型).Bo对应的是业务层操作的数据结构.Vo就是Contro ...
- 洛谷p1345---最小割的奇妙运用
让你去掉最少的点,使得c1和c2变得不连通,你有办法吗??? 这是最小割呀!!! 网络流的最小割去掉的是边,构造边的顶点的唯一关系就好了!!! 需要注意一点 #include<iostream& ...
- 日志管理-log4j与slf4j的使用
一.概述 1.log4j: Log4j是Apache的一个开源项目,通过使用Log4j,我们可以控制日志信息输送的目的地是控制台.文件.GUI组件,甚至是套接口服务器.NT的事件记录器.UNIX Sy ...