Portal

Description

给出长度为\(n(n\leq3\times10^5)\)的序列\(\{a_n\}\),进行\(q(q\leq3\times10^5)\)次询问:给出\(x,y\),求\(\sum_{i=x,i+=y}^n a_i\)。

Solution

将询问离线,按\(y\)排序。

对于\(y<\sqrt n\),对于每个\(y\)计算sum[i]表示\(x=i\)时的答案。计算sum[i]复杂度为\(O(n)\),每次询问为\(O(1)\),对于\(\sqrt n\)个\(y\)总复杂度为\(O(n\sqrt n)\)。

对于\(y>\sqrt n\),直接循环计算\(\sum_{i=x,i+=y}^n a_i\)。每次询问复杂度为\(O(\sqrt n)\),总复杂度为\(O(m\sqrt n)\)。

时间复杂度约为\(O(n\sqrt n)。\)

Code

//Time to Raid Cowavans
#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long lint;
inline char gc()
{
static char now[1<<16],*S,*T;
if(S==T) {T=(S=now)+fread(now,1,1<<16,stdin); if(S==T) return EOF;}
return *S++;
}
inline int read()
{
int x=0; char ch=gc();
while(ch<'0'||'9'<ch) ch=gc();
while('0'<=ch&&ch<='9') x=x*10+ch-'0',ch=gc();
return x;
}
int const N=3e5+1000;
int n,m,n0; lint a[N];
struct query{int a,b,id;} q[N];
bool cmpB(query x,query y) {return x.b<y.b;}
lint sum[N],ans[N];
int main()
{
n=read(); n0=sqrt(n);
for(int i=1;i<=n;i++) a[i]=read();
m=read();
for(int i=1;i<=m;i++) q[i].a=read(),q[i].b=read(),q[i].id=i;
sort(q+1,q+m+1,cmpB);
for(int t=1;t<=m;t++)
{
lint res=0;
if(q[t].b<=n0&&q[t].b!=q[t-1].b) for(int i=n;i>=1;i--) sum[i]=a[i]+sum[i+q[t].b];
if(q[t].b<=n0) res=sum[q[t].a];
else for(int i=q[t].a;i<=n;i+=q[t].b) res+=a[i];
ans[q[t].id]=res;
}
for(int i=1;i<=m;i++) printf("%lld\n",ans[i]);
return 0;
}

P.S.

又得开long long...

Codeforces103D - Time to Raid Cowavans的更多相关文章

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

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

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

  3. CodeForces 103 D Time to Raid Cowavans

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

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

  5. CodeForces - 103D Time to Raid Cowavans

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

  6. 题解【CF103D Time to Raid Cowavans】

    Description 给一个序列 \(a\) ,\(m\) 次询问,每次询问给出 \(t, k\) .求 \(a_t + a_{t+k}+a_{t+2k}+\cdots+a_{t+pk}\) 其中 ...

  7. 【CF103D】Time to Raid Cowavans(分块)

    题意: 思路:院赛防AK题,然而还没来得及做就被数据出锅的题坑了…… #include<cstdio> #include<cstring> #include<string ...

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

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

  9. D. Time to Raid Cowavans 分块暴力,感觉关键在dp

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

随机推荐

  1. 译-BSA NSH Command介绍

    BSA NSH Command全称BMC BladeLogic Network Shell Command,是基于ZSH的shell. 1 说明 NSH命令行(全称Network  Shell,又称为 ...

  2. Linux指令--touch

    原文出处:http://www.cnblogs.com/peida/archive/2012/10/30/2745714.html linux的touch命令不常用,一般在使用make的时候可能会用到 ...

  3. Win2003 设置远程连接限制数

    在开发过程中,很多同事需要连接到一台Win2003服务器,但是连接人数超过了10个,就连接不上了.想设置一下连接限制数,可以如下操作: 1:在运行里面输入gpedit.msc后,弹出"本地计 ...

  4. jsp页面取值

    一般就用el表达式 ${recordList[4].baseRate8.split("/")[0] } <s:date name="recordList[#id]. ...

  5. PHP date函数详解

    在页面的最前页加上date_default_timezone_set(PRC);   /*把时间调到北京时间,php5默认为格林威治标准时间*/date ()a:   "am"或是 ...

  6. CSS中的选择器之html选择器和伪类选择器

    1.html选择器(标签选择器) 基本语法: html标签名称{ 属性名:属性值; 属性名:属性值; } 继续在上面的代码中做修改,实例代码: <!DOCTYPE html> <ht ...

  7. ABP官方文档翻译 6.1.3 异常处理

    处理异常 介绍 启用错误处理 Non-Ajax请求 显示异常 UserFriendlyException Error模型 AJAX请求 异常事件 介绍 此文档是与ASP.NET MVC和Web API ...

  8. ZooKeeper安装(Windows)

    概述 ZooKeeper是Hadoop的正式子项目,它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.名字服务.分布式同步.组服务等.ZooKeeper的目标就是封装好复杂易出错的关 ...

  9. Hadoop学习笔记三

    一.设置HDFS不进行权限检查 默认的HDFS上的文件类似于Linux中的文件,是有权限的.例如test用户创建的文件,root用户如果没有写权限,则不能进行删除. 有2种办法进行修改,修改文件的权限 ...

  10. MySQL二进制日志binlog简单使用

    MySQL数据库进行了哪些CUD操作,通过binlog二进制文件可以查看.binlog不仅可以记录CUD的操作(select操作不包括在内),还是主从备份的基础.主库的操作记录成binlog文件,定期 ...