hdu 5317 RGCDQ 筛法+线段树解法

RGCDQ

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 299    Accepted Submission(s): 151


Problem Description
Mr. Hdu is interested in Greatest Common Divisor (GCD). He wants to find more and more interesting things about GCD. Today He comes up with Range Greatest Common Divisor Query (RGCDQ). What’s RGCDQ? Please let me explain it to you gradually. For a positive integer x, F(x) indicates the number of kind of prime factor of x. For example F(2)=1. F(10)=2, because 10=2*5. F(12)=2, because 12=2*2*3, there are two kinds of prime factor. For each query, we will get an interval [L, R], Hdu wants to know  maxGCD(F(i),F(j))  (Li<jR)
 

Input
There are multiple queries. In the first line of the input file there is an integer T indicates the number of queries.
In the next T lines, each line contains L, R which is mentioned above.

All input items are integers.
1<= T <= 1000000
2<=L < R<=1000000
 

Output
For each query,output the answer in a single line. 
See the sample for more details.
 

Sample Input
 
   
2 2 3 3 5
 

Sample Output
 
   
1 1
 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5317

题目大意:F(i)表示i的素因子数,求一个区间的[L,R]中,任取两个数的GCD(F[i],F[j])最大值。

思路:比较简单,先筛法打表,将F[i]的值求出,因为八个最小素数相乘即可达到上限,所以F[i]的值很小,再求出每个F[i]的所有因数。之后就是维护一下RMQ就行了。此题可以不用线段树,线段树的部分仅用于求RMQ。

AC代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long LL;
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#define y1 y234
#define MAXN 1000010 // 1e6
int t;
bool vis[MAXN];
int F[MAXN], ans[MAXN], last[MAXN];
int tree[MAXN * 2 + 100000];
struct node {
    int id, L, R;
    node(int L, int R, int id) :L(L), R(R), id(id) {}
    bool operator < (const node &w) const {
        return R < w.R;
    }
};
vector Q;
vector Fyinshu[MAXN];
void init() {
    vis[0] = vis[1] = true;
    for(int i = 2; i <= 1000000; i++) {
        if(!vis[i]) {
            F[i]++;
            for(int j = i + i; j <= 1000000; j += i) {
                vis[j] = 1;
                F[j]++;
            }
        }
    }
}
void Pushup(int rt) {
    tree[rt] = max(tree[rt << 1], tree[rt << 1 | 1]);
}
void Update(int num, int pos, int l, int r, int rt) {
    if(l == r) {
        tree[rt] = max(tree[rt], num);
        return;
    }
    int mid = (l + r) >> 1;
    if(pos <= mid) Update(num, pos, lson);
    else Update(num, pos, rson);
    Pushup(rt);
}
int Query(int st, int ed, int l, int r, int rt) {
    int res = 0;
    int mid = (l + r) >> 1;
    if(st <= l && r <= ed) {
        return tree[rt];
    }
    if(st <= mid) res = max(res, Query(st, ed, lson));
    if(ed > mid) res = max(res, Query(st, ed, rson));
    return res;
}
int main() {
    init();
    scanf("%d", &t);
    int st, ed, maxn = -1;
    for(int i = 1; i <= t; i++) {
        scanf("%d%d", &st, &ed);
        Q.push_back(node(st, ed, i));
        maxn = max(maxn, ed);
    }
    sort(Q.begin(), Q.end());
    for(int i = 1; i <= maxn; i++) {
        int tmp = F[i];
        Fyinshu[tmp].clear();
        for(int j = 1; j * j <= tmp; j++) {
            if(tmp % j != 0) continue;
            Fyinshu[tmp].push_back(j);
            if(j * j != tmp) Fyinshu[tmp].push_back(tmp / j);
        }
    }
    int q_cnt = 0;
    for(int i = 1; i <= maxn && q_cnt < t; i++) {
        int len = Fyinshu[F[i]].size();
        for(int j = 0; j < len; j++) {
            int tmp = Fyinshu[F[i]][j];
            if(last[tmp] != 0) {
                Update(tmp, last[tmp], 1, maxn, 1);
            }
            last[tmp] = i;
        }
        for(; i == Q[q_cnt].R; q_cnt++) {
            if(Q[q_cnt].L == Q[q_cnt].R) ans[Q[q_cnt].id] = 0;
            ans[Q[q_cnt].id] = Query(Q[q_cnt].L, Q[q_cnt].R, 1, maxn, 1);
        }
    }
    for(int i = 1; i <= t; i++)
        printf("%d\n", ans[i]);
    return 0;
}

你可能感兴趣的:(线段树,数据结构)