hdu4777 Rabbit Kingdom

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


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] .
 
这题可以用树状数组做,卡了我好长时间= 。= 
题意:给定一个区间,问这个区间中和其他数都互质的数的个数。 
思路:我们可以先预处理出每一个数的有效范围,即向左或向右做多到达哪个位置,使得这些数都不互质。然后处理出以j为左边界的点的所有坐标,用vector存起来。把询问按左端点从小到大排序,然后离线处理每一个点i(1<=i<=n),把以i为左边界的坐标都更新,坐标加1,这个坐标的(右边界+1)减1,然后把以i为左边界的询问都处理完。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define inf 0x7fffffff
#define maxn 200060
struct node1{
    int idx,l,r,ans;
}qs[maxn];
struct node{
    int l,r,w,idx;
}a[maxn];
int rt[maxn],lt[maxn];


bool cmp(node1 a,node1 b){
    if(a.l==b.l)return a.r<b.r;
    return a.l<b.l;
}

bool cmp1(node1 a,node1 b){
    return a.idx<b.idx;
}

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

int b[maxn+10],c[maxn+10];

vector<int>vec[maxn];
vector<int>::iterator it;

int lowbit(int x){
    return x&(-x);
}
void update(int pos,int num)
{
    c[pos]+=num;
    while(pos<maxn){
        b[pos]+=num;
        pos+=lowbit(pos);
    }

}
int getsum(int pos)
{
    int num=0;
    while(pos>0){
        num+=b[pos];
        pos-=lowbit(pos);
    }
    return num;
}





int main()
{
    int n,m,i,j,T,x;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0 && m==0)break;
        for(i=1;i<=n;i++){
            scanf("%d",&a[i].w);
            a[i].l=1;
            a[i].r=n;
            a[i].idx=i;
            vec[i].clear();
        }
        for(i=1;i<=m;i++){
            scanf("%d%d",&qs[i].l,&qs[i].r);
            qs[i].idx=i;
        }
        for(i=1;i<=200010;i++){
            rt[i]=0;
            lt[i]=n+1;
        }


        for(i=1;i<=n;i++){
            if(a[i].w==1){
                a[i].l=1;
                vec[1].push_back(i);
                continue;
            }
            x=a[i].w;
            for(j=2;j*j<=x;j++){
                if(x%j==0){
                    a[i].l=max(a[i].l,rt[j]+1);
                    rt[j]=i;
                }
                while(x%j==0){
                    x/=j;
                }
            }
            if(x!=1){
                a[i].l=max(a[i].l,rt[x]+1);
                rt[x]=i;
            }
            vec[a[i].l ].push_back(i);
        }

        for(i=n;i>=1;i--){
            if(a[i].w==1){
                continue;
            }
            x=a[i].w;
            for(j=2;j*j<=x;j++){
                if(x%j==0){
                    a[i].r=min(a[i].r,lt[j]-1);
                    lt[j]=i;
                }
                while(x%j==0){
                    x/=j;
                }
            }
            if(x!=1){
                a[i].r=min(a[i].r,lt[x]-1);
                lt[x]=i;
            }
        }

        sort(qs+1,qs+1+m,cmp);


        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        int t=1;
        for(i=1;i<=n;i++){
            for(j=0;j<vec[i].size();j++){
                update(vec[i][j],1  );
                update(a[vec[i][j]  ].r+1,-1  );
            }
            while(t<=m && qs[t].l==i){
                qs[t].ans=getsum(qs[t].r)-getsum(qs[t].l-1);
                t++;
            }
            update(i,-1);
            update(a[i].r+1,1 );
        }

        sort(qs+1,qs+1+m,cmp1);
        for(i=1;i<=m;i++){
            printf("%d\n",qs[i].ans);
        }
    }
    return 0;
}



posted @ 2015-11-11 15:07  Herumw  阅读(231)  评论(0编辑  收藏  举报