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 ...
随机推荐
- 20145237 实验二 “Java面向对象程序设计”
20145237 实验二 “Java面向对象程序设计” 实验内容 • 理解并掌握面向对象三要素:封装.继承.多态 • 初步掌握UML建模 • 熟悉S.O.L.I.D原则 • 使用TDD设计实现复数类 ...
- IOS UITextView自适应高度
LOFTER app需要实现了一个类似iPhone短信输入框的功能,它的功能其实蛮简单,就是:[UITextView的高度随着内容高度的变化而变化].实现思路应该是: 在UITextView的text ...
- C# Unity游戏开发——Excel中的数据是如何到游戏中的 (四)2018.4.3更新
本帖是延续的:C# Unity游戏开发--Excel中的数据是如何到游戏中的 (三) 最近项目不算太忙,终于有时间更新博客了.关于数据处理这个主题前面的(一)(二)(三)基本上算是一个完整的静态数据处 ...
- 多种在线地图综合对比,Google,必应,arcgis Online...
不同网络地图的对比 天地图 坐标系:WGS84 地图配色: POI数量:丰富 有无建筑:有 地图特点:天地图按照国家标准进行配图,道路.水系.植被等图层用对应颜色渲染, POI信息丰富, ...
- Java 持久化操作之 --io流与序列化
1)File类操作文件的属性 1.File类的常用方法 1. 文件的绝对完整路径:getAbsolutePath() 文件名:getName() 文件相对路径:getPath() 文件的上一级目录:g ...
- angular2 学习笔记 ( unit test 单元测试 )
第一次写单元测试. 以前一直都有听说 TDD 的事情. 今天总算是去尝试了一下. 先说说 TDD 的想法, 是这样的, 开发项目的流程 : 确定需求 -> 写类,接口,方法的名字(不写具体实现代 ...
- J2ee入门:servlet-mapping的映射配置
<servlet-mapping>元素在Servlet和URL样式之间定义一个映射.它包含了两个子元素<servlet- name>和<url-pattern> & ...
- .NET:持续进化的统一开发平台
阅读文本大概需要 8 分钟. 标题使用的是进化这个词语,是因为 .NET 在不断的努力,也在不断的重构. 这篇文章的更多目的和意义在于科普,俗称"传教". # 持续进化的 .NET ...
- C++中explicit关键字
explicit: 防止隐式转换使用. 隐式转换:不同类型的变量可以互相转换,如将一个整形数值赋值给一个类,ClassXX lei = 4: C++中, 一个参数的构造函数(或者除了第一个参数外其余 ...
- Mac里安装配置Jdk
#下载jdk7的mac版 #官网下载地址http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.h ...