HDU 4630 No Pain No Game

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]);
    }
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章