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 ...
随机推荐
- Linux中open函数以及退出进程的函数
open函数的flag详解1 读写权限:O_RDONLY O_WRONLY O_RDWR (1)linux中文件有读写权限,我们在open打开文件时也可以附带一定的权限说明 (譬如O_RDONLY就表 ...
- win10 下安装、配置、启动mysql
1.下载http://dev.mysql.com/downloads/mysql/ 2.Community > MySQL Community Server 3.Other Downloads: ...
- c++ 之 字符和字符串
字符 1.字符的分类 字符主要包括字母.数字.标点符号.控制字符等 在ASCII编码表中,每一个字符都用一个十进制数来表示 注:ASCII的全称是American Standard Code for ...
- 下载配置MySql,高速启动MySql批处理,MySQLclient软件SQL-Front的配置---ShinePans
首先,下载 sql 绿色版,: http://yunpan.cn/cgERHhTtV8XGh 提取码 85bc 然后解压就可以用, 安装文件夹下有bin文件夹,从里面的命令中启动服务 例如以下: ...
- Composer 基本指令操作使用
Composer 基本指令操作使用 註: 若 composer.phar 改名為 composer, 請自行將 "php composer.phar" 替換成 "comp ...
- BNU10806:请在此处签到
每年圣诞,ZUN都会邀请很多人到幻想乡举行联欢,今年也不例外.在联欢前,所有人需要在自己的昵称旁签到(签全名),以示出席.然后ZUN 会把大家的签到表保存下来作为纪念,以激励来年努力工作. 昵称: ...
- [Regular Expressions] Find Plain Text Patterns
The simplest use of Regular Expressions is to find a plain text pattern. In this lesson we'll look a ...
- MDM 证书申请流程(vendor及customer)
整个流程分为两部分:vendor,customer. 一.Vendor 1.成为一个 MDM Vendor 1) 首先你须要拥有一个 Apple Enterprise account($299/年). ...
- 阿里云 RDS 怎么保证高可用性
RDS在系统构建之初,从网络到硬件再到系统都是基于高可用设计,他可以做到故障30秒无缝转移,对应用完全透明. 当一个数据库实例发生故障时, 另一个会立即顶上,而且对应用是完全透明的.你无需变更应用的连 ...
- iOS之断点下载,使用NSURLSession简单封装
最近公司需要做个文件管理的功能模块,刚交到博主手上时,头都大了.因为没做过这方面的东西,只好咬牙加班,并请教某位大神,指点了一下,清楚研究方向,找了网上大量资料,最后实现简单的封装. 上代码:.h文件 ...