首页 > ACM题库 > HDU-杭电 > HDU 4630-No Pain No Game-线段树-[解题报告]HOJ
2015
09-17

HDU 4630-No Pain No Game-线段树-[解题报告]HOJ

No Pain No Game

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 474    Accepted Submission(s): 187



Problem Description
Life is a game,and you lose it,so you suicide.
But you can not kill yourself before you solve this problem:
Given you a sequence of number a1, a2, …, an.They are also a permutation of 1…n.
You need to answer some queries,each with the following format:
If we chose two number a,b (shouldn’t be the same) from interval [l, r],what is the maximum gcd(a, b)? If there’s no way to choose two distinct number(l=r) then the answer is zero.
 


Input
First line contains a number T(T <= 5),denote the number of test cases.
Then follow T test cases.
For each test cases,the first line contains a number n(1 <= n <= 50000).
The second line contains n number a1, a2, …, an.
The third line contains a number Q(1 <= Q <= 50000) denoting the number of queries.
Then Q lines follows,each lines contains two integer l, r(1 <= l <= r <= n),denote a query.
 


Output
For each test cases,for each query print the answer in one line.
 


Sample Input
1 10 8 2 4 9 5 7 10 6 1 3 5 2 10 2 4 6 9 1 4 7 10
 


Sample Output
5 2 2 4 3
 


Source
 


Recommend
zhuyuanchen520
 
题意: 有N个数, 是 1~N的一个排列。有M个询问, 每次询问一个区间, 问从这个区间中,取两个数的最大的最大公约数。
思路: 离线处理 + 线段树(点更新)
在一个区间内, 如果一个因子出现两次,该因子就肯定是该区间的一个解。 而出现两次并且是最大。 就是该区间的最优解。
首先要求出每个数对应的因子存起来。
然后对每次询问存起来。(离线处理)并按右区间从小到大排序。
Last[V] 数组是表示的是 因子V在上次出现的位置。
然后对每个数因子对线段树进行更新。 
更新的位置是更新在上次出现该因子的位置上。 因为是以该数的因子为第二次出现的。
4
8 2 4 9
3
2 3
1 3
2 4
i = 1时, 线段树的叶子节点[0, 0, 0, 0];
i = 2时, 线段树的叶子节点[2, 0, 0, 0];
i = 3时, 线段树的叶子节点[4, 2, 0, 0]; 遇到两个询问, 输出区间的最大值 Query(2, 3)  = 2  Query(1, 3) = 4;
i = 4时, 线段树的叶子节点[4, 2, 1, 0]; 遇到一个询问, Query(2, 4) = 2;
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
using namespace std;

//线段树 + 离线处理

const int V = 50000 + 5;
typedef struct node {
    int l, r, index;
};
node nod[V];
int sum[V][99], sum_top[V], last[V];
int p[V * 3], first_number_index, ans[V];
int n, m, T;
bool cmp(node a, node b) {
    if(a.r < b.r)
        return true;
    return false;
}
void build(int N) {
    int nn = 1;
    int r = 2;
    while(r < 2 * N) {
        nn++;
        r *= 2;
    }
    first_number_index = 1 << (nn - 1);
    memset(p, 0, sizeof(p));
}
void Update(int index, int num) {
    p[index] = num;
    while(index != 0) {
        index >>= 1;
        p[index] = max(p[index << 1], p[(index << 1) | 1]);
    }
}
int Query(int a, int b) {
    int Max = 0;
    while(a <= b) {
        int temp = 1;
        int index = first_number_index + a - 1;
        while(index % (2 * temp) == 0 && a + (2 * temp) - 1 <= b)
            temp *= 2;
        Max = max(Max, p[index / temp]);
        a += temp;
    }
    return Max;
}
int main() {
    int i, j, k;
    scanf("%d", &T);
    while(T--) {
        scanf("%d", &n);
        build(n);
        memset(sum_top, 0, sizeof(sum_top));
        memset(last, 0, sizeof(last));
        for(i = 1; i <= n; ++i) {
            int temp;
            scanf("%d", &temp);
            for(j = 1; j <= (int) sqrt(temp * 1.0); ++j)
                if(temp % j == 0) {
                    sum[i][sum_top[i]++] = temp / j;
                    if(j * j != temp)
                        sum[i][sum_top[i]++] = j;
                }
        }
        scanf("%d", &m);
        for(i = 0; i < m; ++i) {
            scanf("%d%d", &nod[i].l, &nod[i].r);
            nod[i].index = i;
        }
        int k = 0;
        sort(nod, nod + m, cmp);
        for(i = 1; i <= n && k < m; ++i) {
            for(j = 0; j < sum_top[i]; ++j) {
                int pre = sum[i][j];
                if(last[pre] > 0) {
                    int a = last[pre];
                    int index = first_number_index + a - 1;
                    if(p[index] < pre)
                        Update(index, pre);
                }
                last[pre] = i;
            }
            while(nod[k].r == i) {
                ans[nod[k].index] = Query(nod[k].l, nod[k].r);
                k++;
            }
        }
        for(i = 0; i < m; ++i)
            printf("%d\n", ans[i]);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/dongdongzhang_/article/details/9670039