离线树状数组搞一搞。
#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define ull unsigned long long
using namespace std; const int N = 2e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = ;
const int Mod = 1e9 + ; int n, m, a[N], ans[N], L[N];
bool vis[N], is[N], ok[N];
vector<PII> v[N];
vector<int> p[N];
map<int,int> mp; struct BIT {
int a[N];
void init() {
for(int i = ; i <= n; i++) a[i] = ;
}
void modify(int x, int val) {
for(int i = x; i <= n; i += i & -i)
a[i] += val;
}
int sum(int x) {
int ans = ;
for(int i = x; i; i -= i & -i)
ans += a[i];
return ans;
}
int query(int l, int r) {
return sum(r) - sum(l - );
}
} b, c; int main() {
for(int i = ; i < N; i++) {
if(is[i]) continue;
for(int j = i; j < N; j += i) {
is[j] = true;
p[j].push_back(i);
}
}
while(scanf("%d%d", &n, &m)!= EOF && (n || m)) {
b.init(); mp.clear(); c.init();
for(int i = ; i <= n; i++) {
v[i].clear();
vis[i] = ok[i] = true;
scanf("%d", &a[i]);
b.modify(i, );
L[i] = inf;
}
for(int i = ; i <= m; i++) {
int l, r;
scanf("%d%d", &l, &r);
v[l].push_back(mk(r, i));
}
for(int i = n; i >= ; i--) {
for(int t : p[a[i]]) {
if(mp.find(t) != mp.end()) {
int pos = mp[t];
L[i] = min(L[i], pos);
if(vis[pos]) {
vis[pos] = false;
b.modify(pos, -);
} else if(ok[pos] && L[pos] != inf){
ok[pos] = false;
c.modify(pos, -);
c.modify(L[pos], );
}
}
mp[t] = i;
}
if(L[i] != inf) {
vis[i] = false;
b.modify(i, -);
c.modify(i, );
c.modify(L[i], -);
}
for(PII t : v[i]) {
ans[t.se] = b.query(i, t.fi) + c.sum(t.fi);
}
}
for(int i = ; i <= m; i++) printf("%d\n", ans[i]);
}
return ;
} /**/