Hdu5381-The sum of gcd(莫队)
#include<cstdio>
#include<vector>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=;
typedef __int64 LL;
int N,M,A[maxn],block;
struct Ques
{
int x,y,id;
Ques(int x=,int y=,int id=):x(x),y(y),id(id){}
bool operator < (const Ques& t) const
{
int a=x/block,b=t.x/block; //排序
if(a!=b) return a<b;
return y<t.y;
}
}ques[maxn];
int gcd(int a,int b){ return b==?a:gcd(b,a%b); }
struct node
{
int id,g;
node(int id=,int g=):id(id),g(g){}
};
vector<node> vl[maxn],vr[maxn];
void GetLe()
{
for(int i=;i<=N;i++) //得到以i为右端点连续区间的gcd值
{
if(i==){ vl[i].push_back(node(i,A[i])); continue; }
int g=A[i],id=i;
int Size=vl[i-].size();
for(int j=;j<Size;j++)
{
node& t=vl[i-][j];
int ng=gcd(t.g,g);
if(ng!=g) vl[i].push_back(node(id,g));
g=ng; id=t.id;
}
vl[i].push_back(node(id,g));
}
}
void GetRi() //同理
{
for(int i=N;i>=;i--)
{
if(i==N){ vr[i].push_back(node(i,A[i])); continue; }
int g=A[i],id=i;
int Size=vr[i+].size();
for(int j=;j<Size;j++)
{
node& t=vr[i+][j];
int ng=gcd(t.g,g);
if(ng!=g) vr[i].push_back(node(id,g));
g=ng; id=t.id;
}
vr[i].push_back(node(id,g));
}
}
LL WorkLe(int x,int y)
{
int Size=vl[y].size();
int ny=y;
LL ret=;
for(int i=;i<Size;i++)
{
node& t=vl[y][i];
if(t.id>=x)
{
ret+=(LL)t.g*(ny-t.id+);
ny=t.id-; //跳过去
}
else{ ret+=(LL)t.g*(ny-x+); break; }
}
return ret;
}
LL WorkRi(int x,int y)
{
int nx=x;
LL ret=;
int Size=vr[x].size();
for(int i=;i<Size;i++)
{
node& t=vr[x][i];
if(t.id<=y)
{
ret+=(LL)t.g*(t.id-nx+);
nx=t.id+;
}
else { ret+=(LL)t.g*(y-nx+); break; }
}
return ret;
}
LL ans[maxn];
void solve()
{
for(int i=;i<=N;i++) vl[i].clear(),vr[i].clear();
block=(int)sqrt(N+0.5); //分块
sort(ques,ques+M); //排序
GetLe(); //得到左边连续相同的gcd区间
GetRi(); //得到右边连续相同的gcd区间
int x=,y=;
LL ret=;
for(int i=;i<M;i++) //莫队的主要实现部分
{
Ques& q=ques[i];
while(y<q.y){ y++; ret+=WorkLe(x,y); }
while(y>q.y){ ret-=WorkLe(x,y); y--; }
while(x<q.x){ ret-=WorkRi(x,y); x++; }
while(x>q.x){ x--; ret+=WorkRi(x,y); }
ans[q.id]=ret; //保存答案
}
for(int i=;i<M;i++) printf("%lld\n",ans[i]); //输出
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
for(int i=;i<=N;i++) scanf("%d",&A[i]);//输入
scanf("%d",&M);
int x,y;
for(int i=;i<M;i++)
{
scanf("%d%d",&x,&y);
ques[i]=Ques(x,y,i); //离线保存查询
}
solve();
}
return ;
}
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 莫队+预处理
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 莫队+欧拉函数
题意:给定一个11~nn的全排列AA,若干个询问,每次询问给出一个区间[l,r][l,r],要求得出∑l≤i<j≤r gcd(Ai,Aj)的值. 解法:这题似乎做的人不是很多,蒟蒻当然不会做只 ...
- hdu 4676 Sum Of Gcd 莫队+数论
题目链接 给n个数, m个询问, 每个询问给出[l, r], 问你对于任意i, j.gcd(a[i], a[j]) L <= i < j <= R的和. 假设两个数的公约数有b1, ...
- HDOJ 5381 The sum of gcd 莫队算法
大神题解: http://blog.csdn.net/u014800748/article/details/47680899 The sum of gcd Time Limit: 2000/1000 ...
- hdu5381 The sum of gcd
莫队算法,预处理出每个数字往后的gcd情况,每个数字的gcd只可能是他的因子,因此后面最多只可能有logn种,可以先预处理出,然后套莫队算法,复杂度O(n*sqrt(n)*log(n)). 代码 #i ...
- [CSP-S模拟测试]:sum(数学+莫队)
题目传送门(内部题63) 输入格式 第一行有一个整数$id$,表示测试点编号.第一行有一个整数$q$,表示询问组数.然后有$q$行,每行有两个整数$n_i,m_i$. 输出格式 一共有$q$行,每行一 ...
- HDU 4676 Sum Of Gcd 【莫队 + 欧拉】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=4676 Sum Of Gcd Time Limit: 10000/5000 MS (Java/Others ...
随机推荐
- MongoDB命令行操作
本文专门介绍MongoDB的命令行操作.其实,这些操作在MongoDB官网提供的Quick Reference上都有,但是英文的,为了方便,这里将其稍微整理下,方便查阅. 这里用来做测试的是远端(10 ...
- pyqt记录内容(音乐播放器)
#这是UI文件 # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'AudioPlayerDia ...
- Java IO教程 导读
Java IO是一套java 用来读写数据(输入和输出)的API.大部分程序都要处理一些输入,并有输入产生一些输出.Java为此提供了java.io包. 如果你浏览下java.io包,会对其中各样的类 ...
- Pig系统分析(6)-从Physical Plan到MR Plan再到Hadoop Job
从Physical Plan到Map-Reduce Plan 注:由于我们重点关注的是Pig On Spark针对RDD的运行计划,所以Pig物理运行计划之后的后端參考意义不大,这些部分主要分析流程, ...
- line-hight-(行高)解析
行高定义:line-height属性是指文本行基线之间的距离. 顶线.中线.基线.底线概念 从上到下四条线分别是顶线.中线.基线.底线,很像才学英语字母时的四线三格,我们知道vertical-alig ...
- UIAlertController的创建以及添加
个人还是更喜欢以前的UIAlertView的创建方法,更新后的UIAlertController虽然说将UIAlertView和UIActionSheet的功能和作用以一种模块化替换的方式来代替,但是 ...
- HSSFClientAnchor(int dx1,int dy1,int dx2,int dy2,short col1,int row1,short col2, int row2)
public HSSFClientAnchor(int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int ...
- gitweb随记
1.安装gitweb,命令安装即可 apt-get install gitweb 2.clone cgi $ git clone git://git.kernel.org/pub/scm/git/gi ...
- javaWeb RSA加密使用
加密算法在各个网站运用很平常,今天整理代码的时候看到了我们项目中运用了RSA加密,就了解了一下. 先简单说一下RSA加密算法原理,RSA算法基于一个十分简单的数论事实:将两个大质数相乘十分容易,但 ...
- C#检测串口被拔掉等一些触发事件合集
// //设备异常重载 // protected override void WndProc(ref Message m) { if (m.Msg == 0x0219) {//设备被拔出 if (m. ...