Yaroslav has an array p = p1, p2, ..., pn (1 ≤ pi ≤ n), consisting of n distinct integers. Also, he has m queries:
- Query number i is represented as a pair of integers li, ri (1 ≤ li ≤ ri ≤ n).
- The answer to the query li, ri is the number of pairs of integers q, w (li ≤ q, w ≤ ri) such that pq is the divisor of pw.
Help Yaroslav, answer all his queries.
The first line contains the integers n and m (1 ≤ n, m ≤ 2·105). The second line contains n distinct integers p1, p2, ..., pn (1 ≤ pi ≤ n). The following m lines contain Yaroslav's queries. The i-th line contains integers li, ri (1 ≤ li ≤ ri ≤ n).
Print m integers — the answers to Yaroslav's queries in the order they appear in the input.
Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
1 1 1 1 1
1
10 9 1 2 3 4 5 6 7 8 9 10 1 10 2 9 3 8 4 7 5 6 2 2 9 10 5 10 4 10
27 14 8 4 2 1 2 7 9
#include<bits/stdc++.h>
#define LL long long
#define N 210000
using namespace std;
struct BinaryIndexedTree
{
LL c[N],n;
inline void init()
{
memset(c,0,sizeof(c));
}
inline int lowbit(int x)
{
return x&-x;
}
inline void update(int x,int k)
{
while (x<=n)
{
c[x]+=k;
x+=lowbit(x);
}
}
inline LL getsum(int x)
{
LL ans=0;
while (x>0)
{
ans+=c[x];
x-=lowbit(x);
}
return ans;
}
} BIT;
struct query
{
int l,r,id;
} q1[N],q2[N];
int n,m,a[N],pos[N];
LL ans[N];
bool cmp1(query a,query b)
{
return a.l==b.l ? a.r<b.r : a.l<b.l;
}
bool cmp2(query a,query b)
{
return a.r==b.r ? a.l<b.l : a.r<b.r;
}
int main()
{
while (~scanf("%d%d",&n,&m))
{
BIT.n=n; BIT.init();
memset(ans,0,sizeof(ans));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
pos[a[i]]=i;
}
for(int i=1;i<=m;i++)
{
scanf("%d%d",&q1[i].l,&q1[i].r);
q2[i]=q1[i];q1[i].id=q2[i].id=i;
}
int i,j,k;
sort(q1+1,q1+1+m,cmp1); //提前把询问按照端点排序,使得找起来方便
sort(q2+1,q2+1+m,cmp2); //一个是左端点优先,一个是右端点优先
for(i=1,j=1,k=1;i<=n;i++)
{
for(;q1[j].l==i&&j<=m;j++) //找左端点为i的询问,由于排序了所以依次遍历即可
ans[q1[j].id]-=BIT.getsum(q1[j].r)-BIT.getsum(q1[j].l-1); //计算y
for(int p=1;a[i]*p<=n;p++)
BIT.update(pos[a[i]*p],1); //把位置i的数字的倍数加入sum中
for(;q2[k].r==i&&k<=m;k++) //找右端点为i的询问
ans[q2[k].id]+=BIT.getsum(q2[k].r)-BIT.getsum(q2[k].l-1); //计算x
}
for(int i=1;i<=m;i++)
printf("%I64d\n",ans[i]);
}
return 0;
}