【HDU 5381】 The sum of gcd (子区间的xx和,离线)
【题目】
The sum of gcd
Problem DescriptionYou have an array A,the length of A is n
Let f(l,r)=∑ri=l∑rj=igcd(ai,ai+1....aj)InputThere are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
First line has one integers n
Second line has n integers Ai
Third line has one integers Q,the number of questions
Next there are Q lines,each line has two integers l,r
1≤T≤3
1≤n,Q≤104
1≤ai≤109
1≤l<r≤nOutputFor each question,you need to print f(l,r)Sample Input2
5
1 2 3 4 5
3
1 3
2 3
1 4
4
4 2 6 9
3
1 3
2 4
2 3Sample Output9
6
16
18
23
10AuthorSXYZSource
You have an array A,the length of A is n
Let f(l,r)=∑r i=l ∑r j=i gcd(ai,ai+1....aj) (n,m<=1000,ai<=1000000000)
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
#define Maxn 10010
#define LL long long LL a[Maxn]; struct hp
{
LL l,r,id;
LL ans;
}q[Maxn]; bool cmp(hp x,hp y) {return x.r<y.r;}
bool cmp2(hp x,hp y) {return x.id<y.id;} LL lt[Maxn],g[Maxn],last;
LL n,m; LL gcd(LL a,LL b)
{
if(b==) return a;
return gcd(b,a%b);
} LL c1[Maxn],c2[Maxn];
void add(LL l,LL r,LL y)
{
// printf("add %d %d %d\n",l,r,y);
for(LL i=l;i<=n;i+=i&(-i))
c1[i]+=y,c2[i]+=l*y;
r++;
for(LL i=r;i<=n;i+=i&(-i))
c1[i]-=y,c2[i]-=r*y;
} LL query(LL l,LL r)
{
// printf("ask %d %d ",l,r);
LL ans=;
for(LL i=r;i>=;i-=i&(-i))
ans+=c1[i]*(r+)-c2[i];
l--;
for(LL i=l;i>=;i-=i&(-i))
ans-=c1[i]*(l+)-c2[i];
// printf("%d\n",ans);
return ans;
} void ffind(LL r)
{
if(r==)
{
add(r,r,a[r]);
last=;lt[r]=;g[r]=a[r];
return;
}
lt[r]=last,last=r,g[r]=a[r];
for(LL i=last;lt[i];)
{
g[lt[i]]=gcd(g[lt[i]],a[r]);
if(g[lt[i]]==g[i])
{
lt[i]=lt[lt[i]];
}
else i=lt[i];
}
for(LL i=last;i;i=lt[i])
{
add(lt[i]+,i,g[i]);
}
} int main()
{
LL T;
scanf("%lld",&T);
while(T--)
{
scanf("%lld",&n);
for(LL i=;i<=n;i++) scanf("%lld",&a[i]);
scanf("%lld",&m);
for(LL i=;i<=m;i++)
{
scanf("%lld%lld",&q[i].l,&q[i].r);
q[i].id=i;
}
sort(q+,q++m,cmp);
memset(c1,,sizeof(c1));
memset(c2,,sizeof(c2));
LL now=;
for(LL i=;i<=m;i++)
{
while(now<q[i].r)
{
now++;
ffind(now);
}
q[i].ans=query(q[i].l,q[i].r);
}
sort(q+,q++m,cmp2);
for(LL i=;i<=m;i++) printf("%lld\n",q[i].ans);
}
return ;
}
2016-11-10 21:52:36
伟大的子区间离线做法,log段!!!!!
好多东东都是log段的说。。。
啊啊啊,我觉得我智障!!
想了好久子区间求和怎么破。。。
就记录一个lsm,rsm,满足结合律啊。。。。。
异或和sm的也是可以的!!!
每天智障24小时。。
【HDU 5381】 The sum of gcd (子区间的xx和,离线)的更多相关文章
- hdu 5381 The sum of gcd(线段树+gcd)
题目链接:hdu 5381 The sum of gcd 将查询离线处理,依照r排序,然后从左向右处理每一个A[i],碰到查询时处理.用线段树维护.每一个节点表示从[l,i]中以l为起始的区间gcd总 ...
- hdu 5381 The sum of gcd 莫队+预处理
The sum of gcd Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) P ...
- hdu 5381 The sum of gcd 2015多校联合训练赛#8莫队算法
The sum of gcd Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) T ...
- 2015 Multi-University Training Contest 8 hdu 5381 The sum of gcd
The sum of gcd Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- hdu 5381 The sum of gcd
知道对于一个数列,如果以x为左(右)端点,往右走,则最多会有log(a[x])个不同的gcd,并且有递减性 所以会分成log段,每一段的gcd相同 那我们可以预处理出对于每一个位置,以这个位置为左端点 ...
- HDU 5381 The sum of gcd (技巧,莫队算法)
题意:有一个含n个元素的序列,接下来有q个询问区间,对每个询问区间输出其 f(L,R) 值. 思路: 天真单纯地以为是道超级水题,不管多少个询问,计算量顶多就是O(n2) ,就是暴力穷举每个区间,再直 ...
- HDU - 4676 :Sum Of Gcd (莫队&区间gcd公式)
Given you a sequence of number a 1, a 2, ..., a n, which is a permutation of 1...n. You need to answ ...
- HDOJ 5381 The sum of gcd 莫队算法
大神题解: http://blog.csdn.net/u014800748/article/details/47680899 The sum of gcd Time Limit: 2000/1000 ...
- hdu 4676 Sum Of Gcd 莫队+phi反演
Sum Of Gcd 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4676 Description Given you a sequence of ...
随机推荐
- HttpClient 通过域名访问请求接口出现java.net.UnknownHostException解决方法
在项目中,有一个功能需要请求另外一个项目的接口来获取数据.该项目接口都是通过域名请求访问.每当调用到一定阶段后都会出现未知域名,导致请求数据失败.以下是错误内容 java.net.UnknownHos ...
- 解决在mybatis中使用CTE进行oracle查询数据类型为long的字段出现流关闭问题
今天把notice表中的content字段改为long字段后,含有该字段的使用CTE的查询语句报错了.提示一下错误 ### Cause: java.sql.SQLException: 流已被关闭 ; ...
- PHP 获取当前日期的上个月的日期
获取当前日期的上个月的日期 <?php /** *参考有: *http://www.oschina.net/code/snippet_96541_4015 *http://stackoverfl ...
- 第七篇:web之前端之ajax
前端之ajax 前端之ajax 本节内容 ajax介绍 原生js实现ajax jquery实现ajax json 跨域请求 1. ajax介绍 AJAX(Asynchronous Javascri ...
- 2014年的Google I/O app设计中的材料设计-渣渣的翻译
又是一篇翻译,用了三个多小时.http://android-developers.blogspot.co.id/2014/08/material-design-in-2014-google-io-ap ...
- LIBPNG使用小结(二)
之前写的LIBPNG库学习小结介绍了怎么样自定义LIBPNG库的write.read.flush函数,而不使用LIBPNG库提供的默认函数. 上一篇讲述的都是在单线程的情况下,今天将程序升级,放在多线 ...
- 学习笔记_过滤器详细(过滤器JavaWeb三大组件之一)
过滤器详细 1 过滤器的生命周期 我们已经学习过Servlet的生命周期,那么Filter的生命周期也就没有什么难度了! (l) init(FilterConfig):在服务器启动时会创建Filte ...
- 学习笔记_过滤器概述(过滤器JavaWeb三大组件之一)
过滤器Filter Filter和Lister是Servlet规范里的两个高级特性.不同于Servlet,它们不用于处理客户端请求,只用于对request.response进行修改或者对context ...
- NFS文件共享系统
1.NFS介绍 NFS是Network File System的缩写,主要功能是通过网络让不同的机器系统之间可以彼此共享文件或目录.NFS服务器可以允许NFS客户端将远端NFS服务端的共享目录挂载到本 ...
- 一个fibonacci数列简单求和的问题
前段时间老师在讲函数调用的时候,用Fibonacci数列来演示了一下,因为以前没怎么接触过Fibonacci,所以当时很懵. 当时让求的是Fibonacci数列中,第N位值为多少,当时老师写的是: 之 ...