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 ...
随机推荐
- Nginx+Django+Uwsgi+php
在FreeBSD结合Nginx和FastCGI简单配置Django和PHP http://blog.chinaunix.net/uid-11131943-id-3031767.html Nginx+ ...
- html代码转义到js时,往往会遇到问题,这代码实现html和js互转
这段代码是直接可以用的,大家不妨试试.<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...
- C++ 类成员函数作为参数
#include <iostream> using namespace std; typedef int int32_t; struct IMsgBody{ int body; }; st ...
- php--memcahce安装
安装php_memcache.dll扩展 1.首先将php_memcache.dll文件放入E:\server\php\ext目录下 (php_memcache.dll下载地址:http://wind ...
- Qt窗体内控件自适应调整大小
http://stackoverflow.com/questions/3492739/auto-expanding-layout-with-qt-designer After creating you ...
- 小米1plus MIUI RadioButton的问题
小米1plus MIUI RadioButton不能设置setBackground(Drawable drawable);会变成黑色背景,需要单独处理
- Android性能优化典范(转)
转载自oschina. 2015年伊始,Google发布了关于Android性能优化典范的专题, 一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍 ...
- C#中jQuery Ajax实例(二)
上一篇写了一个简单的Ajax异步程序,这一次同样是简单的程序,只不过这次先把参数传到一般处理程序(后缀为ashx)中,再把结果传回到页面. 1.html代码: <html xmlns=" ...
- 【转】Android各种Adapter的用法
转自:http://my.oschina.net/u/658933/blog/372151 Android各种Adapter的用法 发表于5个月前(2015-01-27 10:56) 阅读(143 ...
- qt 控件 背景色 透明 除去边框
在调试ui的时候,需要将背景色变为透明,与母控件的颜色一致,并且除去边框. 参考链接: http://www.qtcentre.org/threads/12148-how-QTextEdit-tran ...