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. 转:python webdriver API 之鼠标事件

    前面例子中我们已经学习到可以用 click()来模拟鼠标的单击操作,而我们在实际的 web 产品测试中 发现,有关鼠标的操作,不单单只有单击,有时候还要和到右击,双击,拖动等操作,这些操作包含在Act ...

  2. 【你吐吧c#每日学习】10.29 C#字符串类型&Common operators

    backslash \反斜杠 escape sequence 转义字符 double quote 双引号 new line 新行字符 Bell アラート Console.WriteLine(" ...

  3. [原创]java WEB学习笔记60:Struts2学习之路--Actioin-声明式异常处理

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. JetBrains公司介绍(Java、Python、PHP、Ruby、前端和代码测试与重构的IDE)

    JetBrains JetBrains是一家捷克的软件开发公司,该公司位于捷克的布拉格,并在俄国的圣彼得堡及美国麻州波士顿都设有办公室,该公司最为人所熟知的产品是Java编程语言开发撰写时所用的集成开 ...

  5. android ANR问题

    一.什么是ANR ANR:Application Not Responding: 具体请参考:http://blog.csdn.net/dadoneo/article/details/8270107

  6. HDU 4063 Aircraft(计算几何)(The 36th ACM/ICPC Asia Regional Fuzhou Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4063 Description You are playing a flying game. In th ...

  7. 动画--过渡延迟时间 transition-delay

    transition-delay属性和transition-duration属性极其类似,不同的是transition-duration是用来设置过渡动画的持续时间,而transition-delay ...

  8. paper 69:Haar-like矩形遍历检测窗口演示Matlab源代码[转载]

    Haar-like矩形遍历检测窗口演示Matlab源代码 clc; clear; close all; % Haar-like特征矩形计算 board = 24 % 检测窗口宽度 num = 24 % ...

  9. php文件上传参数设置

    php默认的 上传文件大小是2M,要上传超过此大小的文件,需要设置php和apache的一些参数,具体参考如下: 1.file_uploads:是否允许通过HTTP上传文件的开关,默认为ON就是开 2 ...

  10. 真实的C++单例模式举例

    把构造函数声明为protected的理由很简单,但把构造函数声明为private的原因却很少知道.   从语法上讲,任何函数如果被声明为private,这个函数就不能从外部调用,构造函数也是函数,相反 ...