No Pain No Game
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2249 Accepted Submission(s): 990
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.
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.
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
Author
WJMZBMR
Source
Recommend
zhuyuanchen520 | We have carefully selected several similar problems for you:
6275
6274
6273
6272
6271
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long LL;
#define maxn (50001)
struct Node
{
int l,r,val;
Node() { l=r=val=0; }
};
Node seq[maxn<<2],qy[maxn];
int num[maxn],ans[maxn],pre[maxn],vis[maxn];
void pushup(int rt)
{
seq[rt].val=max(seq[rt<<1].val,seq[rt<<1|1].val);
}
void Build(int l,int r,int rt)
{
seq[rt].l=l;
seq[rt].r=r;
seq[rt].val=0;
if(l==r) return;
int mid=(l+r)>>1;
Build(l,mid,rt<<1);
Build(mid+1,r,rt<<1|1);
//pushup(rt);
}
void update(int pos,int v,int rt)
{
if(seq[rt].l==seq[rt].r) { seq[rt].val=max(v,seq[rt].val); return ; }
int mid=(seq[rt].l+seq[rt].r)>>1;
if(mid>=pos) update(pos,v,rt<<1);
else update(pos,v,rt<<1|1);
pushup(rt);
}
int query(int l,int r,int rt)
{
if(seq[rt].l==l&&seq[rt].r==r)
return seq[rt].val;
int mid=(seq[rt].l+seq[rt].r)>>1;
int ans=0;
if(mid>=r) ans=max(ans,query(l,r,rt<<1));
else if (mid<l) ans=max(ans,query(l,r,rt<<1|1));
else ans=max(query(l,mid,rt<<1),query(mid+1,r,rt<<1|1));
return ans;
}
bool cmp(Node x,Node y)
{
return x.r<y.r;
}
/*
只用到对线段树的单点更新,
题目想要对任意区间求最大因子,
即两个数最大因子,那么任意区间内的两个数形成的最大因子中的最大,
即区间的最大因子。
用到了线段树维护区间最大值的基本属性,
枚举扫描到的数的因子,
如果因子在前面已经被检测到,
则更新(开一个数组维护因子前一个位置)。
其中用了不少维护思维,
第一个是把查询排序,
也就是在查询更大的区间时可以假定右边界之前的区间都已知。
第二个就是因子,
一个边界假想两个端点值存在一个公因子且仅在两个端点中存在,
又查询的区间只会扩大不会回退,所以只要保证第一层树的更新正确即可
*/
int n,m;
int main()
{
ios::sync_with_stdio(false);
int t;scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",&num[i]);
scanf("%d",&m);
for(int i=0;i<m;i++)
{
scanf("%d %d",&qy[i].l,&qy[i].r);
qy[i].val=i;
}
sort(qy,qy+m,cmp);
Build(1,n,1);
int cnt=0;
memset(vis,-1,sizeof(vis));
for(int i=1;i<=n;i++)
{
for(int j=1;j*j<=num[i];j++)
{
if(num[i]%j!=0) continue;
if(vis[j]!=-1) update(vis[j],j,1);
if(j*j!=num[i]&&vis[num[i]/j]!=-1) update(vis[num[i]/j],num[i]/j,1);
vis[j]=vis[num[i]/j]=i;
}
while(qy[cnt].r==i&&cnt<m)
{
ans[qy[cnt].val]=query(qy[cnt].l,qy[cnt].r,1);
cnt++;
}
}
for(int i=0;i<m;i++) printf("%d\n",ans[i]);
}
return 0;
}
PS(这是我目前见过的玩的最妙的技巧了。。。)