hdu-4630-No Pain No Game-(树状数组,离线处理)

本文介绍了一种解决区间最大公约数(GCD)查询问题的高效算法。通过使用树状数组来存储和更新区间内所有数的最大约数,实现快速查询区间内任意两数最大GCD。提供了一个具体示例及完整的C++代码实现。

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

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[sub]1[/sub], a[sub]2[/sub], ..., a[sub]n[/sub]. 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

参考大神kuangbin的文章:http://www.cnblogs.com/kuangbin/archive/2013/07/30/3225627.html

题目给出n个数的序列,这个序列是1~n的一个全排列,然后查询是给出一个区间[l,r],让求出区间内任意两个数的最大公约数中的最大值。

让求区间内的gcd的最大值,可以求出区间内的所有数的约数,而那个最大的且是至少两个数的公约数的数就是这个区间的最大gcd,可以用树状数组存放

代码:

//tree[i]是i处的最大约数
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
#define M 50005
int n;
int tree[M];
int a[M],b[M];
int ans[M];
struct node {
    int l,r,id;
    bool operator < (const node &obj) const
    {
        return l>obj.l;
    }
}p[M];
inline int lowbit(int i)
{
    return i&(-i);
}
void add(int i,int v)
{
    while(i<=n)
    {
        tree[i]=max(tree[i],v);
        i+=lowbit(i);
    }
}
int sum(int i)
{
    int res=-1;
    while(i>0)
    {
        res=max(res,tree[i]);
        i-=lowbit(i);
    }
    return res;
}
int main()
{
    int T,i,j,m;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        scanf("%d",&m);
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&p[i].l,&p[i].r);
            p[i].id=i;
        }
        sort(p+1,p+1+m); //对区间按照l从大到小排序,
        memset(tree,0,sizeof(tree));
        memset(b,0,sizeof(b));
        i=n; j=1;
        while(j<=m) //处理区间
        {
            while(i>0&&i>=p[j].l) //循环点
            {
                for(int k=1;k*k<=a[i];k++) //找到a[i]的每一个约数,不断更新tree使其中存的是最大的约数
                {
                    if(a[i]%k==0)
                    {
                        if(b[k]!=0) //k不是第一次出现,也就是保证k至少是两个数的约数
                        {
                            add(b[k],k);
                        }
                        b[k]=i;
                        if(k!=a[i]/k)
                        {
                            if(b[a[i]/k]!=0)
                            {
                                add(b[a[i]/k],a[i]/k);
                            }
                            b[a[i]/k]=i;
                        }
                    }
                }
                i--;
            }
            while(j<=m&&p[j].l>i)
            {
                ans[p[j].id]=sum(p[j].r);
                j++;
            }
        }
        for(i=1;i<=m;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值