题目链接

看的别人的题解,离线之后,按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. Windows上搭个Nginx集群环境玩玩

    一.在windows上安装nginx 1.从这里下载nginx的windows版本 2.把压缩文件解压至c盘根目录,并将文件夹重命名成nginx 3.在conf目录下的nginx.conf文件中,指定 ...

  2. html css js

    html 回顾 字体:font 属性: color: 颜色 size: 字号 表格:table 标签: tr:表格中的行 td: 单元行中的单元格 th:通常使用在table中的第一行, 成为表头, ...

  3. sql SELECT时的with(nolock)选项说明

    I used to see my senior developers use WITH (NOLOCK) when querying in SQL Server and wonder why they ...

  4. Java for LeetCode 042 Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  5. ShortestPath:Six Degrees of Cowvin Bacon(POJ 2139)

    牛与电影 题目大意:就是一群牛,又在玩游戏了(怎么你们经常玩游戏),这个游戏规则如下,把牛拆分成一个一个组,并且定义一个“度”,规定在一个组的牛与他自己的度为0,与其他牛的度为1,不同组的牛不存在度, ...

  6. Enum:Smallest Difference(POJ 2718)

    最小的差别 题目大意:输入一串数字,问你数字的组合方式,你可以随意用这些数字组合(无重复)来组合成一些整数(第一位不能为0),然后问你组合出来的整数的差最小是多少? 这一题可以用枚举的方法去做,这里我 ...

  7. Live Archive 3644 X-Plosives 解题报告

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&category=1 ...

  8. 图像边缘检测——Sobel算子

    边缘是图像最基本的特征,其在计算机视觉.图像分析等应用中起着重要的作用,这是因为图像的边缘包含了用于识别的有用信息,是图像分析和模式识别的主要特征提取手段. 1.何为“图像边缘”? 在图像中,“边缘” ...

  9. 查看nginx版本号

    # ./nginx -v Tengine version: Tengine/ (nginx/)

  10. CentOS 6.5 下安装 Elasticsearch 5

    安装最新的 Elasticsearch 5 需要Java 8.所有先要确定环境中是否有Java 8.如果没有则需要安装. 1. 安装Java 8 首先使用 yum list installed | g ...