No Pain No Game
hdu4630:http://acm.hdu.edu.cn/showproblem.php?pid=4630
题意:给定一个排序,求区间最大GCD。
题解:离散树状数组。首先把查询按左端点从大到小排序。然后用树状数组来维护每个位置出现的最大的公约数。枚举每个数的约数,记录到当前位置为止,上一个x的倍数出现的位置b[x];
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int N=5e4+;
int a[N],c[N],b[N],ans[N];
int n,m;
int lowbit(int x){
return x&(-x);
}
void add(int x,int val){
while(x<=n){
c[x]=max(c[x],val);
x+=lowbit(x);
}
}
int getsum(int x){
int sum=;
while(x>){
sum=max(sum,c[x]);
x-=lowbit(x);
}
return sum;
}
struct Node{
int l,r;
int id;
bool operator<(const Node a)const{
return l>a.l;
}
}num[N];
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(a,,sizeof(a));
memset(c,,sizeof(c));
memset(b,,sizeof(b));
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%d%d",&num[i].l,&num[i].r);
num[i].id=i;
}
sort(num+,num+m+);
int j=n;
for(int i=;i<=m;i++){
for(;j>=num[i].l;j--){
for(int k=;k*k<=a[j];k++){
if(a[j]%k==){
if(b[k]){
add(b[k],k);
}
b[k]=j;
if(k*k!=a[j]){
if(b[a[j]/k]){
add(b[a[j]/k],a[j]/k);
}
b[a[j]/k]=j;
}
}
}
}
ans[num[i].id]=getsum(num[i].r);
}
for(int i=;i<=m;i++)
printf("%d\n",ans[i]);
}
}
No Pain No Game的更多相关文章
- Your pain
Your pain is the breaking of the shell that encloses your understanding. 你的痛苦是你那包裹知识的皮壳的破裂.
- HDU 4630 No Pain No Game 线段树 和 hdu3333有共同点
No Pain No Game Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Pain for friend
For a guy who has experienced his fair share of mysteries,on mystery,I still can't figure out is why ...
- etymon word write alb pain high alt increase large agency ag lose weight fat assist out~3
1● alb 2● write =====>rait 1● alg 2● pain 痛,疼痛 1● alt 2● high 高 1 ...
- 41 Pain and Pain Management 疼痛与疼痛管理
Pain and Pain Management 疼痛与疼痛管理 ①Years ago,doctors often said that pain was a normal part of life.I ...
- HDU 4630 No Pain No Game(2013多校3 1010题 离线处理+树状数组求最值)
No Pain No Game Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 4630 No Pain No Game(线段树+离线操作)
No Pain No Game Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU - 4630 No Pain No Game (线段树 + 离线处理)
id=45786" style="color:blue; text-decoration:none">HDU - 4630 id=45786" style ...
- E - No Pain No Game 线段树 离线处理 区间排序
E - No Pain No Game HDU - 4630 这个题目很好,以后可以再写写.这个题目就是线段树的离线写法,推荐一个博客:https://blog.csdn.net/u01003321 ...
- 「2014-2-8」Reading a blog on the pain points of Global Variables of C language
晚上读到一篇<C 语言全局变量那些事儿>.我先前对链接的理解不深,算是涨了一番姿势.此文吐槽的重点,是「非 static 限定的全局变量」带来的看似出人意料(实则可以被合理解释)的行为.虽 ...
随机推荐
- 【转】BUG敏感度的培养
在我们刚踏入软件测试行业时,不管你是专业的.非专业的,培训出来的还是未培训的.刚进公司时你看着身边的同时报的Bug很多并且大都是严重程度高,自己也很想提高一下,想要提高自己的bug敏感度,建议从下面几 ...
- jquery扩展 $.fn
$.fn是指jquery的命名空间,加上fn上的方法及属性,会对jquery实例每一个有效. 如扩展$.fn.abc(),即$.fn.abc()是对jquery扩展了一个abc方法,那么后面你的每一个 ...
- Android Studio如何显示行号
Android Studio默认没有显示行号,很多同学在使用中很不习惯.本经验介绍怎样让Android Studio显示行号. 首先我们打开我们下载安装好的Android Studio 然后右击工具按 ...
- effective c++(07)之为多态基类声明virtual析构函数
class TimeKeeper { public: TimeKeeper() ; ~TimeKepper() ; ... } ; class AtomicClock:public TimeKeepe ...
- Android中FTP服务器搭建入门
http://www.2cto.com/kf/201501/374048.html http://blog.csdn.net/smile3670/article/details/44343617 有 ...
- sql索引碎片产生的原理 解决碎片的办法(sql碎片整理)(转)
本文讲述了SQL SERVER中碎片产生的原理,内部碎片和外部碎片的概念.以及解决碎片的办法和填充因子.在数据库中,往往每一个对于某一方面性能增加的功能也会伴随着另一方面性能的减弱.系统的学习数据库知 ...
- Masonry 控件详解
1. Masonry的属性 @property (nonatomic,strong,readonly)MASConstraint *left; //左侧 @property(nonatomic,s ...
- ajax和jsonp的封装
一直在用jQuery的ajax,跨域也是一直用的jQuery的jsonp,jQuery确实很方便,$.ajax({...})就可以搞定. 为了更好的理解ajax和jsonp,又重新看了下书,看了一些博 ...
- OpenJudge/Poj 1936 All in All
1.链接地址: http://poj.org/problem?id=1936 http://bailian.openjudge.cn/practice/1936 2.题目: All in All Ti ...
- Java 使用反射拷贝对象一般字段值
在<Java解惑>上面看到第八十三例--诵读困难者,要求使用非反射实现单例对象的拷贝.查阅了部分资料,先实现通过反射拷贝对象. 1. 编写需要被拷贝的对象Person package co ...