题目链接

看的别人的题解,离线之后,按r排序,枚举1-n,利用pre[j],存上次j的倍数出现的位置,树状数组里统计的当前位置到最后的最大值,树状数组是求区间最值其实应该很麻烦的,但是此题用法只是求到最后的最大值,插入的时候,往前更新就好了,类似求和。

 #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int num[];
int p[];
int n;
struct node
{
int l,r,id;
} que[];
bool cmp(node a,node b)
{
return a.r < b.r;
}
bool cmp1(node a,node b)
{
return a.id < b.id;
}
int pre[];
int ans[];
int lowbit(int t)
{
return t&(-t);
}
void insert(int t,int d)
{
while(t)
{
p[t] = max(p[t],d);
t -= lowbit(t);
}
}
int getmax(int t)
{
int maxz = ;
while(t <= n)
{
maxz = max(maxz,p[t]);
t += lowbit(t);
}
return maxz;
}
int main()
{
int i,j,t,m,tnum;
scanf("%d",&t);
while(t--)
{
memset(p,,sizeof(p));
memset(pre,,sizeof(pre));
scanf("%d",&n);
for(i = ; i <= n; i ++)
scanf("%d",&num[i]);
scanf("%d",&m);
for(i = ; i < m; i ++)
{
scanf("%d%d",&que[i].l,&que[i].r);
que[i].id = i;
}
sort(que,que+m,cmp);
tnum = ;
for(i = ; i <= n; i ++)
{
if(tnum == m) break;
for(j = ; j*j <= num[i]; j ++)
{
if(num[i]%j == )
{
if(pre[j] != )
insert(pre[j],j);
pre[j] = i;
if (j * j== num[i]) continue ;
if(pre[num[i]/j] != )
insert(pre[num[i]/j],num[i]/j);
pre[num[i]/j] = i;
}
}
while(tnum < m&&que[tnum].r == i)
{
ans[que[tnum].id] = getmax(que[tnum].l);
tnum ++;
}
}
sort(que,que+m,cmp1);
for(i = ;i < m;i ++)
{
if(que[i].l == que[i].r)
printf("0\n");
else
printf("%d\n",ans[i]);
}
}
return ;
}

HDU 4630 No Pain No Game(树状数组)的更多相关文章

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

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

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

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

  3. 4630 no pain no game 树状数组

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4630 题意:给你N个数,然后给你M个询问,每个询问包含一个l 一个r,问你lr 这个区间中任意两个数最 ...

  4. hdu 5869 区间不同GCD个数(树状数组)

    Different GCD Subarray Query Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K ( ...

  5. hdu 6203 ping ping ping(LCA+树状数组)

    hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...

  6. hdu 1394 Minimum Inversion Number(树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个0 — n-1的排列,对于这个排列你可以将第一个元素放到最后一个,问你可能得到的最 ...

  7. HDU 5792 World is Exploding (树状数组)

    World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...

  8. HDU 5773 The All-purpose Zero(树状数组)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5773 [题目大意] 给出一个非负整数序列,其中的0可以替换成任意整数,问替换后的最长严格上升序列长 ...

  9. POJ 3928 &amp; hdu 2492 &amp; Uva1428 PingPong 【树状数组】

    Ping pong                                                   Time Limit: 2000/1000 MS (Java/Others)   ...

随机推荐

  1. error splicing file: file too large解决方法

    FAT32格式的usb最大支持4G的文件,拷贝超过4G的文件需要把usb换成NTFS格式.

  2. css3 变形(transform)、转换(transition)和动画(animation)

    http://www.w3cplus.com/content/css3-transform/  在CSS3中transform主要包括以下几种:旋转rotate.扭曲skew.缩放scale和移动tr ...

  3. css行内样式

    <title>归园田居</title> </head> <body> <h2>归园田居</h2> <p>种豆南山下, ...

  4. TinyXML 在vs2010 VC++使用

    1.下载TinyXML  http://www.grinninglizard.com/tinyxml/  解压缩,将以下六个文件复制到您的工程项目文件(跟您的cpp文件在一个目录下)中 tinyxml ...

  5. HDU 4334 Trouble (暴力)

    Trouble Time Limit: 5000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  6. Ubuntu 下搭建SVN服务器

    root@iZ25q0jd99eZ:~# sudo apt-get install subversion root@iZ25q0jd99eZ:/etc/subversion# mkdir /svn r ...

  7. 查询DDOS攻击IP列表 grep -i

    [root@xsx ~]# netstat -an|grep -i syn_recv|awk '{print $5}'|awk -F\: '{print $1}'|sort|uniq -c 1 149 ...

  8. Shell脚本入门与应用

    编写第一个shell脚本 如同其他语言一样,通过我们使用任意一种文字编辑器,比如 nedit.kedit.emacs.vi 等来编写我们的 shell 程序.程序必须以下面的行开始(必须方在文件的第一 ...

  9. ***mysql中经度纬度字段用什么存储(关于mysql的float和decimal区别)

    float,decimal精确度比较   float,double容易产生误差,对精确度要求比较高时,建议使用decimal来存,decimal在mysql内存是以字符串存储的, 用于定义货币要求精确 ...

  10. 【软件工程】week5-个人作业-敏捷开发方法初窥

    敏捷开发方法初窥 引言:本周的软件工程个人博客作业是阅读关于敏捷开发方法的文章(http://martinfowler.com/agile.html),并撰写自己的读后感.文章内容非常丰富,对敏捷开发 ...