No Pain No Game HDU - 4630 線段樹+離線處理

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

題意:T組樣例,給定一個長度爲N的序列,M個詢問,每次詢問給定一個區間[L,R],查詢這個區間任取兩個數,最大GCD是多少

思路:任意2個數的GCD一定是他們的因子,我們把每個數的因子分解出來,我們枚舉每個點爲右端點i,如果該點的某個GCD在之前出現過,就在之前的地方更新,不管之前有沒有出現,都將這個GCD出現的位置標爲i;之後我們查詢以該右端點爲結尾的查詢區間。

#include<map>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=5e4+100;
int a[maxn],ans[maxn],book[maxn];
struct node{
	int l;
	int r;
	int sum;
}tree[maxn<<2];
struct Node{
	int l;
	int r;
	int id;
}qu[maxn];
void pushup(int cur)
{
	tree[cur].sum=max(tree[cur<<1].sum,tree[cur<<1|1].sum);
}
void build(int l,int r,int cur)
{
	tree[cur].l=l;
	tree[cur].r=r;
	tree[cur].sum=0;
	if(l==r) return ;
	int m=(l+r)>>1;
	build(l,m,cur<<1);
	build(m+1,r,cur<<1|1);
}
void update(int tar,int val,int cur)
{
	if(tree[cur].l==tree[cur].r) 
	{
		tree[cur].sum=max(tree[cur].sum,val);
		return ;
	}
	if(tar<=tree[cur<<1].r) update(tar,val,cur<<1);
	else update(tar,val,cur<<1|1); 
	pushup(cur);
}
int query(int L,int R,int cur)
{
	if(L<=tree[cur].l&&tree[cur].r<=R) return tree[cur].sum;
	int res=0;
	if(L<=tree[cur<<1].r) res=max(res,query(L,R,cur<<1));
	if(R>tree[cur<<1].r) res=max(res,query(L,R,cur<<1|1));
	return res;
}
bool cmp(Node x,Node y)
{
	return x.r<y.r;
}
int main()
{
	int t,n,q;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		build(1,n,1);
		memset(book,0,sizeof(book));
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		scanf("%d",&q);
		for(int i=1;i<=q;i++)
		{
			scanf("%d%d",&qu[i].l,&qu[i].r);
			qu[i].id=i;
		}
		sort(qu+1,qu+1+q,cmp);
		int k=1;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j*j<=a[i];j++)
			{
				if(a[i]%j==0)
				{
					if(book[j]) update(book[j],j,1);
					book[j]=i;
					if(a[i]/j!=j&&book[a[i]/j]) update(book[a[i]/j],a[i]/j,1);
					book[a[i]/j]=i;
				}
			}
			for(;k<=q;k++)
			{
				if(qu[k].r!=i) break; 
				ans[qu[k].id]=query(qu[k].l,qu[k].r,1);
			}
		}
		for(int i=1;i<=q;i++) printf("%d\n",ans[i]);
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章