【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 ...
随机推荐
- [Form Builder]POST 与 commit_form 的区别
commit_form:将form上的数据写入数据库,并且会在database提交,即 直接查询表是能够查到结果,在form左下角会得到“FRM-40400:Transaction complete: ...
- c语言,strcat(),字符串拼接
#include<stdio.h> #include<string.h> int main() { char destination[25]; char *zhang=& ...
- NSObject中的isa到底是个什么?
首先看一下NSObject的定义: @interface NSObject <NSObject> { Class isa OBJC_ISA_AVAILABILITY; } 官方解释:Eve ...
- Service解析
Service解析: 运行service有如下两种方式: StartService() 访问者退出,service仍然运行: BindService() 访问者与service绑定,访问者退出,ser ...
- jdk在windows中的配置
1.下载jdk(java developer kit),其内部包含jre(java runtime environment): 安装解压缩到一盘内,如:G:\Program Files\Java: 2 ...
- 记一次ftp服务器错误 centOS 6.4 vsftpd 500 illegal port command
这个错误是因为是主动模式的,应该改为被动模式 以下是操作过程: iptables中加 -A INPUT -p tcp -m state --state NEW -m tcp --dport 10221 ...
- 《tr命令-优化版》-linux命令五分钟系列之二十五
本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...
- WinForm聊天室
前几天开始学Socket编程,跟着老师一点一点的做.最后做了一个WinForm版的小聊天室.这个聊天室的客户端和服务端都只是在本机上运行. 这里我首先和大家谈谈我对聊天室的一点理解,聊天室其实是服务端 ...
- yii2源码学习笔记(二十)
Widget类是所有部件的基类.yii2\base\Widget.php <?php /** * @link http://www.yiiframework.com/ * @copyright ...
- MYSQL数据库备份与恢复
mysqldump -h主机名 -P端口 -u用户名 -p密码 (–database) 数据库名 > 文件名.sql 备份MySQL数据库的命令 mysqldump -hhostname -u ...