Problem Description

This is a simple problem. The teacher gives Bob a list of problems about GCD (Greatest Common Divisor). After studying some of them, Bob thinks that GCD is so interesting. One day, he comes up with a new problem about GCD. Easy as it looks, Bob cannot figure it out himself. Now he turns to you for help, and here is the problem:

Given an array \(a\) of \(N\) positive integers \(a_1, a_2, \cdots a_{N-1}, a_N\) ; a subarray of \(a\) is defined as a continuous interval between \(a_1\) and \(a_N\) .In other words,\(a_i, a_{i+1}, \cdots, a_{j-1}, a_j\) is a subarray of \(a\), for \(1\le i\le j\le N\).For a query in the form \((L, R)\) , tell the number of different GCDs contributed by all subarrays of the interval \([L, R]\).

Input

There are several tests, process till the end of input.

For each test, the first line consists of two integers \(N\) and \(Q\), denoting the length of the array and the number of queries, respectively. \(N\) positive integers are listed in the second line, followed by \(Q\) lines each containing two integers \(L,R\) for a query.

You can assume that

\(1≤N,Q≤100000\)

\(1≤a_i≤1000000\)

Output

For each query, output the answer in one line.

Sample Input

5 3

1 3 4 6 9

3 5

2 5

1 5

Sample Output

6

6

6

Description(CHN)

给定一个数列,多次询问,每次询问 \(L,R\),求 \([L,R]\) 中所有子区间的 \(gcd\) 有多少种

Solution

预处理对于数列中的每个位置,对于它为 \(R\) 的所有区间中不同的 \(gcd\) 出现的最右边的 \(L\) 是什么。这个东西直接在上一个位置的基础上枚举就好了

将询问离线

我们用BIT维护每种 \(gcd\) 出现的区间的 \(L\) 的最右的位置在哪里,然后就用差分计算答案就好了

#include<bits/stdc++.h>
#define ui unsigned int
#define ll long long
#define db double
#define ld long double
#define ull unsigned long long
const int MAXN=300000+10;
int n,m,a[MAXN],ans[MAXN];
std::map<int,int> M;
std::vector< std::pair<int,int> > V[MAXN],query[MAXN];
struct BIT{
int C[MAXN];
inline void init()
{
memset(C,0,sizeof(C));
}
inline int lowbit(int x)
{
return x&(-x);
}
inline void add(int x,int k)
{
while(x<=n)C[x]+=k,x+=lowbit(x);
}
inline int sum(int x)
{
if(!x)return 0ll;
int res=0;
while(x>0)res+=C[x],x-=lowbit(x);
return res;
}
};
BIT T;
template<typename T> inline void read(T &x)
{
T data=0,w=1;
char ch=0;
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch>='0'&&ch<='9')data=((T)data<<3)+((T)data<<1)+(ch^'0'),ch=getchar();
x=data*w;
}
template<typename T> inline void write(T x,char ch='\0')
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
if(ch!='\0')putchar(ch);
}
template<typename T> inline void chkmin(T &x,T y){x=(y<x?y:x);}
template<typename T> inline void chkmax(T &x,T y){x=(y>x?y:x);}
template<typename T> inline T min(T x,T y){return x<y?x:y;}
template<typename T> inline T max(T x,T y){return x>y?x:y;}
#define ft first
#define sd second
int main()
{
while(scanf("%d%d\n",&n,&m)!=EOF)
{
for(register int i=1;i<=n;++i)read(a[i]),V[i].clear(),query[i].clear();
T.init();M.clear();
V[1].push_back(std::make_pair(1,a[1]));
for(register int i=2;i<=n;++i)
{
int now=a[i];V[i].push_back(std::make_pair(i,a[i]));
for(register int j=0,lt=V[i-1].size();j<lt;++j)
{
std::pair<int,int> pr=V[i-1][j];
int d=std::__gcd(now,pr.sd);
if(d!=now)V[i].push_back(std::make_pair(pr.ft,d)),now=d;
}
}
for(register int i=1;i<=m;++i)
{
int l,r;read(l);read(r);
query[r].push_back(std::make_pair(i,l));
}
for(register int i=1;i<=n;++i)
{
for(register int j=0,lt=V[i].size();j<lt;++j)
{
std::pair<int,int> pr=V[i][j];
if(M[pr.sd])T.add(M[pr.sd],-1);
T.add(pr.ft,1);M[pr.sd]=pr.ft;
}
for(register int j=0,lt=query[i].size();j<lt;++j)
{
std::pair<int,int> pr=query[i][j];
ans[pr.ft]=T.sum(i)-T.sum(pr.sd-1);
}
}
for(register int i=1;i<=m;++i)printf("%d\n",ans[i]);
}
return 0;
}

【刷题】HDU 5869 Different GCD Subarray Query的更多相关文章

  1. HDU 5869 Different GCD Subarray Query rmq+离线+数状数组

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5869 Different GCD Subarray Query Time Limit: 6000/3 ...

  2. HDU 5869 Different GCD Subarray Query 离线+树状数组

    Different GCD Subarray Query Problem Description   This is a simple problem. The teacher gives Bob a ...

  3. hdu 5869 Different GCD Subarray Query BIT+GCD 2016ICPC 大连网络赛

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  4. HDU 5869 Different GCD Subarray Query (GCD种类预处理+树状数组维护)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5869 问你l~r之间的连续序列的gcd种类. 首先固定右端点,预处理gcd不同尽量靠右的位置(此时gc ...

  5. HDU 5869 Different GCD Subarray Query 树状数组 + 一些数学背景

    http://acm.hdu.edu.cn/showproblem.php?pid=5869 题意:给定一个数组,然后给出若干个询问,询问[L, R]中,有多少个子数组的gcd是不同的. 就是[L, ...

  6. HDU 5869 Different GCD Subarray Query

    离线操作,树状数组,$RMQ$. 这个题的本质和$HDU$ $3333$是一样的,$HDU$ $3333$要求计算区间内不同的数字有几个. 这题稍微变了一下,相当于原来扫描到$i$的之后是更新$a[i ...

  7. HDU 5869 Different GCD Subarray Query 树状数组+离线

    Problem Description This is a simple problem. The teacher gives Bob a list of problems about GCD (Gr ...

  8. HDU 5869 Different GCD Subarray Query(2016大连网络赛 B 树状数组+技巧)

    还是想不到,真的觉得难,思路太巧妙 题意:给你一串数和一些区间,对于每个区间求出区间内每段连续值的不同gcd个数(该区间任一点可做起点,此点及之后的点都可做终点) 首先我们可以知道每次添加一个值时gc ...

  9. HDU 5869.Different GCD Subarray Query-区间gcd+树状数组 (神奇的标记右移操作) (2016年ICPC大连网络赛)

    树状数组... Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/6 ...

随机推荐

  1. odoo明细表汇总数据

    一.在主表中#改动地方 总结算金额 求和:def _get_subtotal2(self, cr, uid, ids, field_name, arg, context=None): # 初始化 re ...

  2. 秋风下的萧瑟 NOIP2018 游记

    “北方的秋天还真的是美丽冻人呢!” 是么?我有些疑惑,任凭雨滴落在脸上. 这天,可真不好,秋雨可让这天气一天比一天的寒冷了. 大概,故事从这里开始吧? 上一次的故事说道了哪里?那么,我们从今天的新故事 ...

  3. mssql sqlserver 保留小数位指定位数的2种方法分享

    摘要: 下文讲述将"sql数值型"类型数值转换为指定小数位的数据 方法1:采用 cast 方式转换数值类型至指定小数位: ,) set @a = 18.893 ,) 方法2:采用 ...

  4. Windows Server2003 IIS服务器安全配置整理

    一.系统的安装   1.按照Windows2003安装光盘的提示安装,默认情况下2003没有把IIS6.0安装在系统里面.2.IIS6.0的安装 开始菜单—>控制面板—>添加或删除程序—& ...

  5. CS229笔记:生成学习算法

    在线性回归.逻辑回归.softmax回归中,学习的结果是\(p(y|x;\theta)\),也就是给定\(x\)的条件下,\(y\)的条件概率分布,给定一个新的输入\(x\),我们求出不同输出的概率, ...

  6. mpvue两小时,产出一个《点钞辅助工具》小程序

    CoffeeScript,Pug,Sass使用 以下内容门槛较高,如看不懂或觉得需要继续了解,结尾处放置了原视频流程与GitHub地址,欢迎琢磨与Star,谢谢. 文章不做技术语法解读,如不清楚,请前 ...

  7. Ing_制作在线QQ

    制作在线QQ的具体步骤 1.首先登录到http://is.qq.com/webpresence/code.shtml 网站2.选择风格3.填写相关数据4.生成网页代码5.复制代码到“写字板”,另存文件 ...

  8. WebGL模型拾取——射线法

    今天要把WebGL中一个非常重要的算法记录下来——raycaster射线法拾取模型.首先我们来了解一下为什么要做模型拾取,我们在做webgl场景交互的时候经常要选中场景中的某个模型,比如鼠标拖拽旋转, ...

  9. Java收发邮件过程中具体的功能是怎么实现的

    SMTP协议 用户连上邮件服务器后,要想给它发送一封电子邮件,需要遵循一定的通迅规则,SMTP协议就是用于定义这种通讯规则的. 因而,通常我们也把处理用户smtp请求(邮件发送请求)的邮件服务器称之为 ...

  10. idou老师教你学Istio 17 : 通过HTTPS进行双向TLS传输

    众所周知,HTTPS是用来解决 HTTP 明文协议的缺陷,在 HTTP 的基础上加入 SSL/TLS 协议,依靠 SSL 证书来验证服务器的身份,为客户端和服务器端之间建立“SSL”通道,确保数据运输 ...