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 ...
随机推荐
- phpcms v9模版调用代码大全(全面而实用)
首页调用栏目 {pc:content action="category" siteid="$siteid" num="15" order=& ...
- BlueDroid代码分析之GKI
目录 1. 概述 2. 线程 2.1 主要函数 2.2 功能 3. 事件 3.1 主要函数 3.2 功能 1. 概述 GKI以库libbt-brcm_gki.so(Static Lib?)的形式提供给 ...
- PHP 日期比较
$temptime = mktime(8,2,12,4,4,2014);$dt1 = date("Y-m-d",time());$dt2 = date("Y-m-d&qu ...
- [LeetCode]题解(python):059-Spiral Matrix II
题目来源 https://leetcode.com/problems/spiral-matrix-ii/ Given an integer n, generate a square matrix fi ...
- thinkphp接手机网站接口
首先说下,支付宝提供的demo不一定完全正确,可能回缺少些步骤,所以,最好按照规则文档的步骤参照demo写. 先说遇到的问题: 1.错误0001,缺少partner.service等必参数 最初选择的 ...
- django 部署到 apache
安装完django之后,每次都需要通过命令来启动启动开发服务器.虽然调试和测试方便,但只能在本地运行,并且不能承受许多用户同时使用的负载.所以需要将Django部署到生产级的服务器,这里选择apach ...
- 个人常用iOS第三方库以及XCode插件介绍
第三方库 CocoaPod CocoaPod并不是iOS上的第三方库 而是大名鼎鼎的第三方库的管理工具 在CocoaPod没有出现之前 第三方库的管理是非常痛苦的 尤其是一些大型的库(比如nimbus ...
- iOS GCD简单使用
Grand Central Dispatch (GCD) 1)运行在主线程的Main queue,通过dispatch_get_main_queue获取. /*!* @function dispatc ...
- 由单例模式学到:volatile关键字
MSDN上说: volatile 关键字指示一个字段可以由多个同时执行的线程修改. 声明为 volatile 的字段不受编译器优化的限制. 这样可以确保该字段在任何时间呈现的都是最新的值. volat ...
- Could not find class XXX referenced from method XXX.<YYY>
Since some ADT-Version you have to set which libraries / projects should be exported too. Project-Pr ...