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;
}

最新文章

  1. 64. ZigZag Conversion
  2. [Solution] Microsoft Windows 服务(1) C#创建Windows服务
  3. [Android Pro] 监听内容提供者ContentProvider的数据变化
  4. content management system
  5. Flex4+Spring3+Hibernate3+BlazeDS整合笔记
  6. stm32f407 定时器 用的APB1 APB2 及 定时器频率
  7. windows程序设计读书笔记1——创建窗口
  8. PL/SQL 游标的使用
  9. DOM操作-引用同级的元素
  10. 到处是坑的微信公众号支付开发(java)
  11. Unix 让进程安全地退出
  12. React修改state(非redux)中数组和对象里边的某一个属性的值
  13. 深入理解Java虚拟机7-chap10-11-斗者4星
  14. SocketIO Server
  15. csdn中使用git的一些注意事项---免得走弯路
  16. (String中)正则表达式使用如下
  17. 字符串GZIP压缩解压
  18. C#中.XSD是什么文件?
  19. java POI创建Excel示例(xslx和xsl区别 )
  20. Scrapy:学习笔记(1)——XPath

热门文章

  1. 20155339 Exp8 Web基础
  2. [Oracle]OpenVMS 运行 Oracle 时的推荐值
  3. Scala学习(二)--- 控制结构和函数
  4. [Zlib]_[初级]_[使用zlib库压缩和解压STL string]
  5. MSBUILD : error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”
  6. 设计模式 笔记 工厂方法 Factory Methon
  7. 基于AngularJs的单页面程序
  8. laraver框架学习------工厂模型填充测试数据
  9. 使用tensorflow实现mnist手写识别(单层神经网络实现)
  10. SCRUM 12.09 软件工程第二周计划