HDU 4777 Rabbit Kingdom 【树状数组】

 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

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] .
#include<bits/stdc++.h>
using namespace std;
const int MAX = 2e5 + 7;
int c[MAX], num[MAX], aaannnsss[MAX];
int pre[MAX];
int n, m;
int lowbit(int x){
    return x & -x;
}
void add(int pos, int x){
    if(pos == 0) return;
    while(pos <= MAX){
        c[pos] += x;
        pos += lowbit(pos);
    }
}
int query(int pos){
    int ans = 0;
    while(pos){
        ans += c[pos];
        pos -= lowbit(pos);
    }
    return ans;
}
struct node{
    int x, y, id;
    bool operator < (const node &a) const{
        return y == a.y ? x < a.x : y < a.y;
    }
} ans[MAX], ass[MAX], a[MAX];
vector <int> v[MAX];
void init(){
    for(int i = 0; i < MAX; i++) v[i].clear();
    for(int i = 2; i < MAX; i++) if(v[i].size() == 0)
        for(int j = i; j < MAX; j += i) v[j].push_back(i);
}
void iiinit(){
    for(int i = 1; i <= n; i++) ans[i].x = 0, ans[i].y = n + 1, ans[i].id = i;
    memset(pre, 0, sizeof pre);
    for(int i = 1; i <= n; i++){
        int flag = num[i];
        for(int j = 0; j < v[flag].size(); j++){
            int x = v[flag][j];
            ans[i].x = max(ans[i].x, pre[x]);
            ans[pre[x]].y = min(ans[pre[x]].y, i);
            pre[x] = i;
        }
    }
}
int main(){
    init();
    while(scanf("%d%d", &n, &m) != EOF && (n + m)){
        for(int i = 1; i <= n; i++) scanf("%d", &num[i]);
        for(int i = 1; i <= m; i++){
            scanf("%d%d", &a[i].x, &a[i].y);
            a[i].id = i;
        }
        iiinit();
        for(int i = 1; i <= n; i++) ass[i] = ans[i];
        memset(c, 0, sizeof c);
        sort(a + 1, a + m + 1);
        sort(ans + 1, ans + n + 1);
        int j = 1, k = 1;
        for(int i = 1; i <= m; i++){
            int x = a[i].x, y = a[i].y;
            while(ass[j].id <= y && j <= n){
                add(ass[j].id, 1);
                add(ass[j].x, -1);
                j++;
            }
            while(ans[k].y <= y && k <= n){
                add(ans[k].id, -1);
                add(ans[k].x, 1);
                k++;
            }
            aaannnsss[a[i].id] = query(y) - query(x - 1);
        }
        for(int i = 1; i <= m; i++) printf("%d\n", aaannnsss[i]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Head_Hard/article/details/82153304