hdu 4630 查询[L,R]区间内任意两个数的最大公约数
No Pain No Game
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2000 Accepted Submission(s): 851
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.
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.
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
/*
hdu 4630 查询[L,R]区间内任意两个数的最大公约数 给你n个数,m个询问,输出区间[l,r]内的任意两个数的最大公约数 对于每一个数而言,对左右都会造成影响,所以我们考虑把查询按r从小到大排序,遇到r则输出结果
像这样的话我们就能只考虑a[i]与 [1,i-1]之间数的关系
因为是求的最大公约数,所以枚举a[i]的因子,如果发现此因子已经出现过,则在此前因子出现的地方赋值
(因为我们是求[l,r]之间的,并不能保证之前因子出现的位置在此之内),然后更新该因子的位置 hhh-2016-04-05 19:55:32
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <vector>
#include <functional>
#define lson (i<<1)
#define rson ((i<<1)|1)
using namespace std;
const int maxn = 5e5+5;
int pos[maxn];
int a[maxn];
int tans[maxn];
struct node
{
int l,r;
int Max;
int mid()
{
return (l+r)>>1;
}
} tree[maxn<<2]; void push_up(int i)
{
tree[i].Max = max(tree[lson].Max,tree[rson].Max);
} void build(int i,int l,int r)
{
tree[i].l = l,tree[i].r = r;
tree[i].Max=0;
if(l == r)
return ;
int mid = tree[i].mid();
build(lson,l,mid);
build(rson,mid+1,r);
push_up(i);
} void update(int i,int k,int val)
{
if(tree[i].l == tree[i].r && tree[i].l == k)
{
tree[i].Max = max(tree[i].Max,val);
return ;
}
int mid = tree[i].mid();
if(k <= mid)
update(lson,k,val);
else
update(rson,k,val);
push_up(i);
} int query(int i,int l,int r)
{
if(tree[i].l >= l && tree[i].r <= r)
{
return tree[i].Max;
}
int ans = 0;
int mid = tree[i].mid();
if(l <= mid)
ans = max(ans,query(lson,l,r));
if(r > mid)
ans = max(ans,query(rson,l,r));
return ans;
} struct qy
{
int l,r;
int id;
} qry[maxn]; bool cmp(qy a ,qy b)
{
return a.r < b.r;
} int main()
{
int T;
int n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
build(1,1,n);
memset(pos,-1,sizeof(pos));
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]);
int m;
scanf("%d",&m);
for(int i = 1; i <= m; i++)
{
scanf("%d%d",&qry[i].l,&qry[i].r);
qry[i].id = i;
}
sort(qry+1,qry+1+m,cmp); for(int i = 1,cur = 1; i <= n; i++)
{ for(int j = 1; j*j <= a[i]; j++)
{
if(a[i]%j == 0)
{
if(pos[j] != -1)
update(1,pos[j],j);
if(pos[a[i]/j]!= -1)
update(1,pos[a[i]/j],a[i]/j);
pos[j] = i;
pos[a[i]/j] = i;
}
} while(i == qry[cur].r && cur <= m)
{
tans[qry[cur].id] = query(1,qry[cur].l,qry[cur].r);
cur ++;
}
}
for(int i = 1; i <= m; i++)
{
printf("%d\n",tans[i]);
}
}
return 0;
}
hdu 4630 查询[L,R]区间内任意两个数的最大公约数的更多相关文章
- hdu 4638 树状数组 区间内连续区间的个数(尽可能长)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- D. Powerful array 离线+莫队算法 给定n个数,m次查询;每次查询[l,r]的权值; 权值计算方法:区间某个数x的个数cnt,那么贡献为cnt*cnt*x; 所有贡献和即为该区间的值;
D. Powerful array time limit per test seconds memory limit per test megabytes input standard input o ...
- CF 给你三个数字L, R, K,问在[L, R]范围内有多少个数字满足它每一位不同数字不超过k个,求出它们的和(数位DP)
题意: 给你三个数字L, R, K,问在[L, R]范围内有多少个数字满足它每一位不同数字不超过k个,求出它们的和 分析:考虑用状态压缩 , 10给位0~9 , 如果之前出现过了某个数字x ,那就拿当 ...
- HDU-1695 GCD(求一个区间内与一个数互质的个数)
题意: 给你一个T,是样例的个数,接下来是五个数l1,r1,l2,r2,k 前四个数代表两个区间(l1,r1),(l2,r2)这个题l1=1,l2=1; 取x1属于(1,r1),x2属于(1,r2) ...
- 谷歌面试题:输入是两个整数数组,他们任意两个数的和又可以组成一个数组,求这个和中前k个数怎么做?
谷歌面试题:输入是两个整数数组,他们任意两个数的和又可以组成一个数组,求这个和中前k个数怎么做? 分析: "假设两个整数数组为A和B,各有N个元素,任意两个数的和组成的数组C有N^2个元素. ...
- HDU 3709 Balanced Number 求区间内的满足是否平衡的数量 (数位dp)
平衡数的定义是指,以某位作为支点,此位的左面(数字 * 距离)之和 与右边相等,距离是指某位到支点的距离; 题意:求区间内满足平衡数的数量 : 分析:很好这又是常见的数位dp , 不过不同的是我们这次 ...
- SQLALchemy如何查询mysql某个区间内的数据
查了下,找到3种方式: 方法一注意时间格式:xxxx-xx-xx 方法二没有‘day’ 方法三的时间格式同方法一 1.result = Jobs.query.filter(Jobs.create_ti ...
- Excel 统计在某个区间内数值的个数
=COUNTIF(A1:A50,"<=1.0E-5")-COUNTIF(A1:A50,"<60")
- js 取任意两个数之间的随机整数
function getRandomInt(min, max) { min = Math.ceil(min); max = Math.floor(max); return Math.floor(Mat ...
随机推荐
- python pip包管理
pip 是一个安装和管理 Python 包的工具 , 是 easy_install 的一个替换品.本文将详细说明 安装 pip 的方法和 使用 pip 的一些基本操作如安装.更新和卸载 python ...
- 在arc模式下 CGImage 释放问题
//大图bigImage //定义myImageRect,截图的区域 if (imagecount >= 3) { CGRect myImageRect; if (i.size.width< ...
- EL表达式 与 servlvet3.0的新规范
EL表达式 EL表达式 是一种简化的数据访问方式,是对jsp脚本的简化 . 如我们在一个页面中需要输出session的保存的一个值: <% out.println(session.getAt ...
- New UWP Community Toolkit - DeveloperTools
概述 UWP Community Toolkit 中有一个开发者工具集 DeveloperTools,可以帮助开发者在开发过程中进行 UI 和功能的调试,本篇我们结合代码详细讲解 Develope ...
- 算法题丨4Sum
描述 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...
- Spring Security入门(3-2)Spring Security对接用户的权限系统
源文链接,多谢作者的分享: http://www.360doc.com/content/14/0727/16/18637323_397445724.shtml 1.原生的spring-security ...
- Python 爬取淘宝商品信息和相应价格
!只用于学习用途! plt = re.findall(r'\"view_price\"\:\"[\d\.]*\"',html) :获得商品价格和view_pri ...
- Python 中格式化字符串 % 和 format 两种方法之间的区别
Python2.6引入了 format 格式化字符串的方法,现在格式化字符串有两种方法,就是 % 和 format ,具体这两种方法有什么区别呢?请看以下解析. # 定义一个坐标值 c = (250, ...
- Vue框架之双向绑定事件
Vue框架之双向绑定事件 首先介绍下Vue框架的语法 vue通过 {{temp}} 来渲染变量 {{count+100}} # 求和 v-text # 为标签插入text文本 v-html # 为标签 ...
- hdu-1082 Matrix Chain Multiplication---栈的运用
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1082 题目大意: 题意大致是N个矩阵,如果要求计算的矩阵例如(AB),如果A的列等于B的行,进行:A ...