https://nanti.jisuanke.com/t/41391
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<stack>
#include<cstring>
#include<set>
#include<iterator>
#include<list>
#include<deque>
#include<queue>
#include<map>
#include<cmath>
#include<sstream>
#include<cstdio>
#include<ctime>
#include<iomanip>
//#include<unordered_set>
using namespace std;
#define lowbit(x) x&(-x)
typedef long long ll;
typedef pair<int, int> P;
const int N = 1e5 + 10;
const int M = 1e5 + 10;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
vector<int> g[N];
vector<P> query[N];
ll c[N];
int n, m;
void update(int x)
{
for (int i = x; i <= n; i += lowbit(i))
{
c[i]++;
}
}
int ask(int x)
{
int sum = 0;
for (int i = x; i; i -= lowbit(i))
{
sum += c[i];
}
return sum;
}
int a[N], pos[N];
ll ans[N];
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
pos[a[i]] = i;
}
for (int i = 1; i <= n; i++)
{
for (int j = i + i; j <= n; j += i)
{
int p = pos[i];
int q = pos[j];
if (p > q)
swap(p, q);
g[q].push_back(p);
}
}
for (int i = 1; i <= m; i++)
{
int l, r;
scanf("%d%d", &l, &r);
query[r].push_back(P(l, i));
}
for (int i = 1; i <= n; i++)
{
for (int j = 0; j < g[i].size(); j++)
{
update(g[i][j]);
}
for (int j = 0; j < query[i].size(); j++)
{
P p = query[i][j];
ans[p.second] = ask(i) - ask(p.first - 1);
}
}
for (int i = 1; i <= m; i++)
{
printf("%lld\n", ans[i]);
}
return 0;
}