No Pain No Game

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1465    Accepted Submission(s):
631

Problem Description
Life is a game,and you lose it,so you suicide.
But
you can not kill yourself before you solve this problem:
Given you a sequence
of number a1, a2, ..., an.They are also a
permutation of 1...n.
You need to answer some queries,each with the following
format:
If we chose two number a,b (shouldn't be the same) from interval [l,
r],what is the maximum gcd(a, b)? If there's no way to choose two distinct
number(l=r) then the answer is zero.
 
Input
First line contains a number T(T <= 5),denote the
number of test cases.
Then follow T test cases.
For each test cases,the
first line contains a number n(1 <= n <= 50000).
The second line
contains n number a1, a2, ..., an.
The third
line contains a number Q(1 <= Q <= 50000) denoting the number of
queries.
Then Q lines follows,each lines contains two integer l, r(1 <= l
<= r <= n),denote a query.
 
Output
For each test cases,for each query print the answer in
one line.
 
Sample Input
1
10
8 2 4 9 5 7 10 6 1 3
5
2 10
2 4
6 9
1 4
7 10
 
Sample Output
5
2
2
4
3
Source
 
 /**
题意:求解区间[ L , R ] max gcd() ; 如果只求一次,那么我们可以这样做。
把每个数字的因子刷一遍,满足>=2的最大就是结果。
如果多次,可以这样求解。
在区间[ L , R ] 如果素因子出现,更新它上次出现的位置的最大值,
保存当前出现的位置。第一次出现不更新,只保存。 当枚举到R的时候,我们就可以在[ L, R ]中找最大值即可。 所以这一题就可以对r 排序,然后进行更新。
**/
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std; vector<int>yz[];
struct node
{
int l,r,len;
int maxn;
}f[*];
struct node2
{
int st,ed;
int i,val;
}a[];
int date[];
int pre[]; bool cmp(struct node2 n1,struct node2 n2)
{
return n1.ed<n2.ed;
}
bool cmp1(struct node2 n1,struct node2 n2)
{
return n1.i<n2.i;
}
void init()
{
for(int i=;i<=;i++)
for(int j=i;j<=;j=j+i)
yz[j].push_back(i);
}
void build(int l,int r,int n)
{
int mid = (l+r)/;
f[n].l = l;
f[n].r = r;
f[n].len = f[n].r-f[n].l +;
f[n].maxn = ;
if(l==r) return;
build(l,mid,n*);
build(mid+,r,n*+);
}
void update(int wz,int num,int n)
{
int mid=(f[n].l + f[n].r)/;
if(f[n].l == wz && f[n].r == wz)
{
if(f[n].maxn<num)
f[n].maxn = num;
return;
}
if(mid>=wz) update(wz,num,n*);
else if(mid<wz) update(wz,num,n*+);
f[n].maxn = f[n*].maxn>f[n*+].maxn ? f[n*].maxn : f[n*+].maxn;
}
int query(int l,int r,int n)
{
int mid=(f[n].l + f[n].r)/;
if(f[n].l == l && f[n].r == r)
{
return f[n].maxn;
}
if(mid>=r) return query(l,r,n*);
else if(mid<l) return query(l,r,n*+);
else return max(query(l,mid,n*),query(mid+,r,n*+));
}
int main()
{
int T , n , m;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
build(,n,);
for(int i=;i<=n;i++)
scanf("%d",&date[i]);
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&a[i].st,&a[i].ed);
a[i].i = i;
a[i].val = ;
}
sort(a+,a++m,cmp);
memset(pre,,sizeof(pre));
int k = ;
for(int i=;i<=n;i++)
{
int ans = yz[date[i]].size();
for(int j=;j<ans;j++)
{
int temp = yz[date[i]][j];
if(pre[temp])
update(pre[temp],temp,);
pre[temp] = i;
}
while(a[k].ed == i && k<=m)
{
a[k].val = query(a[k].st,a[k].ed,);
k++;
}
}
sort(a+,a++m,cmp1);
for(int i=;i<=m;i++) printf("%d\n",a[i].val);
}
return ;
}
 

HDU 4630 No Pain No Game 线段树 和 hdu3333有共同点的更多相关文章

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

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

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

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

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

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

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

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

  5. hdu 5274 Dylans loves tree(LCA + 线段树)

    Dylans loves tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Othe ...

  6. HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模

    Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

    HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意:  给一个序列由 ...

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

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

  9. hdu 1556:Color the ball(线段树,区间更新,经典题)

    Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

随机推荐

  1. eclipse jsp 加载服务器tomcat

    1.window->Preferences

  2. 记录把方法添加到 JavaScript 对象不明白的地方

    <!DOCTYPE html> <html> <body> <script> function person(firstname,lastname,ag ...

  3. css3选择器(下)

    1.:enabled选择器 在Web的表单中,有些表单元素有可用(“:enabled”)和不可用(“:disabled”)状态,比如输入框,密码框,复选框等.在默认情况之下,这些表单元素都处在可用状态 ...

  4. 阿里 drds 分布式数据库分节点查询

    mybatis 模式下,xml 中写法 <select id="selectFailDetailOneNode" resultMap="BaseResultMap& ...

  5. java 虚拟机工具

    jps 命令 可以查询开启了rmi服务的远程虚拟机进程状态. -v jvm参数. jstat -gcutil命令 [cangyue@/System/Library/Frameworks/JavaVM. ...

  6. 【py分析】

    pyQuery pyQuery 是 jQuery 在 python 中的实现,能够以 jQuery 的语法来操作解析 HTML 文档,十分方便.使用前需要安装,easy_install pyquery ...

  7. Linux下/etc/resolv.conf 会被重新写入

    主要原因是因为安装了network manager,所以在启动后每次都会重写这个文件. 所以需要在network manager->eth0->ipv4->Automatic(DHC ...

  8. 在TVideoGrabber中如何在预览时设置相机属性

    在使用TVideoGrabber进行预览时,如何设置相机的属性呢?比如曝光.对比度.亮度等. 下面来看一下,如何通过编程来调整这些设置: ——通过指定VideoDevice属性(在VideoDevic ...

  9. umask setuid setgid

    cat /etc/bashrc if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ];#用户UI ...

  10. Java的多线程+Socket 后台

    打包好可执行的jar文件之后, 用ftp上传到服务器, 用nohup命令, 让这个服务器在后台运行, 并将输出重定向到log文件中, 命令是: #nohup java -jar MTSServer.j ...