题意:

给你n个数的序列a,q个询问,每个询问给l,r,求在下标i在[l,r]的区间任意两个数的最大公约数中的最大值

分析:

有了hdu3333经验,我们从左向右扫序列,如果当前数的约数在前面出现过,那这个约数可能就是最大的答案。所以我们枚举当前数的所有约数,用线段树维护区间最大值,查询序列离线处理保证查询的正确。

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<1|1
#define All 1,N,1
#define N 50010
#define read freopen("in.txt", "r", stdin)
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
//bef当前数的约数前一次出现的位置
int maxv[N*],n,bef[N],e,a[N],maxn[N];
struct que{
int l,r,id;
}q[N];
bool cmp(que x,que y){
return x.r<y.r;
}
void pushup(int rt){
maxv[rt]=max(maxv[rt<<],maxv[rt<<|]);
}
void build(int l,int r,int rt){
maxv[rt]=;
if(l==r)return;
int m=(l+r)>>;
build(lson);
build(rson);
}
void update(int p,int v,int l,int r,int rt){
if(l==r){
maxv[rt]=max(maxv[rt],v);
return;
}
int m=(l+r)>>;
if(p<=m)update(p,v,lson);
else update(p,v,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
return maxv[rt];
}
int m=(l+r)>>;
int num=;
if(L<=m)num=max(num,query(L,R,lson));
if(R>m)num=max(num,query(L,R,rson));
return num;
}
void solve(){
sort(q+,q+e+,cmp);
memset(bef,,sizeof(bef));
build(,n,);
int b=;
for(int i=;i<=n;++i){
for(int j=;j*j<=a[i];j++){
if(a[i]%j==){
if(bef[j]){
update(bef[j],j,,n,);
}
int tmp=a[i]/j;
if(bef[tmp]&&tmp!=j)
update(bef[tmp],tmp,,n,);
bef[j]=i;
bef[tmp]=i;
}
}
while(b<=e&&q[b].r==i){
if(q[b].l==q[b].r)
maxn[q[b].id]=;
else{
maxn[q[b].id]=query(q[b].l,q[b].r,,n,);
}
b++;
}
}
for(int i=;i<=e;++i)
printf("%d\n",maxn[i]);
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<=n;++i)
scanf("%d",&a[i]);
scanf("%d",&e);
for(int i=;i<=e;++i){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
}
solve();
}
return ;
}

HDU 4630-No Pain No Game(线段树+离线处理)的更多相关文章

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

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

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

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

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

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

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

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

  5. HDU 4630 No Pain No Game 线段树 和 hdu3333有共同点

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

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

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

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

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

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

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

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

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

  10. 线段树+离线 hdu5654 xiaoxin and his watermelon candy

    传送门:点击打开链接 题意:一个三元组假设满足j=i+1,k=j+1,ai<=aj<=ak,那么就好的.如今告诉你序列.然后Q次询问.每次询问一个区间[l,r],问区间里有多少个三元组满足 ...

随机推荐

  1. 2010年山东省第一届ACM大学生程序设计竞赛 Balloons (BFS)

    题意 : 找联通块的个数,Saya定义两个相连是 |xa-xb| + |ya-yb| ≤ 1 ,但是Kudo定义的相连是 |xa-xb|≤1 并且 |ya-yb|≤1.输出按照两种方式数的联通块的各数 ...

  2. SDUT1586 计算组合数(组合数)

    这个题数据量小,不容易超时. #include<stdio.h> long long fac(int n) { ; ; i <= n ; i++) { m = i*m; } retu ...

  3. Test Markdown Editor

    Last night, I just saw a cute blogger's homepage. Then I want to write something. But anyway, I use ...

  4. C# 虚方法的重载 new 与 virtual

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  5. Rockethon 2015

    A Game题意:A,B各自拥有两堆石子,数目分别为n1, n2,每次至少取1个,最多分别取k1,k2个, A先取,最后谁会赢. 分析:显然每次取一个是最优的,n1 > n2时,先手赢. 代码: ...

  6. Shell最多支持多少个参数

    本文转自:http://www.jb51.net/article/56548.htm   这篇文章主要介绍了Shell最多支持多少个参数?本文是对Shell最多可以输入多少个参数的一篇测试文章,需要的 ...

  7. 代码自动生成工具_java版

    项目结构: 这里要实现的功能是,当我们给出了bean,如:Admin,User,People等实体类后, 我想用代码自动生成我想要的代码,最后生成的效果: 也就是说为每一个bean都生成相应的Dao, ...

  8. 在Eclipse下debug 出现Source not found for ...

    在Eclipse下debug 出现Source not found for ... 在Eclipse下调试Servlet出现了Source not found for XxxAction.execut ...

  9. Servlet3.0的新特性

    注意:Servlet3.0的项目一定要使用Tomcat7.0才能看到效果!! 1.新增标注支持     在Servlet3.0的部署描述文件web.xml的顶层标签<web-app>中有一 ...

  10. android4.4.2内核移植3.4.1

    01◑ 内核源码总目录下Makefile文件修改: 195行: ARCH ?= $(SUBARCH)替换成: ARCH ?= arm 02◑ arch/arm下makefile: ①差7行 # tes ...