| ||||||||||
| ||||||||||
Rabbit KingdomTime Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2615 Accepted Submission(s): 884
Problem Description
Long long ago, there was an ancient rabbit kingdom in the forest. Every rabbit in this kingdom was not cute but totally pugnacious, so the kingdom was in chaos in season and out of season.
n rabbits were numbered form 1 to n. All rabbits' weight is an integer. For some unknown reason, two rabbits would fight each other if and only if their weight is NOT co-prime. Now the king had arranged the n rabbits in a line ordered by their numbers. The king planned to send some rabbits into prison. He wanted to know that, if he sent all rabbits between the i-th one and the j-th one(including the i-th one and the j-th one) into prison, how many rabbits in the prison would not fight with others. Please note that a rabbit would not fight with himself.
Input
The input consists of several test cases.
The first line of each test case contains two integer n, m, indicating the number of rabbits and the queries. The following line contains n integers, and the i-th integer W i indicates the weight of the i-th rabbit. Then m lines follow. Each line represents a query. It contains two integers L and R, meaning the king wanted to ask about the situation that if he sent all rabbits from the L-th one to the R-th one into prison. (1 <= n, m, W i <= 200000, 1 <= L <= R <= n) The input ends with n = 0 and m = 0.
Output
For every query, output one line indicating the answer.
Sample Input
Sample Output
Source
Recommend
| ||||||||||
|
题目大意:有n个数,m个询问,询问求的是一个区间内与其他数都互质的数有多少个,比如一个序列:1 3 6 7 5 15 答案为2个(1、7)。
题目思路:我们可以预处理一下每个数能到达的最左的位置和最右的位置,对于每个询问,离线一下,按右端点排序。
我们考虑不满足的情况:
①对于每个点p<=qr,我们只需要知道其左区间是否在询问的区间内即可(该点不满足)。我们先执行L++;
②对于每个点p.r<=qr,那么他的右区间肯定在询问的区间左边,使得不满足,但是要抵消左边的情况,否则会多算一次,所以我们执行,p.pos++,L--;
然后对于每个询问用树状数组维护单点更新区间和即可。
include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
#define lowbit(x) x&-x
int prime[maxn],tot,pp[maxn],arr[maxn],L[maxn],R[maxn],facnum[maxn],sum[maxn],res[maxn];
bool vis[maxn];
vector<int>vec[maxn];
vector<int>factor[maxn];
struct Node
{
int index,l,r;
Node(int _index=0,int _l=0,int _r=0):index(_index),l(_l),r(_r){}
}ans[maxn];
bool cmp(const Node& a,const Node& b)
{
return a.r<b.r;
}
void Getprime()
{
tot=0;
memset(vis,false,sizeof(vis));
for(int i=2;i<maxn;i++)
{
if(!vis[i]) prime[tot++]=i;
for(int j=0;j<tot&&i*prime[j]<maxn;j++)
{
vis[i*prime[j]]=true;
if(i%prime[j]==0) break;
}
}
}
void Init(int n)
{
memset(pp,0,sizeof(pp));
memset(sum,0,sizeof(sum));
for(int i=0;i<=n;i++) L[i]=0,R[i]=n+1,factor[i].clear(),vec[i].clear();
}
void Divide(int p,int x)
{
facnum[p]=0;
for(int i=0;prime[i]*prime[i]<=x;i++)
{
if(x%prime[i]==0)
{
factor[p].push_back(prime[i]);
facnum[p]++;
while(x%prime[i]==0) x/=prime[i];
}
}
if(x>1) factor[p].push_back(x),facnum[p]++;
}
void Update(int p,int val)
{
if(p<=0) return;
while(p<maxn)
{
sum[p]+=val;
p+=lowbit(p);
}
}
int Query(int p)
{
int ans=0;
while(p>0)
{
ans+=sum[p];
p-=lowbit(p);
}
return ans;
}
int main()
{
int n,m;
Getprime();
while(~scanf("%d%d",&n,&m)&&(n||m))
{
Init(n);
for(int i=1;i<=n;i++)
{
scanf("%d",&arr[i]);
Divide(i,arr[i]);
for(int j=0;j<facnum[i];j++)
{
L[i]=max(L[i],pp[factor[i][j]]);//求一个数最左能到达哪
pp[factor[i][j]]=i;
}
}
for(int i=0;i<maxn;i++) pp[i]=n+1;
for(int i=n;i>=1;i--)
{
for(int j=0;j<facnum[i];j++)
{
R[i]=min(R[i],pp[factor[i][j]]);//同理
pp[factor[i][j]]=i;
}
}
for(int i=1;i<=n;i++) vec[R[i]].push_back(i);//用vector存取右端点到R【i】结束的点集
for(int i=1;i<=m;i++)
{
scanf("%d%d",&ans[i].l,&ans[i].r);
ans[i].index=i;
}
sort(ans+1,ans+1+m,cmp);
int now=1;
for(int i=1;i<=m;i++)
{
while(now<=n&&now<=ans[i].r)
{
Update(L[now],1);
for(int j=0;j<vec[now].size();j++)
{
int id=vec[now][j];
Update(L[id],-1);
Update(id,1);
}
now++;
}
res[ans[i].index]=Query(ans[i].r)-Query(ans[i].l-1);
res[ans[i].index]=ans[i].r-ans[i].l+1-res[ans[i].index];
}
for(int i=1;i<=m;i++) printf("%d\n",res[i]);
}
}