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的更多相关文章

  1. Your pain

    Your pain is the breaking of the shell that encloses your understanding. 你的痛苦是你那包裹知识的皮壳的破裂.

  2. 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 ...

  3. Pain for friend

    For a guy who has experienced his fair share of mysteries,on mystery,I still can't figure out is why ...

  4. 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 ...

  5. 41 Pain and Pain Management 疼痛与疼痛管理

    Pain and Pain Management 疼痛与疼痛管理 ①Years ago,doctors often said that pain was a normal part of life.I ...

  6. 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 ...

  7. hdu 4630 No Pain No Game(线段树+离线操作)

    No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. HDU - 4630 No Pain No Game (线段树 + 离线处理)

    id=45786" style="color:blue; text-decoration:none">HDU - 4630 id=45786" style ...

  9. E - No Pain No Game 线段树 离线处理 区间排序

    E - No Pain No Game  HDU - 4630 这个题目很好,以后可以再写写.这个题目就是线段树的离线写法,推荐一个博客:https://blog.csdn.net/u01003321 ...

  10. 「2014-2-8」Reading a blog on the pain points of Global Variables of C language

    晚上读到一篇<C 语言全局变量那些事儿>.我先前对链接的理解不深,算是涨了一番姿势.此文吐槽的重点,是「非 static 限定的全局变量」带来的看似出人意料(实则可以被合理解释)的行为.虽 ...

随机推荐

  1. Navicate DataModel 注册码

    注册信息: 姓    名:ttrar.com 组    织:(空) 序列号:NAVD-6CLM-6BKA-5TXK 内容来自: 可视化数据库设计工具(Navicat Data Modeler)1.0. ...

  2. 如何提高jQuery的性能

    缓存变量DOM遍历是昂贵的,所以尽量将会重用的元素缓存. // 糟糕 h = $('#element').height(); $('#element').css('height',h-20); // ...

  3. C#JSON格式数据的转换

    json格式字符串转化为json对象:JObject calculate = (JObject)JsonConvert.DeserializeObject(Rep.Request["data ...

  4. Android之Http网络编程(三)

    在前面两篇博客<Android之Http网络编程(一)>.<Android之Http网络编程(二)>中,简单的介绍了对网页的请求和客户端与服务端的简单的参数交互.那么,这一篇博 ...

  5. 常用经典SQL语句大全(基础)

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sqlserver --- 创 ...

  6. Appium Android 屏幕滑动

  7. mongodb 数据备份,还原笔记

    公司数据库迁移,所以补充了一下知识: 1 集合的导入和导出 命令行帮助 mongoexport --help  导出 导出 newsServer 数据库下  news 集合 mongoexport - ...

  8. SQL SERVER删除列,报错."由于一个或多个对象访问此列,ALTER TABLE DROP COLUMN ... 失败"

    队友给我修改数据的语句.总是执行失败.很纳闷. 如下图: 仔细看了下这个列,并没有什么特殊.如下图: 但其确实有个约束: 'DF__HIS_DRUG___ALL_I__04E4BC85' . 为什么有 ...

  9. 【制作镜像Win*】特殊配置

    下载disk.config: wget http://10.254.3.75/images/configdrive/disk.config 修改libvirt.xml,更换cdrom为disk.con ...

  10. 【HeadFirst设计模式】11.代理模式

    定义: 为另一个对象提供一个替身或占位符以访问这个对象. 远程代理 虚拟代理 保护代理.动态代理 其它: 远程代理的远程接口必须继承记号接口Remote 远程代理的使用过程中,要注意序列化的问题, 远 ...