(分块暴力)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 ...
随机推荐
- linux 创建你的 /proc 文件
一旦你有一个定义好的 read_proc 函数, 你应当连接它到 /proc 层次中的一个入口项. 使用一个 creat_proc_read_entry 调用: struct proc_dir_ent ...
- WPF 元素裁剪 Clip 属性
本文介绍如何在 WPF 使用 Clip 裁剪元素 在 WPF 的 UIElement 提供了 Clip 属性,这个属性默认是空,但是如果设置了这个属性就会对元素进行裁剪 这个属性是一个 Geometr ...
- H3C系统调试的操作
- 2019-1-25-win10-uwp-禁用-ScrollViewer-交互
title author date CreateTime categories win10 uwp 禁用 ScrollViewer 交互 lindexi 2019-01-25 21:45:37 +08 ...
- vue 实例未加载完成显示 花括号解决方案
css [v - cloak] { display: none!important; } html < div id = "app" v-cloak >
- Laravel -- Excel 导入(import) (v2.1.0)
原博客地址 https://www.jianshu.com/p/7287ebdc77bb Install (安装) //> composer.json 中 require中添加如下: " ...
- 【2016常州一中夏令营Day5】
小 W 拼图[问题描述]小 W 和小 M 一起玩拼图游戏啦~小 M 给小 M 一张 N 个点的图,有 M 条可选无向边,每条边有一个甜蜜值,小 W 要选K 条边,使得任意两点间最多有一条路径,并且选择 ...
- CMD操纵Mysql命令大全
连接:mysql -h主机地址 -u用户名 -p用户密码 (注:u与root可以不用加空格,其它也一样)断开:exit (回车) 创建授权:grant select on 数据库.* to 用户名@登 ...
- C#使用SmtpClient发送邮件解决授权码配置问题
授权码,(新版邮箱在开启smtp权限设置时,会生成授权码)如果不做配置,客户端是不能发送邮件的 //指定邮箱账号和密码,需要注意的是,这个密码是你在QQ邮箱设置里开启服务的时候给你的那个授权码 Smt ...
- JMeter JMS测试计划
在本节中,我们将学习如何编写一个简单的测试计划来测试Java Messaging Service(JMS). 出于测试目的,我们使用Apache ActiveMQ.有各种JMS服务器,如:glassf ...