很脑残的写了主席树,其实树状数组就能解决
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+10;
inline void read(int &x){char ch;bool ok;
for(ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if(ch=='-') ok=1;
for(x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());if(ok) x=-x;}
int A[N],B[N];
struct HisTree{
int l,r,sum;
}T[N*110];
int R[N],cnt;
void build(int l,int r,int &x,int y,int pos,int val){
T[++cnt]=T[y],T[cnt].sum+=val,x=cnt;
if(l==r)return;
int mid=(l+r)/2;
if(pos<=mid)build(l,mid,T[x].l,T[y].l,pos,val);
else build(mid+1,r,T[x].r,T[y].r,pos,val);
}
int ask(int l,int r,int x,int y,int L,int R){
if(l>=L&&r<=R)return T[y].sum-T[x].sum;
int mid=(r+l)/2;int val=0;
if(L<=mid)val+=ask(l,mid,T[x].l,T[y].l,L,R);
if(R>mid)val+=ask(mid+1,r,T[x].r,T[y].r,L,R);
return val;
}
int main(){
int n,m;cin>>n>>m;
for(int i=1;i<=n;++i)read(A[i]),B[A[i]]=i;
for(int i=1;i<=n;++i){
build(1,n,R[i],R[i-1],i,1ll*0);
for(int j=1;j*j<=A[i];++j){
if(A[i]%j==0){
int tmp;
if(B[j]<i)tmp=R[i],build(1,n,R[i],tmp,B[j],1ll*1);
if(A[i]!=j*j&&B[A[i]/j]<i)tmp=R[i],build(1,n,R[i],tmp,B[A[i]/j],1ll*1);
}
}
for(int j=2;j*A[i]<=n;++j){
if(B[A[i]*j]<i){
int tmp=R[i];
build(1,n,R[i],tmp,B[A[i]*j],1ll*1);
}
}
}
while(m--){
int l,r;read(l);read(r);
printf("%d\n",ask(1,n,R[l-1],R[r],l,r));
}
}