# HDU 4777 Rabbit Kingdom

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>

#include<queue>
using namespace std;

const int maxn=2e5+10;
int w[maxn],n,q;
struct X
{
int x,y,ans,id;
} s[maxn];

struct OP
{
int x,y1,y2;

bool com[maxn];
int primes, prime[maxn],L[maxn],R[maxn];
int pre[maxn];
int c[maxn],hh;

vector<int>Prime[maxn];

int lowbit(int x) { return x&(-x); }
void ADD(int p, int val) { while (p <= n) c[p] = c[p] + val, p = p + lowbit(p); }
int sum(int p) { int r = 0; while (p > 0) r = r + c[p], p = p - lowbit(p); return r; }
void update(int L, int R, int val) { ADD(L, val); ADD(R + 1, -val); }

void solve()
{
primes = 0;
memset(com,false,sizeof(com));
com[0] = com[1] = true;
for (int i = 2; i < maxn; ++i)
{
if (!com[i])
{
prime[++primes] = i;
}
for (int j = 1; j <= primes && i*prime[j] < maxn; ++j)
{
com[i*prime[j]] = true;
if (!(i % prime[j]))
break;
}
}

for(int i=2;i<=200000;i++)
{
int tp = i;
for(int j=1;j<=primes&&tp!=1;j++)
{
if(tp%prime[j]) continue;

Prime[i].push_back(prime[j]);

while(!(tp%prime[j])) tp /= prime[j];

if(!com[tp]&&tp>1)
{
Prime[i].push_back(tp);
break;
}
}
}
}

bool cmp1(X a,X b) { return a.x<b.x; }
bool cmp2(OP a,OP b) { return a.x<b.x; }
bool cmp3(X a,X b) { return a.id<b.id; }

int main()
{
solve();

while(~scanf("%d%d",&n,&q))
{
if(n==0&&q==0) break;
for(int i=1; i<=n; i++) scanf("%d",&w[i]);

for(int i=1; i<=q; i++) scanf("%d%d",&s[i].x,&s[i].y), s[i].id=i;
sort(s+1,s+1+q,cmp1);

for(int i=1;i<=200000;i++) pre[i]=0;
for(int i=1;i<=n;i++)
{
if(w[i]==1) L[i]=1;
else
{
L[i]=0;
for(int j=0;j<Prime[w[i]].size();j++)
{
L[i]=max(L[i],pre[Prime[w[i]][j]]+1);
pre[Prime[w[i]][j]]=i;
}
}
}

for(int i=1;i<=200000;i++) pre[i]=n+1;
for(int i=n;i>=1;i--)
{
if(w[i]==1) R[i]=n;
else
{
R[i]=n+1;
for(int j=0;j<Prime[w[i]].size();j++)
{
R[i]=min(R[i],pre[Prime[w[i]][j]]-1);
pre[Prime[w[i]][j]]=i;
}
}
}

int sz1=0,sz2=0;
for(int i=1;i<=n;i++)
{
del[sz2].x=i; del[sz2].y1=i;  del[sz2].y2=R[i];  sz2++;
}

sort(del,del+sz2,cmp2);

memset(c,0,sizeof c);

for(int i=1;i<=n;i++)
{
{
}
while(idq<=q&&s[idq].x==i)
{
s[idq].ans=sum(s[idq].y);
idq++;
}
while(iddel<sz2&&del[iddel].x==i)
{
update(del[iddel].y1,del[iddel].y2,-1);
iddel++;
}
}

sort(s+1,s+1+q,cmp3);

for(int i=1;i<=q;i++) printf("%d\n",s[i].ans);

}
return 0;
}

posted @ 2016-10-29 22:46 Fighting_Heart 阅读(...) 评论(...) 编辑 收藏