HDU 4630 No Pain No Game

本文详细解析了HDU 4630 'No Pain No Game'的题目,深入探讨了解题思路和算法实现,帮助读者克服难关。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 a 1, a 2, ..., a n.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 a 1, a 2, ..., a n.
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]);
    }
}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值