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. The golden ratio: 1.618

    http://www.chinaz.com/design/2015/1109/467968_2.shtml The golden ratio: 1.618 a/b=b/(a+b) The Fibona ...

  2. 当android studio一直显示gradle compile dependency

    出现这种情况,是被墙的问题,我的解决办法是这样的: 打开file---->setting,然后搜索gradle,把offline勾上,然后点击apply以及ok,就可以了. 有时候它会关闭,只需 ...

  3. 用Commons-FileUpload组件实现文件上传

    需要用到Tomcat还有commons-fileupload-1.3.1.jar包和commons-io-2.4.jar包. 如果需要传一个文件,form表单必须有enctype="mult ...

  4. 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 ...

  5. 在TextView上加上下划线或中划线

    在做商城项目是需要用到原价格以及降价后的价格,不可避免用到下划线或中划线  textView.getPaint().setFlags(Paint. UNDERLINE_TEXT_FLAG ); //下 ...

  6. (转)SQL Server 2005 中的计算字段

    在实际工作上遇到的问题: 在订单表中有某项商品是将“订购数量(Quantity)”乘以“单件价格(UnitCost)”等于该项商品的总价(Subtotal). 在数据表中有的列(以下皆改叫为“字段”) ...

  7. sql 查看 锁定的表 或者 未提交 的事务

    --查看锁定的 表select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName from sy ...

  8. java父类转子类的一个方法

    一般子类可以转父类.但父类转子类就会报cast error. 使用jsonobject 思想:先把父类转jsonstring 再把jsonstring转子类.剩余的子类值可以设定进去. import ...

  9. User Managerment 职责不能使用的问题

    1.此职责默认的是没有启用授权的(grant),需要在系统管理员职责下搜索到对应的菜单,进行grant 2.就算授权后,用户还是不能进行操作,需要进行以下设置: 1. Log into the app ...

  10. sp_addlinkedserver 方法应用

    EXEC  sp_addlinkedserver      @server='DBVIP',--被访问的服务器别名       @srvproduct='',      @provider='SQLO ...