Mole is hungry again. He found one ant colony, consisting of n ants, ordered in a row. Each ant i (1 ≤ i ≤ n) has a strength si.
In order to make his dinner more interesting, Mole organizes a version of «Hunger Games» for the ants. He chooses two numbers l and r(1 ≤ l ≤ r ≤ n) and each pair of ants with indices between l and r (inclusively) will fight. When two ants i and j fight, ant i gets one battle point only if si divides sj (also, ant j gets one battle point only if sj divides si).
After all fights have been finished, Mole makes the ranking. An ant i, with vi battle points obtained, is going to be freed only if vi = r - l, or in other words only if it took a point in every fight it participated. After that, Mole eats the rest of the ants. Note that there can be many ants freed or even none.
In order to choose the best sequence, Mole gives you t segments [li, ri] and asks for each of them how many ants is he going to eat if those ants fight.
The first line contains one integer n (1 ≤ n ≤ 105), the size of the ant colony.
The second line contains n integers s1, s2, ..., sn (1 ≤ si ≤ 109), the strengths of the ants.
The third line contains one integer t (1 ≤ t ≤ 105), the number of test cases.
Each of the next t lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), describing one query.
Print to the standard output t lines. The i-th line contains number of ants that Mole eats from the segment [li, ri].
5
1 3 2 4 2
4
1 5
2 5
3 5
4 5
4
4
1
1
In the first test battle points for each ant are v = [4, 0, 2, 0, 2], so ant number 1 is freed. Mole eats the ants 2, 3, 4, 5.
In the second test case battle points are v = [0, 2, 0, 2], so no ant is freed and all of them are eaten by Mole.
In the third test case battle points are v = [2, 0, 2], so ants number 3 and 5 are freed. Mole eats only the ant 4.
In the fourth test case battle points are v = [0, 1], so ant number 5 is freed. Mole eats the ant 4.
思路:我们用L[i]表示a[i],能向左被整除的最远,R[i],向右被整除的最远,
这个直接搞是会超时的,我们用pre[a[i]]表示,前一次出现a[i]出现的位置,如果R[pre[a[i]]] >= i ,就不用求 L[i],R[i] 了
然后用线段树维护区间最小值,最小值的个数,最小值出现的id(记录一个就好)
对于 ql,qr询问,查找 区间最小值的下标id,如果 L[id] <= ql && R[id] >= qr,说明这个最小值可以整除区间所有数
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<ctime>
#include<bitset>
#define LL long long
#define INF 89898989
#define mod 1000000007
#define maxn 100010
#define pi acos(-1.0)
#define eps 1e-6
using namespace std;
struct node
{
int Min,num,id;
}tree[maxn*3] ;
int a[maxn],ql,qr ;
int L[maxn],R[maxn] ;
map<int,int>pre;
void build(int L,int R,int o )
{
if(L==R)
{
tree[o].Min = a[L] ;
tree[o].num = 1 ;
tree[o].id = L ;
return ;
}
int mid=(L+R)>>1;
build(L,mid,o<<1) ;
build(mid+1,R,o<<1|1);
if(tree[o<<1].Min<tree[o<<1|1].Min)
{
tree[o] = tree[o<<1] ;
}
else if(tree[o<<1].Min > tree[o<<1|1].Min)
{
tree[o] = tree[o<<1|1] ;
}
else
{
tree[o].Min = tree[o<<1].Min ;
tree[o].num = tree[o<<1].num+tree[o<<1|1].num ;
tree[o].id = tree[o<<1].id ;
}
}
node find(int L,int R,int o)
{
if(ql <= L && qr >=R)
{
return tree[o] ;
}
int mid=(L+R)>>1 ;
if(qr<= mid) return find(L,mid,o<<1) ;
if(ql > mid) return find(mid+1,R,o<<1|1) ;
node a = find(L,mid,o<<1) ;
node b = find(mid+1,R,o<<1|1) ;
if(a.Min < b.Min) return a ;
else if(a.Min > b.Min) return b ;
a.num += b.num ;
return a ;
}
int main()
{
int i,j,k,n,m;
int x,y;
while(scanf("%d",&n) != EOF)
{
for( i = 1 ; i <= n ;i++)
scanf("%d",&a[i]) ;
build(1,n,1) ;
pre.clear();
for(i = 1 ; i <= n ;i++)
{
if(pre[a[i]])
{
j = pre[a[i]] ;
if(R[j]>=i){
L[i] = L[j] ;
R[i] = R[j] ;
pre[a[i]] = i ;
continue ;
}
}
j = i+1;
while(j<=n && a[j]%a[i]==0)j++;
R[i] = j-1;
j = i-1;
while(j>=1&&a[j]%a[i]==0)j--;
L[i] = j+1;
pre[a[i]] = i ;
}
node ans;
int num ;
cin >> m ;
for( i = 1 ; i <= m;i++)
{
scanf("%d%d",&ql,&qr) ;
ans=find(1,n,1) ;
x = L[ans.id] ;
y = R[ans.id] ;
if(x <= ql && y >= qr)
{
num = qr-ql+1-ans.num;
}
else num = qr-ql+1;
printf("%d\n",num);
}
}
return 0 ;
}