【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 ...
随机推荐
- 什么是aop?
这个命题其实是讲了的,但是之前没有做,发现一些面试会问到,结合自己之前的学习经历.简单把这个问题描述一下. aop是跟oop相对应的一个概念.分别是aspect oriented programmin ...
- [转]Form Builder:app_field.clear_dependent_fields和APP_FIELD.set_dependent_field的用法
转自:http://www.cnblogs.com/toowang/p/3668070.html 可以调用APP_FIELD.clear_dependent_fields和APP_FIELD.set_ ...
- Eclipse SVN插件账号、密码修改
操作系统:win7 svn插件:Window -> Preferences -> Team -> SVN 修改方式: 1,删除C:\Users\用户名\AppData\Roaming ...
- (转)CSS字体大小: em与px、pt、百分比之间的对比
CSS样式最混乱的一个方面是应用程序中文本扩展的font-size属性.在CSS中,你可以用四个不同的单位度量来显示在web浏览器中的文本 大小.这四个单位哪一种最适合Web? 这个问题引起了广泛的争 ...
- SQL Server delete、truncate、drop
在T-SQL中这三个命令符,相信很多朋友都不会陌生的,我自己在工作也会常常使用到它们,虽然我们清除的知道用这三个命令符可以达到怎样的预期效果. 但是却很少深入的去了解它们,知道它们有什么区别,又各有什 ...
- Java中的static关键字解析(转自海子)__为什么main方法必须是static的,因为程序在执行main方法的时候没有创建任何对象,因此只有通过类名来访问。
Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键 ...
- Jquery为下拉列表动态赋值与取值,取索引
接触前端也不久对jquery用的也只是皮毛,写过去感觉能复用的发出来,大家指点下 1.下拉列表动态赋值 function initddlYear() { var mydate = new Date() ...
- Ngen生成Native代码实战及优缺点分析
先科普一下,.Net是一个用于Windows的托管代码模型,用于高效构建具有视觉上引人注目的用户体验的应用程序.但这个模型生成的代码并非可执行代码,而是由.Net公共语言运行库环境执行的IL代码.所以 ...
- Core Canvas–Day1
绘制 1.坐标系统: canvas的坐标以左上角为原点,如图 可对canvas的坐标系统进行变换,变换的方式有 平移translate 旋转rotate 缩放scale 创建自定义的变换方式,切变 2 ...
- 安卓模拟器还是"genymotion"最靠谱.
安卓模拟器还是"genymotion"最靠谱. genymotion