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 ...
随机推荐
- chrome加载本地js
通过chrome的扩展功能,可以执行一些本地脚本. 1.如何添加扩展程序 具体做法是:chrome -> 设置 -> 扩展程序 -> 加载正在开发的扩展程序 图(1.1) 图(1.2 ...
- FreeBSD简单配置SSH并用root远程登陆方法
FreeBSD简单配置SSH并用root远程登陆方法 前言:最近下载了FreeBSD,在虚拟机上安装,第一步先要开启SSH服务,用终端putty软件可以实现在windows系统进行远程管理, 初级 = ...
- 最简单的内核模块hello world
[root@rt2m09617.sqa.tbc /home/ahao.mah/main] #cat hello.c // Defining __KERNEL__ and MODULE allows u ...
- spring简单入门示例
1 控制反转IOC\依赖注入DI,因为翻译的不同,因此有两个名字. 控制反转意思就是说,当我们调用一个方法或者类时,不再有我们主动去创建这个类的对象,控制权交给别人(spring). 依赖 ...
- C# 图结构操作
仿造<<Java常用算法手册>>里面对的算法,使用C#实现了一遍. 理论知识我就不讲解了,在这本书里面已经写的非常完美! 代码如何下: using System; using ...
- 使用vue-cli脚手架安装的eslint 容易犯错的地方
1. 函数名字与括号之间要有空格. 2. 不要使用双引号 3. 不要有多月的空行 4.函数参数的逗号后要有空格 5.每个结束语句以后不用加“分号”
- BNU10791:DOTA选人
DOTA(Defense of the Ancients)是一款很受欢迎的游戏.DOTA将10个游戏玩家分为两组,分别为天灾和近卫,推倒对方主基地的一方获得胜利.每个玩家可以选择一个英雄作为游戏中的角 ...
- 8个常用的Android开发工具
周末发现一些比较有用的android开发常用的工具,里面大部分是自己经常用的,还有一些暂时很少用,暂且在这里记录下,以后一定同样会经常用到的. 1 The SDK and AVD Manager 这个 ...
- C#使用checked关键字处理"溢出"错误
代码如下: private void btnCalculate_Click(object sender, EventArgs e) { byte num1, num2;//定义两个byte变量 if( ...
- 不安装开发环境,查看logcat日志
#公司的测试终于想到要看 android 的日志.安装开发环境太麻烦了,这里有个简单的办法. 我把\sdk\platform-tools目录中,文件名以"adb"开头的三个文件打包 ...