HDU 4630 gcd(线段树离线处理)
2018-03-31 23:30
281 查看
No Pain No Game
Time Limit: 4000/2000 MS (Java/Others) MemoryLimit: 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 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]
4000
,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
题意:
有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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <vector>
#define N 50005
#define MAX(x, y) ((x)>(y)?(x):(y))
using namespace std;
int a
;
int pre
;
int ans
19954
;
vector<int>fac
;
typedef struct{
int l, r, id;
}Input;
Input in
;
bool cmp(Input a, Input b)
{
return a.r < b.r;
}
void Init()
{
int i, j;
for(i = 1; i < N; i++)
for(j = i; j < N; j += i)
fac[j].push_back(i);
}
struct LineTree{
void Push_up(int rt)
{
T[rt].Max = MAX(T[rt<<1].Max, T[rt<<1|1].Max);
}
void Build(int rt, int l, int r)
{
T[rt].l = l;
T[rt].r = r;
T[rt].Max = 0;
if(l == r) return;
T[rt].mid = ((l + r)>>1);
Build(rt<<1, l, T[rt].mid);
Build(rt<<1|1, T[rt].mid + 1, r);
}
void Update(int rt, int pos, int val)
{
if(T[rt].l == pos && T[rt].r == pos){
if(val > T[rt].Max) T[rt].Max = val;
return;
}
if(pos <= T[rt].mid) Update(rt<<1, pos, val);
else Update(rt<<1|1, pos, val);
Push_up(rt);
}
int Query(int rt, int l, int r)
{
if(l == T[rt].l && r == T[rt].r) return T[rt].Max;
if(r <= T[rt].mid) return Query(rt<<1, l, r);
else if(l > T[rt].mid) return Query(rt<<1|1, l, r);
else return MAX(Query(rt<<1, l, T[rt].mid), Query(rt<<1|1, T[rt].mid + 1, r));
}
typedef struct{
int l, mid, r, Max;
}Node;
Node T[N<<2];
};
LineTree tree;
int main()
{
//freopen("in.txt","r",stdin);
Init();
int T;
scanf("%d", &T);
while(T--){
int n, m, i, j, k;
scanf("%d", &n);
tree.Build(1, 1, n);
for(i = 1; i <= n; i++)
scanf("%d", a + i);
scanf("%d", &m);
for(i = 0; i < m; i++){
scanf("%d%d", &in[i].l, &in[i].r);
in[i].id = i;
}
sort(in, in + m, cmp);
memset(pre, 0, sizeof(pre));
for(i = 1, j = 0; i <= n && j < m; i++){
for(k = 0; k < fac[a[i]].size(); k++){
int tmp = fac[a[i]][k];
if(pre[tmp])
tree.Update(1, pre[tmp], tmp);
pre[tmp] = i;
}
while(j < m && in[j].r == i){
ans[in[j].id] = tree.Query(1, in[j].l, i);
j++;
}
}
for(i = 0; i < m; i++)
printf("%d\n", ans[i]);
}
return 0;
}
相关文章推荐
- gcd(线段树离线处理)——HDU 4630
- HDU 4630 No Pain No Game(线段树离线处理)
- HDU 5381 The sum of gcd 离线处理+线段树
- hdu 5381 The sum of gcd (线段树x树状数组x区间和维护进阶x离线处理)
- HDU - 4630 No Pain No Game (线段树 + 离线处理)
- HDU - 4630 No Pain No Game (线段树 + 离线处理)
- HDU 4630-No Pain No Game(线段树+离线处理)
- HDU 5869 Different GCD Subarray Query (离线处理 树状数组)
- hdu 4630 线段树+离线处理
- HDU 4288 Coder 【线段树+离线处理+离散化】
- 离线处理(线段树|树状数组)| 莫对算法 —— HDU 4638 Group
- hdu 3333 Turing Tree(线段树求区间内不同值之和+离线处理)
- HDU 4630 No Pain No Game (线段树 + 离线)
- HDU 4288 Coder(线段树+离线处理)
- hdu 4638 Group (线段树+离线处理)
- HDU 4417 Super Mario(线段树离线处理/主席树)
- HDU 4288 && Codeforces 85D (线段树+离散化+离线处理)
- HDU 4638-Group(线段树+离线处理)
- hdu 4630 树状数组+离线操作+GCD
- HDU 4288 线段树 + 离线处理