HDU 4630 No Pain No Game 线段树 和 hdu3333有共同点

No Pain No Game

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

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
 
 /**
题意:求解区间[ L , R ] max gcd() ; 如果只求一次,那么我们可以这样做。
把每个数字的因子刷一遍,满足>=2的最大就是结果。
如果多次,可以这样求解。
在区间[ L , R ] 如果素因子出现,更新它上次出现的位置的最大值,
保存当前出现的位置。第一次出现不更新,只保存。 当枚举到R的时候,我们就可以在[ L, R ]中找最大值即可。 所以这一题就可以对r 排序,然后进行更新。
**/
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std; vector<int>yz[];
struct node
{
int l,r,len;
int maxn;
}f[*];
struct node2
{
int st,ed;
int i,val;
}a[];
int date[];
int pre[]; bool cmp(struct node2 n1,struct node2 n2)
{
return n1.ed<n2.ed;
}
bool cmp1(struct node2 n1,struct node2 n2)
{
return n1.i<n2.i;
}
void init()
{
for(int i=;i<=;i++)
for(int j=i;j<=;j=j+i)
yz[j].push_back(i);
}
void build(int l,int r,int n)
{
int mid = (l+r)/;
f[n].l = l;
f[n].r = r;
f[n].len = f[n].r-f[n].l +;
f[n].maxn = ;
if(l==r) return;
build(l,mid,n*);
build(mid+,r,n*+);
}
void update(int wz,int num,int n)
{
int mid=(f[n].l + f[n].r)/;
if(f[n].l == wz && f[n].r == wz)
{
if(f[n].maxn<num)
f[n].maxn = num;
return;
}
if(mid>=wz) update(wz,num,n*);
else if(mid<wz) update(wz,num,n*+);
f[n].maxn = f[n*].maxn>f[n*+].maxn ? f[n*].maxn : f[n*+].maxn;
}
int query(int l,int r,int n)
{
int mid=(f[n].l + f[n].r)/;
if(f[n].l == l && f[n].r == r)
{
return f[n].maxn;
}
if(mid>=r) return query(l,r,n*);
else if(mid<l) return query(l,r,n*+);
else return max(query(l,mid,n*),query(mid+,r,n*+));
}
int main()
{
int T , n , m;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
build(,n,);
for(int i=;i<=n;i++)
scanf("%d",&date[i]);
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&a[i].st,&a[i].ed);
a[i].i = i;
a[i].val = ;
}
sort(a+,a++m,cmp);
memset(pre,,sizeof(pre));
int k = ;
for(int i=;i<=n;i++)
{
int ans = yz[date[i]].size();
for(int j=;j<ans;j++)
{
int temp = yz[date[i]][j];
if(pre[temp])
update(pre[temp],temp,);
pre[temp] = i;
}
while(a[k].ed == i && k<=m)
{
a[k].val = query(a[k].st,a[k].ed,);
k++;
}
}
sort(a+,a++m,cmp1);
for(int i=;i<=m;i++) printf("%d\n",a[i].val);
}
return ;
}
 
上一篇:HDU 4630 No Pain No Game (线段树+离线)


下一篇:女性社区TOP10