hdu5381 The sum of gcd
莫队算法,预处理出每个数字往后的gcd情况,每个数字的gcd只可能是他的因子,因此后面最多只可能有logn种,可以先预处理出,然后套莫队算法,复杂度O(n*sqrt(n)*log(n))。
代码
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
#define N 100000
using namespace std;
int n,q,s[N][],i,j,tmp,l,r;
long long ans,Ans[N];
vector<pair<int,int> > vec0[N],vec1[N];
struct g
{
int l,r,t,id;
}Q[N];
bool cmp(g a,g b)
{
if (a.t==b.t)
return a.r<b.r;
return a.t<b.t;
}
int gcd(int a,int b)
{
if (b==) return a;
return gcd(b,a%b);
}
int GCD(int a,int b)
{
int k;
k=log2(b-a+);
return gcd(s[a][k],s[b-(<<k)+][k]);
}
int ef(int i,int l,int r,int x)
{
int m;
while (l<=r)
{
m=(l+r)>>;
if (GCD(i,m)==x) l=m+;else r=m-;
}
return l;
}
int EF(int i,int l,int r,int x)
{
int m;
while (l<=r)
{
m=(l+r)>>;
if (GCD(m,i)==x) r=m-;else l=m+;
}
return r;
}
long long calc(int l,int r)
{
long long ans=;
int len,t,i;
if (l<r)
{
len=vec0[l].size();
t=l;
for (i=;i<len;i++)
{
ans+=1LL*(min(r,vec0[l][i].second)-t+)*vec0[l][i].first;
t=vec0[l][i].second+;
if (t>r) break;
}
}
else
{
len=vec1[l].size();
t=l;
for (i=;i<len;i++)
{
ans+=1LL*(t-max(r,vec1[l][i].second)+)*vec1[l][i].first;
t=vec1[l][i].second-;
if (t<r) break;
}
}
return ans;
}
void QL()
{
while (l<Q[i].l)
{
ans-=calc(l,r);
l++;
}
while (l>Q[i].l)
{
l--;
ans+=calc(l,r);
}
}
void QR()
{
while (r<Q[i].r)
{
r++;
ans+=calc(r,l);
}
while (r>Q[i].r)
{
ans-=calc(r,l);
r--;
}
}
int main()
{
int test;
scanf("%d",&test);
while (test--)
{
scanf("%d",&n);
for (i=;i<=n;i++)
{
scanf("%d",&s[i][]);
vec0[i].clear();
vec1[i].clear();
}
for (i=n;i>=;i--)
for (j=;j<=log2(n);j++)
s[i][j]=gcd(s[i][j-],s[i+(<<(j-))][j-]); for (i=;i<=n;i++)
{
l=i;r=n;
while (l<=n)
{
tmp=GCD(i,l);
l=ef(i,l,r,tmp);
vec0[i].push_back(make_pair(tmp,l-));
}
} for (i=n;i>=;i--)
{
l=;r=i;
while (r>)
{
tmp=GCD(r,i);
r=EF(i,l,r,tmp);
vec1[i].push_back(make_pair(tmp,r+));
}
} scanf("%d",&q);
for (i=;i<=q;i++)
{
scanf("%d%d",&Q[i].l,&Q[i].r);
Q[i].id=i;Q[i].t=Q[i].l/;
}
sort(Q+,Q++q,cmp);
l=Q[].l;r=Q[].r;ans=;
for (i=l;i<=r;i++) ans+=calc(i,r);
Ans[Q[].id]=ans; for (i=;i<=q;i++)
{
if (l<Q[i].l)
{
QR();QL();
}
else
{
QL();QR();
}
Ans[Q[i].id]=ans;
}
for (i=;i<=q;i++)
printf("%I64d\n",Ans[i]);
}
}
hdu5381 The sum of gcd的更多相关文章
- hdu5381 The sum of gcd]莫队算法
题意:http://acm.hdu.edu.cn/showproblem.php?pid=5381 思路:这个题属于没有修改的区间查询问题,可以用莫队算法来做.首先预处理出每个点以它为起点向左和向右连 ...
- 【HDU 5381】 The sum of gcd (子区间的xx和,离线)
[题目] The sum of gcd Problem Description You have an array A,the length of A is nLet f(l,r)=∑ri=l∑rj= ...
- 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 4676 Sum Of Gcd 莫队+phi反演
Sum Of Gcd 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4676 Description Given you a sequence of ...
- 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 ...
- HDU 4676 Sum Of Gcd 【莫队 + 欧拉】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=4676 Sum Of Gcd Time Limit: 10000/5000 MS (Java/Others ...
- 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 ...
- hdu 5381 The sum of gcd(线段树+gcd)
题目链接:hdu 5381 The sum of gcd 将查询离线处理,依照r排序,然后从左向右处理每一个A[i],碰到查询时处理.用线段树维护.每一个节点表示从[l,i]中以l为起始的区间gcd总 ...
- HDOJ 5381 The sum of gcd 莫队算法
大神题解: http://blog.csdn.net/u014800748/article/details/47680899 The sum of gcd Time Limit: 2000/1000 ...
随机推荐
- 低功耗蓝牙4.0BLE编程-nrf51822开发(5)-链路层
链路层定义设备处于状态机中五种状态的一种: (1)旁路状态: 处于此状态下的设备不发送或接收数据,处于其它状态下都可以转到此状态. (2)广告状态: 处于此状态的设备发送广播包或者监听.响应广播包.可 ...
- 日志案例分析(PV,UV),以及动态分区
1.实现的流程 需求分析 时间:日,时段 分区表:两级 PV UV 数据清洗 2015-08-28 18:19:10 字段:id,url,guid,tracktime 数据分析 导出 2.新建源数据库 ...
- python join
# 对序列进行操作 ' '.join(['hello','good','boy','doiido']) hello:good:boy:doiido # 对字符串进行操作 ':'.join(" ...
- qt如何实现一个渐隐窗口呢(开启的时候他是从上往下渐渐显示)
qt如何实现一个渐隐窗口呢?就是比如说开启的时候他是从上往下渐渐显示的,关闭的时候从下往上渐渐小时的http://stackoverflow.com/questions/19087822/how-to ...
- Aptana Studio 3的汉化
Aptana Studio 3(下面简称Aptana 3)的汉化方法 1.找到这个网站 http://aptana.com/support 2.单击下面的链接 view documentation 在 ...
- JavaScript学习之对象
JavaScript对象 一.对象简介 JavaScript 是面向对象的编程语言 (OOP).OOP 语言使我们有能力定义自己的对象和变量类型.注意:对象只是一种特殊的数据.对象拥有属性和方法. 1 ...
- CSS知识点补充
一.css框模型概述 元素框的最内部分是实际的内容,直接包围内容的是内边距.内边距呈现了元素的背景.内边距的边缘是边框.边框以外是外边距,外边距默认是透明的,因此不会遮挡其后的任何元素 1.css内边 ...
- H5 -- 本地存储计数器的值 和前端校验用户
1. 存储计数器的值 <!DOCTYPE html> <html> <head lang="en"> <meta charset=&quo ...
- 《JAVA NIO》第一章 简介
1.2 CPU已不再是束缚 相反,是JVM 自身在I/O 方面效率欠佳.操作系统与Java 基于流的I/O模型有些不匹配. 操作系统要移动的是大块数据(缓冲区),这往往是在硬件直接存储器存取(DMA) ...
- SET Statements (Transact-SQL)
The Transact-SQL programming language provides several SET statements that change the current sessio ...