http://acm.hdu.edu.cn/showproblem.php?pid=4630

离散化+树状数组

将数组 *a  从后向前遍历 遍历到 a[x] 的时候 再枚举a[x]的约数 假如 约数 k

last[k] 对应到 上一个k出现的位置  那么可被公约数 k 更新的区间是   last[k] --- 最后

把所有a[x]的约数都处理完之后  从 x 到任意 y(y>=x)形成的段的 最优解都可求

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
#include<list>
using namespace std; typedef long long ll;
typedef pair<double,double>ppd;
const double PI = acos(-1.);
const double eps = (1e-9);
const int N=50001;
struct node
{
int l,r;
int index;
}q[N];
int a[N],last[N],ans[N];
int c[N];
int lowbit(int x)
{
return x&(-x);
}
void update(int i,int k)
{
while(i<N)
{
c[i]=max(c[i],k);
i+=lowbit(i);
}
}
int getM(int i)
{
int tmp=0;
while(i>=1)
{
tmp=max(tmp,c[i]);
i-=lowbit(i);
}
return tmp;
}
bool cmp(const node &x,const node &y)
{
return x.l>y.l;
}
int main()
{
//freopen("data.in","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d",&a[i]);
int Q;
scanf("%d",&Q);
for(int i=0;i<Q;++i)
{
scanf("%d %d",&q[i].l,&q[i].r);
q[i].index=i;
}
sort(q,q+Q,cmp);
memset(c,0,sizeof(c));
memset(last,-1,sizeof(last));
int ln=0;
for(int i=n;i>=1;--i)
{
for(int x=1;x*x<=a[i];++x)
if(a[i]%x==0)
{
if(last[x]!=-1)
update(last[x],x);
last[x]=i;
int y=a[i]/x;
if(x==y)
continue;
if(last[y]!=-1)
update(last[y],y);
last[y]=i;
}
while(ln<Q&&q[ln].l==i)
{
ans[q[ln].index]=getM(q[ln].r);
++ln;
}
}
for(int i=0;i<Q;++i)
printf("%d\n",ans[i]);
}
return 0;
}

hdu 4630 No Pain No Game的更多相关文章

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

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

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

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

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

  5. HDU 4630 No Pain No Game 树状数组+离线操作

    题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值. 解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数. ...

  6. HDU 4630 No Pain No Game(树状数组)

    题目链接 看的别人的题解,离线之后,按r排序,枚举1-n,利用pre[j],存上次j的倍数出现的位置,树状数组里统计的当前位置到最后的最大值,树状数组是求区间最值其实应该很麻烦的,但是此题用法只是求到 ...

  7. HDU 4630 No Pain No Game 树状数组+离线查询

    思路参考 这里. #include <cstdio> #include <cstring> #include <cstdlib> #include <algo ...

  8. hdu 4630 No Pain No Game 线段树离线处理

    题目链接 求出一个区间内任意两个数的gcd的最大值. 先将询问读进来然后按r值排序. 将每一个数因数分解, 对每一个因子x, 如果pre[x]!=-1, 那么就更新update(pre[x], x, ...

  9. HDU 4630 No Pain No Game (线段树+离线)

    题目大意:给你一个无序的1~n的排列a,每次询问[l,r]之间任取两个数得到的最大gcd是多少 先对所有询问离线,然后把问题挂在区间的左端点上(右端点也行) 在预处理完质数,再处理一个next数组,表 ...

随机推荐

  1. bootstrap学习笔记<六>(表单二之按钮)

    按钮(补充) (ps:居中元素可以使用<center></center>标签) 块级按钮(ps:按钮占一整行) <button class="btn btn-p ...

  2. mysql 求时间段平均值

    考虑下面的需求,在一段时间内,间隔一段时间,取一个平均值,把所有的平均值取出来,怎么办?思路:在存储过程中,拼接sql语句.根据起始时间和结束时间,while循环每次加一段时间.DROP PROCED ...

  3. hibernate的主键生成策略

    一共是13种,其中包括native native: 对于 oracle 采用 Sequence 方式,对于MySQL 和 SQL Server 采用identity(自增主键生成机制),native就 ...

  4. 安装64位版Oracle11gR2后无法启动SQLDeveloper的解决方案(原创) (2016-10-29 下午01:56)

    安装64位版Oracle11gR2后发现启动SQL Developer时弹出配置java.exe的路径,找到Oracle自带java.exe后产生的路径"C:\app\用户名\product ...

  5. linux之flock函数锁文件

    头文件  #include<sys/file.h> 定义函数  int flock(int fd,int operation); 函数说明  flock()会依参数operation所指定 ...

  6. Openfire 是怎么存离线消息

    原文:http://myopenfire.com/article/getarticle/26 1.openfire默认怎么存离线消息   在默认情况下,不添加任何插件的情况下,当用户不在线,对于发送给 ...

  7. synchronized与static synchronized 的区别

    synchronized是对类的当前实例加锁,防止其他线程同时访问该类的该实例的synchronized块,这里的概念是“类的当前实例”,而static synchronized是对类的所有实例加锁, ...

  8. Bootstrap强调内容

    在实际项目中,对于一些重要的文本,希望突出强调的部分都会做另外的样式处理.Bootstrap同样对这部分做了一些轻量级的处理. 如果想让一个段落p突出显示,可以通过添加类名“.lead”实现,其作用就 ...

  9. 转!!!Mysql无法创建外键的原因

    在Mysql中创建外键时,经常会遇到问题而失败,这是因为Mysql中还有很多细节需要我们去留意,我自己总结并查阅资料后列出了以下几种常见原因. 1.  两个字段的类型或者大小不严格匹配.例如,如果一个 ...

  10. easyui dialog

    <!--弹出“创建”窗口 --> <div id="dialog" class="easyui-dialog" title="创建机 ...