HDU 5381 The sum of gcd(数论+莫队算法)
来源:互联网 发布:唐青枫捏脸数据 编辑:程序博客网 时间:2024/05/20 20:22
Description
给出一序列a,定义,m次查询,每次给出l,r,求f(l,r)
Input
第一行一整数T表示用例组数,每组用例首先输入一整数n表示序列长度,然后n个整数ai,之后输入一整数m表示查询数,之后m行每行两个整数l,r表示一个查询
(1<=T<=3,1<=n,m<=1e4,1<=ai<=1e9,1<=l < r<=n)
Output
对于每个查询,输出f(l,r)
Sample Input
2
5
1 2 3 4 5
3
1 3
2 3
1 4
4
4 2 6 9
3
1 3
2 4
2 3
Sample Output
9
6
16
18
23
10
Solution
l或r固定之后,f(l,r)的取值最多有logA种情况,故每次从f(l,r)转移到至多logA次更新,用vl[i]记录以第i个元素为左端点的区间gcd所有取值,vl[i][j].first表示第j个gcd取值,vl[i][j].second表示使得f(i,r)=vl[i][j].first的最大r值;用vr[i]记录以第i个元素为右端点的区间gcd所有取值,vr[i][j].first表示第j个gcd取值,vr[i][j].second表示使得f(l,i)=vr[i][j].first的最小l值。用nlogA的复杂度处理出vl和vr,之后用莫队离线处理这m个查询,总时间复杂度
Code
#include<cstdio>#include<cstring>#include<algorithm>#include<vector>#include<cmath>using namespace std;typedef long long ll;#define maxn 11111typedef pair<int,int>P;vector<P>vl[maxn],vr[maxn];int T,n,nn,m,a[maxn],l,r;ll res,ans[maxn];int gcd(int a,int b){ return b?gcd(b,a%b):a;}struct Query{ int l,r,id; bool operator < (const Query &b)const { if((l-1)/nn!=(b.l-1)/nn)return l<b.l; return r<b.r; }}q[maxn];void update_l(int v){ int last=l; ll temp=0; for(int i=0;i<vl[l].size();i++) { int g=vl[l][i].first,p=vl[l][i].second; if(p<=r)temp+=1ll*(p-last+1)*g,last=p+1; else { temp+=1ll*(r-last+1)*g; break; } } res+=1ll*v*temp;}void update_r(int v){ int last=r; ll temp=0; for(int i=0;i<vr[r].size();i++) { int g=vr[r][i].first,p=vr[r][i].second; if(p>=l)temp+=1ll*(last-p+1)*g,last=p-1; else { temp+=1ll*(last-l+1)*g; break; } } res+=1ll*v*temp;}int main(){ scanf("%d",&T); while(T--) { scanf("%d",&n); nn=(int)(sqrt(1.0*n)+0.5); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); vl[i].clear(),vr[i].clear(); } vl[n+1].clear(),vr[0].clear(); for(int i=1;i<=n;i++) { int x=a[i],y=i; for(int j=0;j<vr[i-1].size();j++) { int g=gcd(vr[i-1][j].first,a[i]); if(g!=x) { vr[i].push_back(P(x,y)); x=g; } y=vr[i-1][j].second; } vr[i].push_back(P(x,y)); } for(int i=n;i>=1;i--) { int x=a[i],y=i; for(int j=0;j<vl[i+1].size();j++) { int g=gcd(vl[i+1][j].first,a[i]); if(g!=x) { vl[i].push_back(P(x,y)); x=g; } y=vl[i+1][j].second; } vl[i].push_back(P(x,y)); } scanf("%d",&m); for(int i=0;i<m;i++) { scanf("%d%d",&q[i].l,&q[i].r); q[i].id=i; } sort(q,q+m); res=0,l=1,r=0; for(int i=0;i<m;i++) { while(r<q[i].r)r++,update_r(1); while(r>q[i].r)update_r(-1),r--; while(l<q[i].l)update_l(-1),l++; while(l>q[i].l)l--,update_l(1); ans[q[i].id]=res; } for(int i=0;i<m;i++)printf("%I64d\n",ans[i]); } return 0;}
0 0
- HDU 5381 The sum of gcd(数论+莫队算法)
- HDU 5381The sum of gcd 莫队算法
- 莫队算法,gcd(The sum of gcd,HDU 5381)
- hdu 5381 The sum of gcd 莫队算法+区间gcd
- HDU 5381(The sum of gcd-莫队算法解决区间段gcd的和)
- hdu 5381 The sum of gcd 2015多校联合训练赛#8莫队算法
- HDU 5381 The sum of gcd 莫队暴力
- HDOJ 5381 The sum of gcd 莫队算法
- hdu 5381 The sum of gcd(线段树+gcd)
- HDU 5381 The sum of gcd 询问区间内所有子区间的GCD和 [莫队算法]
- HDU 5381 The sum of gcd
- hdu 5381 The sum of gcd
- hdu 5381 The sum of gcd
- HDU 5381The sum of gcd
- HDU 5381 The sum of gcd
- HDU 5381 The sum of gcd
- HDU 5381 The sum of gcd
- HDU 4676 Sum Of Gcd【数论,数据结构(分块)】
- jQuery、AJAX基础学习笔记
- mysql主从复制错误sql-running no
- lightoj 1134 - Be Efficient
- Numpy和Matplotlib的安装
- 浅谈Linux内存管理机制
- HDU 5381 The sum of gcd(数论+莫队算法)
- 数据结构第2章上机实验题2.1
- [Machine Learning]---ROC(Receiver Operating Characteristic) Curve
- CSS学习笔记
- matlab自带滤波器函数小结(图像处理)
- 深度好文丨读完此文,就知道Hadoop了!
- Microsoft SQL Server 2008 R2完全卸载再安装手册
- android的四大组件及其生命周期
- VS2013连接sql2008的问题-Could not load file or assembly 'Microsoft.SqlServer.Manageme