题目链接:http://codeforces.com/problemset/problem/103/D

题意:给定一个长度为n的序列。然后q个询问。每个询问为(a,b),表示从序列第a项开始每b项的加和。

思路:2014集训队论文中的<<根号算法——不只是分块>>中提到这题。 传统的数据结构比较擅长处理连续区间的询问。但是不擅长处理间隔位置的询问。考虑到分块。 对于b>sqrt(n)的询问。我们暴力计算。可以发现b越大我们扫描的位置就会越小。最大扫描次数为O(n/sqrt(n))。然后对于b<sqrt(n)的。我们离线处理。以b为关键字来分组。 询问中b相同的为一组。 然后用预存部分和来计算。 这样整体的时间复杂度为O(n*sqrt(n)).

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include<time.h>
#include<vector>
#include<iostream>
#include<map>
using namespace std;
typedef long long int LL;
const int MAXN = * + ;
int block, n, q, a[MAXN];
LL ans[MAXN],sum[MAXN];
struct Query{ int id, pos; Query(int _id = , int _pos = ) :id(_id), pos(_pos){} };
vector<Query>D[MAXN];
void init(){
block = (int)sqrt(n + 0.5);
for (int i = ; i < MAXN; i++){
if (D[i].empty()){ continue; }
if (i > block){
for (int j = ; j < D[i].size(); j++){
LL res = ;
for (int k = D[i][j].pos; k <= n; k += i){
res += a[k];
}
ans[D[i][j].id] = res;
}
}
else{
memset(sum, , sizeof(sum));
for (int j = n; j > ; j--){
sum[j] = a[j]+(j+i<=n?sum[i+j]:);
}
for (int j = ; j < D[i].size(); j++){
ans[D[i][j].id] = sum[D[i][j].pos];
}
}
D[i].clear();
}
}
int main(){
//#ifdef kirito
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
//#endif
// int start = clock();
while (~scanf("%d", &n)){
for (int i = ; i <= n; i++){
scanf("%d", &a[i]);
}
scanf("%d", &q);
for (int i = ; i <= q; i++){
int pos, d; scanf("%d%d", &pos, &d);
D[d].push_back(Query(i, pos)); //相同d的为一组
}
init();
for (int i = ; i <= q; i++){
printf("%lld\n", ans[i]);
}
}
//#ifdef LOCAL_TIME
// cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
return ;
}

CodeForces 103D 分块处理的更多相关文章

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

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

  2. (分块暴力)Time to Raid Cowavans CodeForces - 103D

    题意 给你一段长度为n(1 ≤ n ≤ 3·1e5)的序列,m (1 ≤ p ≤ 3·1e5)个询问,每次询问a,a+b,a+2b+...<=n的和 思路 一开始一直想也想不到怎么分,去维护哪些 ...

  3. CodeForces 103D Time to Raid Cowavans 分块+dp

    先对b从小到大sort,判断b是不是比sqrt(n)大,是的话就直接暴力,不是的话就用dp维护一下 dp[i]表示以nb为等差,i为起点的答案,可以节省nb相同的情况 #include<bits ...

  4. CodeForces 444C 分块

    题目链接:http://codeforces.com/problemset/problem/444/C 题意:给定一个长度为n的序列a[].起初a[i]=i,然后还有一个色度的序列b[],起初b[i] ...

  5. CodeForces 455D 分块

    题目链接:http://codeforces.com/problemset/problem/455/D 题意:给定一个长度为n的序列a[]. m次操作.共有两种操作 1 l r:将序列的a[l].a[ ...

  6. CodeForces 551E 分块

    题目链接:http://codeforces.com/problemset/problem/551/E 题意:给定一个长度为N的序列. 有2个操作 1 l r v:序列第l项到第r项加v(区间加), ...

  7. Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) Problem E (Codeforces 828E) - 分块

    Everyone knows that DNA strands consist of nucleotides. There are four types of nucleotides: "A ...

  8. CodeForces 13E 分块

    题目链接:http://codeforces.com/problemset/problem/13/E 题意:给定n个弹簧和每个弹簧初始的弹力a[].当球落在第i个位置.则球会被弹到i+a[i]的位置. ...

  9. Serega and Fun CodeForces - 455D (分块 或 splay)

    大意:给定n元素序列, 2种操作 将区间$[l,r]$循环右移1位 询问$[l,r]$中有多少个等于k的元素 现在给定q个操作, 输出操作2的询问结果, 强制在线 思路1: 分块 每个块内维护一个链表 ...

随机推荐

  1. 【Alpha版本】冲刺随笔汇总

    [Alpha版本]冲刺-Day1 [Alpha版本]冲刺-Day2 [Alpha版本]冲刺-Day3 [Alpha版本]冲刺-Day4 [Alpha版本]冲刺-Day5 [Alpha版本]冲刺-Day ...

  2. CMS系统的实现图

  3. table td 文字超出显示省略号

    .autocut {      width:250px;      overflow:hidden;      white-space:nowrap;      text-overflow:ellip ...

  4. 微信电脑版-微信for windows客户端发布

    12月份微信Windows版客户端1.0 Alpha推出,昨天微信for windows 1.0客户端(测试版)发布更新,超过三亿人使用的聊天应用,现在登录Windows桌面.你可以在Windows上 ...

  5. AJAX 请求区分 $_SERVER['HTTP_X_REQUESTED_WITH'] 小解

    关于这个内容,很多人都有所了解.但从我搜索的内容来看,他们只是略微看一下,根本不知道里面到底是什么情况. 受到很多模版代码的影响,大家都以为PHP有这样一个自定义变量:$_SERVER['HTTP_X ...

  6. 使用github pages, hexo搭建个人博客教程

    具体的原理性的东西就不说了直接上教程,怕等下自己忘了. 一. github 阶段 申请一个github 账号并成功登录进去. 创建一个名字为xxx.github.io的空项目. 二. hexo 阶段 ...

  7. 2016年11月28日--ADO.Net 增、删、改、查

    数据访问 对应命名空间:System.Data.SqlClient; SqlConnection:连接对象SqlCommand:命令对象SqlDataReader:读取器对象 CommandText: ...

  8. 进程互斥和fork

    自父进程继承 进程的资格(真实(real)/有效(effective)/已保存(saved) 用户号(UIDs)和组号(GIDs)) 环境(environment) 堆栈 内存 打开文件的描述符(注意 ...

  9. Bootstrap Fileupload 文件上传

    1.在jsp中引入css与js文件, <link href="${ctx}/plugins/fileup/css/fileinput.css" media="all ...

  10. .NET LINQ 投影运算

    投影运算      投影是指将对象转换为一种新形式的操作,该形式通常只包含那些将随后使用的属性. 通过使用投影,您可以构建依据每个对象生成的新类型. 您可以映射属性,并对该属性执行数学函数. 还可以在 ...