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

Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

Submit Status

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 a 1, a 2, ..., a n.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 a 1, a 2, ..., a n

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
题意:求解给予[i , j]区间内随意两个值的最大gcd,而且输出它
因为数据一一去处理,复杂度肯定很大,所以要进行离线处理
详细内容,提供一个大牛博客:http://m.blog.csdn.net/blog/u010033217/38156507

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define root 1, 1, N
const int MAXN = 5e4 + 5;
int T, N, A[MAXN], Q, pre[MAXN], Sum[MAXN << 2], ans[MAXN];
struct qeu {
int l, r, id;
bool operator < (const qeu & a) const {
return r < a.r;
}
} QS[MAXN]; vector<int>G[MAXN]; void init() {
for(int i = 1; i < MAXN; i ++) {
for(int j = i ; j < MAXN; j += i) {
G[j].push_back(i);
}
}
} void pushup(int rt) {
Sum[rt] = max(Sum[rt << 1], Sum[rt << 1|1]);
} void build(int rt, int l, int r) {
Sum[rt] = 0;
if(l == r) return ;
int mid = (l + r) >> 1;
build(lson);
build(rson);
} void update(int p, int v, int rt, int l, int r) {
if(l == r) {
Sum[rt] = max(Sum[rt], v);
return;
}
int mid = (l + r) >> 1;
if(p <= mid) update(p, v, lson);
else update(p, v, rson);
pushup(rt);
} int query(int L, int R, int rt, int l, int r) {
if(L <= l && r <= R) {
return Sum[rt];
}
int mid = (l + r) >> 1;
int ret = 0;
if(L <= mid) ret = max(ret, query(L, R, lson));
if(R > mid) ret = max(ret, query(L, R, rson));
return ret;
} int main() {
init();
//freopen("D://imput.txt", "r", stdin);
scanf("%d", &T);
while(T --) {
scanf("%d", &N);
build(root);
for(int i = 1; i <= N; i ++) {
scanf("%d", &A[i]);
}
scanf("%d", &Q);
for(int i = 1; i <= Q; i ++) {
scanf("%d%d", &QS[i].l, &QS[i].r);
QS[i].id = i;
}
memset(pre, -1, sizeof(pre));
sort(QS + 1, QS + Q + 1);
for(int i = 1, j = 1; i <= N && j <= Q; i ++) {
for(int k = 0 ; k < G[A[i]].size(); k ++) {
int tmp = G[A[i]][k];
if(pre[tmp] != -1) {
update(pre[tmp], tmp, root);
}
pre[tmp] = i;
}
while(j <= Q && QS[j].r == i) {
ans[QS[j].id] = query(QS[j].l, QS[j].r, root);
j ++;
}
}
for(int i = 1; i <= Q; i ++) {
printf("%d\n", ans[i]);
}
}
return 0;
}




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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 初涉期望dp/概率dp【在更】

    大致可以分为两种:爆精度:小数取模.

  2. 【Java_多线程并发编程】JUC原子类——4种原子类

    根据修改的数据类型,可以将JUC包中的原子操作类可以分为4种,分别是: 1. 基本类型: AtomicInteger, AtomicLong, AtomicBoolean ;2. 数组类型: Atom ...

  3. GIMP语言设置

    初学GIMP,需要设置语言:点击 编辑 - 首选项 其他的配置如: 配置快捷键 自己熟悉吧!

  4. Java学习经验

    随着Java学习的深入,越来越感觉记笔记的重要性,一方面可以使自己更加善于总结,提高对项目和自己的认知,另一方面可以让知识条例更加鲜明,并且加深对知识点的记忆.Java是一门很早开始兴起的语言,用途非 ...

  5. centos 7 中文乱码的解决办法

    @@首先查看系统的操作版本,我的版本是centos 7.2 的. @@查看系统是否有安装中文语言包,一般我们在安装的时候系统都会默认的为我们安装上去的. locale -a | grep " ...

  6. zabbix:告警、恢复消息次数

    之前zabbix配置告警,存在告警信息发送多次并且恢复信息也跟着发送多次了,导致企业微信流量不够用,没有找到恢复信息单独的设置项 动作中的步骤我个人理解为:1-5的意思是发送5条告警消息      3 ...

  7. SPOJ-Matrices with XOR property,暴力打表!

    Matrices with XOR property 应该先去看看这题的,补题的时候发现这题其实挺简单的.. 题意:n*m的格子用1-n*m的数去填,要求如果一个格子(i1,j1)与另外一个格子(i2 ...

  8. 3.ruby语法基础,全部变量,实例变量,类变量,局部变量的使用和注意的要点

    1.ruby的全局变量的概念和Java的全局变量的概念不同, ruby的全局变量是以$符号开头的,如果给全局变量的初始化值为nil会出现警告. 赋值给全局变量,这是ruby不推荐的,这样会使程序变得很 ...

  9. 【Luogu】P1306斐波那契公约数(递推)

    题目链接 有个定理叫gcd(f(n),f(m))=f(gcd(n,m)) 所以递推就好了. #include<cstdio> #include<cstdlib> #includ ...

  10. 怎么创建SpringBoot项目

    上述中讲到了怎么创建SpringBoot项目,那么现在就来介绍下SpringBoot配置文件的两种格式yml和properties 首先呢发上一份application.properties 在放上一 ...