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 ...
随机推荐
- crucible3.x +fisheye3.x 安装和破解
2015-11-24 22:29 423人阅读 评论(1) 收藏 举报 分类: linux(1) 版权声明:本文为博主原创文章,未经博主允许不得转载. 破解文件下载路径:http://downlo ...
- css中textarea去掉边框和选中后的蓝色边框问题的解决方法
我们在设计网页的输入框时,有时会遇到需要把textarea的边框去掉的问题,经过测试,下面的代码是可以的. textarea{ border: solid 0px; outline:none; }
- Windows下Memcache的安装与在php中使用
memcache dll插件和测试例子下载地址: http://pecl.php.net/package/memcache Windows下Memcache的安装方法 Memcached官方:http ...
- 创建数据库指定路径sql
create database stuDB on primary -- 默认就属于primary文件组,可省略(/*--数据文件的具体描述--*/ name='stuDB_data', - ...
- Python 链接Mysql数据库
参考链接:https://pypi.python.org/pypi/PyMySQL#downloads import pymysql.cursors,xml.dom.minidom # Connect ...
- Asp.net MVC 版本简史
http://www.dotnet-tricks.com/Tutorial/mvc/XWX7210713-A-brief-history-of-Asp.Net-MVC-framework.html A ...
- Largest Number || LeetCode
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX 1000 int cm ...
- JS给swf传参数
不仅可以用flashvars ="name=12&age=23" 还可以在指定swf地址时传参数src="test.swf?name=12&age=23& ...
- 一个短小的JS函数,用来得到仅仅包含不重复元素的数组
下面函数主要利用了数组的sort方法,之后的逻辑是看最后一个元素是否等于要添加的元素,如果不是就往尾后加. 这个做法的效率等于sort方法的效率,还过得去. 代码: <!DOCTYPE HTML ...
- sql中的行转列和列转行的问题
sql中的行转列和列转行的问题 这是一个常见的问题,也是一个考的问题 1.行转列的问题 简单实例 CREATE TABLE #T ( MON1 INT, MON2 INT, MON3 INT ) G ...