HDU 4630 gcd(线段树离线处理)

No Pain No Game

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


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
 

题意:

有n(n最大为50000)个数,有m(m最大为50000)个询问,每个询问的两个数l,r表示求[l, r]内的数中任意两个数能得到的最大公约数


思路:

        询问【L,R,max_gcd(x,y)】,区间内任意两个值的最大gcd。这样的话,如果每来一个查询我就处理的话那就很难降低复杂度,是采用离线处理的思想,我们首先将所有的查询全部读入,然后将查询按右端点排序(为什么按照右端点排序呢,前往下看)。   
        要求区间内任意两个点的gcd的最大值,对于这个问题,如果不做一些预处理,我们是不能有效的进行操作的,因为这个问题不是像区间极值哪些问题具有合并性的。所以我们要考虑gcd的特征,两个数的gcd一定是这两个数的因子,这样的话我们就能处理出每个数的因子,通过判断这两个数是否有公因子来更新答案(公因子比之前维护的ans大)。那么我们现在可以捋一捋所知信息:对于一个区间,我们能否通过某种方法查看这个区间内各个数字的因子,然后判断出最大的公因子即为所求。那么假设现在i位置的数字为a[i],那么如果a[i]具有因子x,如果某个位置a[j]也具有因子x,那么区间[i,j]至少拥有这样的gcd为x,如果x大于当前的ans,那么我们就可以更新答案了。    
        根据上述信息,我们维护一颗线段树,线段树维护的值是区间内最大的gcd。首先我们预处理所有数字的因子,然后我们将排序过后的区间从左到右依次处理(为了加强理解,设定k<i<j),每扫描一个值a[i],我们查看a[i]的所有因子,对于某一个因子x,如果之前有某个值a[k]也存在这样的因子x,那么就可以更新区间[k,i](这个非常关键),然后这个x也可能会更新后面的某个区间[i,j](假设a[j]包含因子x)。接着我们考虑数组pre[x]代表x这个因子上一次(最近一次)出现的位置(即某个数值包含因子x),如果没有出现过就标记为-1,如果之前处理了所有的pre[x],那么我们枚举每个数值的因子,就可以根据pre数组判断能否更新区间[pre[x], now_postion],那么对于查询[l , r]只要它在[pre[x], now_postion]中,那么就可以更新线段树的值。那么对于按右端点排序好的查询,如果在不断update的过程中遇到了查询的右端点,那么我们就可以做查询即可。


[cpp]  view plain  copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <algorithm>  
  5. #include <vector>  
  6. #define N 50005  
  7. #define MAX(x, y) ((x)>(y)?(x):(y))  
  8. using namespace std;  
  9. int a[N];  
  10. int pre[N];  
  11. int ans[N];  
  12. vector<int>fac[N];  
  13.   
  14. typedef struct{  
  15.     int l, r, id;  
  16. }Input;  
  17.   
  18. Input in[N];  
  19.   
  20. bool cmp(Input a, Input b)  
  21. {  
  22.     return a.r < b.r;  
  23. }  
  24.   
  25. void Init()  
  26. {  
  27.     int i, j;  
  28.     for(i = 1; i < N; i++)  
  29.         for(j = i; j < N; j += i)  
  30.             fac[j].push_back(i);  
  31. }  
  32.   
  33. struct LineTree{  
  34.     void Push_up(int rt)  
  35.     {  
  36.         T[rt].Max = MAX(T[rt<<1].Max, T[rt<<1|1].Max);  
  37.     }  
  38.     void Build(int rt, int l, int r)  
  39.     {  
  40.         T[rt].l = l;  
  41.         T[rt].r = r;  
  42.         T[rt].Max = 0;  
  43.         if(l == r) return;  
  44.         T[rt].mid = ((l + r)>>1);  
  45.         Build(rt<<1, l, T[rt].mid);  
  46.         Build(rt<<1|1, T[rt].mid + 1, r);  
  47.     }  
  48.     void Update(int rt, int pos, int val)  
  49.     {  
  50.         if(T[rt].l == pos && T[rt].r == pos){  
  51.             if(val > T[rt].Max) T[rt].Max = val;   
  52.             return;  
  53.         }  
  54.         if(pos <= T[rt].mid) Update(rt<<1, pos, val);  
  55.         else Update(rt<<1|1, pos, val);  
  56.         Push_up(rt);  
  57.     }  
  58.     int Query(int rt, int l, int r)  
  59.     {  
  60.         if(l == T[rt].l && r == T[rt].r) return T[rt].Max;  
  61.         if(r <= T[rt].mid) return Query(rt<<1, l, r);  
  62.         else if(l > T[rt].mid) return Query(rt<<1|1, l, r);  
  63.         else return MAX(Query(rt<<1, l, T[rt].mid), Query(rt<<1|1, T[rt].mid + 1, r));  
  64.     }  
  65.     typedef struct{  
  66.         int l, mid, r, Max;  
  67.     }Node;  
  68.     Node T[N<<2];  
  69. };  
  70.   
  71. LineTree tree;  
  72.   
  73. int main()  
  74. {  
  75.     //freopen("in.txt","r",stdin);  
  76.     Init();  
  77.     int T;  
  78.     scanf("%d", &T);  
  79.     while(T--){  
  80.         int n, m, i, j, k;  
  81.         scanf("%d", &n);  
  82.         tree.Build(1, 1, n);  
  83.         for(i = 1; i <= n; i++)  
  84.             scanf("%d", a + i);  
  85.         scanf("%d", &m);  
  86.         for(i = 0; i < m; i++){  
  87.             scanf("%d%d", &in[i].l, &in[i].r);  
  88.             in[i].id = i;  
  89.         }  
  90.         sort(in, in + m, cmp);  
  91.         memset(pre, 0, sizeof(pre));  
  92.         for(i = 1, j = 0; i <= n && j < m; i++){  
  93.             for(k = 0; k < fac[a[i]].size(); k++){  
  94.                 int tmp = fac[a[i]][k];  
  95.                 if(pre[tmp])  
  96.                     tree.Update(1, pre[tmp], tmp);  
  97.                 pre[tmp] = i;  
  98.             }  
  99.             while(j < m && in[j].r == i){  
  100.                 ans[in[j].id] = tree.Query(1, in[j].l, i);  
  101.                 j++;  
  102.             }  
  103.         }  
  104.         for(i = 0; i < m; i++)  
  105.             printf("%d\n", ans[i]);  
  106.     }  
  107.     return 0;  
  108. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值