HDU 4777 Rabbit Kingdom (2013杭州赛区1008题,预处理,树状数组)

Rabbit Kingdom

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 40    Accepted Submission(s): 20


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 Wi 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, Wi <= 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
3 2 2 1 4 1 2 1 3 6 4 3 6 1 2 5 3 1 3 4 6 4 4 2 6 0 0
 

 

Sample Output
2 1 1 3 1 2
Hint
  In the second case, the answer of the 4-th query is 2, because only 1 and 5 is co-prime with other numbers in the interval [2,6] .
 

 

Source
 
思路: 这道题刚开始看的时候完全没有思路。。 后来在网上看了各大神的解题思路,才逐渐理解做法。
 
第一步是先预处理,处理出来a[i]前后互质的区间范围, 这部分可以先求出来素数,然后枚举素数得到每个数的质因数,之后再前后扫描就可以得出来互质的范围。
 
第二步是处理询问的东西, 这部分的思路比较巧妙。 做法: 对于询问按照l递增排序; 从0开始枚举N+1个数(0……N), 当当前枚举的结点达到了某个互质区间的范围我们就这样处理把这个互质区间数的位置+1,右端点-1, 在当前情况下, 因为我们的询问是按照l递增排序的, 假设数的数值是k,左端点为l,右端点为r, 询问的区间只可能是
这样的情况都是满足题意得。 当我们枚举的范围要超过k时, 需要把右端点的值+1。
#include<bits/stdc++.h>
#define N 200010
using namespace std;
struct node
{
    int l,r,id;
};

int prime[N],w[N],b[N],l[N],r[N],ans[N],tree[N];
node q[N];
vector<int> factor[N],v[N];
int n,m;

void get_prime()
{
    for (int i=2;i<N;i++)
    {
        if (!prime[i]) prime[++prime[0]]=i;
        for (int j=1;j<=prime[0]&&prime[j]*i<N;j++)
        {
            prime[prime[j]*i]=1;
            if (i%prime[j]==0) break;
        }
    }
}

void getfactors(int k)
{
    factor[k].clear();
    int x=w[k];
    for (int i=1;prime[i]*prime[i]<=x;i++)
        if (x%prime[i]==0)
    {
        factor[k].push_back(prime[i]);
        while(x%prime[i]==0) x/=prime[i];
    }
    if (x!=1) factor[k].push_back(x);
}

bool cmp(node a, node b)
{
    return a.l<b.l;
}

int lowbit(int a)
{
    return (a)&(-a);
}

int sum(int a)
{
    int s=0;
    while (a>0)
    {
        s+=tree[a];
        a-=lowbit(a);
    }
    return s;
}

void update(int a,int b)
{
    while (a<=n)
    {
        tree[a]+=b;
        a+=lowbit(a);
    }
}

int main()
{
    get_prime();
    while(scanf("%d%d",&n,&m)!=EOF&&n!=0&&m!=0)
    {
        for (int i=0;i<=n;i++) v[i].clear();
        for (int i=0;i<=n;i++) tree[i]=0;

        for (int i=1;i<=n;i++) scanf("%d",&w[i]);
        for (int i=1;i<=m;i++)
        {
            scanf("%d%d",&q[i].l,&q[i].r);
            q[i].id=i;
        }
        sort(q+1,q+m+1,cmp);

        for (int i=1;i<=n;i++) getfactors(i);
        for (int i=1;i<N;i++) b[i]=0;
        for (int i=1;i<=n;i++)
        {
            int tmp=0;
            for (int j=0;j<(int)factor[i].size();j++)
            {
                tmp=max(tmp,b[factor[i][j]]);
                b[factor[i][j]]=i;
            }
            l[i]=tmp;
            v[l[i]].push_back(i);
        }
        for (int i=1;i<N;i++) b[i]=n+1;
        for (int i=n;i>=1;i--)
        {
            int tmp=n+1;
            for (int j=0;j<(int)factor[i].size();j++)
            {
                tmp=min(tmp,b[factor[i][j]]);
                b[factor[i][j]]=i;
            }
            if (tmp==n+1) r[i]=0; else r[i]=tmp;
        }

        int cur=1;
        for (int i=0;i<=n;i++)
        {
            while(cur<=m&&q[cur].l==i)
            {
                ans[q[cur].id]=sum(q[cur].r)-sum(q[cur].l-1);
                cur++;
            }
            for (int j=0;j<v[i].size();j++)
            {
                update(v[i][j],1);
                if (r[v[i][j]]!=0) update(r[v[i][j]],-1);
            }
            if (r[i]!=0) update(r[i],1);
        }
        for (int i=1;i<=m;i++) printf("%d\n",ans[i]);
    }
    return 0;
}

  

代码:
 
 

posted on 2015-10-03 22:18  wzb_hust  阅读(102)  评论(0编辑  收藏  举报

导航