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. poj1859The Perfect Symmetry

    链接 按x或y排序,假如有对称点的话,头尾相对. #include <iostream> #include<cstdio> #include<cstring> #i ...

  2. 报错Database returned an invalid value in QuerySet.datetimes(). Are time zone definitions for your database and pytz installed?解决

    在django中的setting.py中: LANGUAGE_CODE = 'en-us' TIME_ZONE = 'Asia/Shanghai' #'UTC' USE_I18N = True USE ...

  3. 无限极分类,传递一个子ID得到所有父集,用于在前台分层显示标题

    方法: static public function getParents($data,$id){ $arr=array(); foreach ($data as $v) { if ($v['id'] ...

  4. hdu5785(极角排序求所有锐角钝角个数)

    做法很显然,求出所有的锐角和钝角就能求出有多少个锐角三角形了. 我用了愚钝的方法,写了两三个小时... 看了下别人简单的代码.学习了下做法. sort(temp+,temp+cnt+);//排序 Fo ...

  5. 原创: 开题报告中摘要部分快速将一段文字插入到word的表格中

    开题报告的摘要是表格形式,之前需要一个一个字的敲入,十分不方便修改. 所以百度了一下方法.现总结如下: 达到的效果 1 将这段文字复制粘贴到word中,在word文件中的每一个字与字之间插入空格.如何 ...

  6. Integer封装与拆箱

    Integer封装与拆箱 简介: 目录: Integer自动封装的陷阱 Integer自动拆箱机制 Integer自动封装的陷阱 public class IntegerDemo { public s ...

  7. java 生成随机数

    本段代码是生成的六位随机数.也可修改生成任意位随机数. int[] array = {0,1,2,3,4,5,6,7,8,9}; Random rand = new Random(); for (in ...

  8. IE6 + png24 透明图片(滤镜)

    IE6 + png24 透明图片(滤镜) .IE6png{_filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true ...

  9. Java常见错误

    1.NullPointerExceptin 空指针异常 a.引用没有初始化就使用 b.引用置空了,仍然被使用 2.IndexOutofBoundsException 下标越界 a.数组下标小于0 或者 ...

  10. 工作常用的linux/mysql/php/工具命令

    工作常用的linux/mysql/php/工具命令: 1. tar备份目录 tar zcvf ****.tar.gz ****/ tar 备份跳过目录 tar --exclude=test1 3. s ...