【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 ...
随机推荐
- 2013调试sql的方法
view-sql server object explorer- 连接数据库-成功以后再服务器点击允许debug-在存储过程里面添加断点即可
- jquery处理textarea中的手动换行
textarea的手动换行会产生换行标志,但这个标志存在却看不到,存入数据库中后读出来显示在页面上却不会换行,如何处理呢? 网上众说纷纭,经过测试用 textarea的内容.replace(/\n/g ...
- java ssh框架入门
源码:http://pan.baidu.com/s/1hspUOKG
- IBM WebSphere MQ 通道类型配置
IBM WebSphere MQ 通道类型配置 初学MQ,四种常见通道,windows下操作 目录 Sender--Receiver Server-Receiver Server-Requester ...
- struts2类型转换中的错误处理
由于类型转换过程中有可能出现原始参数无法转换为目标类型的错误,所以struts2提供了类型转换中的异常处理机制. 在struts2的默认配置文件struts-default.xml中有如下一段配置代码 ...
- Ibatis学习记录
几大要素:1.jdbc.properties //数据库连接配置2.SqlMapContext.xml //主配置文件3.user_SqlMap.xml //映射文件4.三层框架 创建Ibatis工程 ...
- UVA 10795 A Different Task(汉诺塔 递归))
A Different Task The (Three peg) Tower of Hanoi problem is a popular one in computer science. Briefl ...
- windows server 2008镜像重启后密码变为默认密码的问题的解决方案
1. cmd中执行regedit,打开注册表: 修改HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Cloudbase Solusions\Cloudbase-Init ...
- skymvc网站测试之mysql数据生成
skymvc网站测试之mysql数据生成 使用方法: 删除数据 /index.php?m=test_mysql&a=autoDelete 重置自增ID /index.php?m=test_my ...
- 学习PHP 301跳转的方法
发布:JB01 来源:脚本学堂 [大 中 小]本文详细介绍了,在php编程中实现301跳转,即301永久重定向的方法,感兴趣的朋友可以参考学习下. 本文转自:http://www.jbxu ...