No Pain No Game(树状数组)
2017-10-14 01:50
351 查看
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 a[sub]1[/sub], a[sub]2[/sub], ..., a[sub]n[/sub]. 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
题意:
给你个数字数字序列,然后m次查询,求区间[l,r]最大的两个数字的公因子。
思路:先预处理一下,求出每一个数字的因子,维护 c[j] 代表 [j,i] 出现 2 次以上的最大数。
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 a[sub]1[/sub], a[sub]2[/sub], ..., a[sub]n[/sub]. 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
题意:
给你个数字数字序列,然后m次查询,求区间[l,r]最大的两个数字的公因子。
思路:先预处理一下,求出每一个数字的因子,维护 c[j] 代表 [j,i] 出现 2 次以上的最大数。
#if 1 #include<bits/stdc++.h> using namespace std; const int MAXX=5e4+5; int c[MAXX],a[MAXX],ans[MAXX],pre[MAXX]; vector<int> factor[MAXX]; void pre_do() { for(int i=1; i<MAXX; i++) { for(int j=i; j<MAXX; j+=i) { factor[j].push_back(i); } } } struct node { int l,r,id; bool operator <(const node &a)const { return r<a.r; } }nodes[MAXX]; int lowbit(int x) { return x&(-x); } void add(int x,int v) { for(; x>0; x-=lowbit(x)) { c[x]=max(c[x],v); } } int sum(int x) { int M=0; for(; x<MAXX; x+=lowbit(x)) { M=max(c[x],M); } return M; } int main() { ios::sync_with_stdio(false); pre_do(); int t; cin>>t; while(t--) { memset(pre,0,sizeof(pre)); memset(c,0,sizeof(c)); int n,m; cin>>n; for(int i=1; i<=n; i++) { cin>>a[i]; } cin>>m; for(int i=0; i<m; i++) { cin>>nodes[i].l>>nodes[i].r; nodes[i].id=i; } sort(nodes,nodes+m); int j=0; for(int i=1; i<=n&&j<m; i++) { int x=a[i]; for(int k=0; k<(int)factor[x].size(); k++) { int val=factor[x][k]; if(pre[val]) add(pre[val],val); //出现两次 pre[val]=i; } while(j<m&& nodes[j].r==i) { ans[nodes[j].id]=sum(nodes[j].l); j++; } } for(int i=0; i<m; i++) { cout<<ans[i]<<endl; } } } #endif
相关文章推荐
- hdu-4630-No Pain No Game-(树状数组,离线处理)
- hdu 4630 No Pain No Game(树状数组)
- hdu 4630 No Pain No Game(树状数组离线操作)
- HDU 4630 No Pain No Game(树状数组)
- HDU 4630 No Pain No Game(2013多校3 1010题 离线处理+树状数组求最值)
- No Pain No Game(数状数组离线查询)
- HDU 5975 && 2016ICPC大连 E: Aninteresting game(树状数组原理)
- HDU 4602 Magic Ball Game(离线处理,树状数组,dfs)
- HDU 4630 No Pain No Game
- No Pain No Game
- hdu 4630 No Pain No Game
- HDU 4630 No Pain No Game 线段树 和 hdu3333有共同点
- HDU4630:No Pain No Game(线段树)
- HDU 4630 No Pain No Game 树状数组+离线操作
- hdu4630 No Pain No Game(离线 线段树)
- HDU 4630 No Pain No Game 树状数组+离线操作
- hdu 4605-Magic Ball Game(树状数组)
- HDU 4630 No Pain No Game 树状数组
- HDU 4605 Magic Ball Game 树状数组
- hdu4605 Magic Ball Game(树状数组)