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

题目大意:

给一个序列,然后q次询问[l,r]询问两两gcd最大值是多少(不能是同一个数),无解输出0;

解题思路:

想总结一下,感觉自己好菜啊。

两两取gcd,最后的值肯定是该数的约数。

也就是说,数是可以一个一个加入的。

现将约数分解。

一个数被加入。如果他的约数k上一次在p处,那么询问左端点<=p的话答案就至少值为k。

就离线处理一下。

保证加入的点来更新答案。

我觉得和HH的项链有点像。

代码:

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define lll spc<<1
#define rrr spc<<1|1
using std::max;
struct qst{
int l,r;
int no;
int ans;
void Insert(int i)
{
no=i;
ans=;
scanf("%d%d",&l,&r);
return ;
}
}q[];
int maxs[];
int a[];
int lst[];
int n,Q;
int T;
bool cmp(qst x,qst y)
{
return x.r<y.r;
}
bool cmq(qst x,qst y)
{
return x.no<y.no;
}
void Reset(void)
{
memset(lst,,sizeof(lst));
memset(maxs,,sizeof(maxs));
return ;
}
void pushup(int spc)
{
maxs[spc]=max(maxs[lll],maxs[rrr]);
return ;
}
void update(int spc,int l,int r,int v,int pos)
{
if(l==r)
{
maxs[spc]=max(v,maxs[spc]);
return ;
}
int mid=(l+r)>>;
if(pos<=mid)
update(lll,l,mid,v,pos);
else
update(rrr,mid+,r,v,pos);
pushup(spc);
return ;
}
int query(int l,int r,int ll,int rr,int spc)
{
if(l>rr||ll>r)
return -0x3f3f3f3f;
if(ll<=l&&r<=rr)
return maxs[spc];
int mid=(l+r)>>;
return max(query(l,mid,ll,rr,lll),query(mid+,r,ll,rr,rrr));
}
int main()
{
scanf("%d",&T);
while(T--)
{
Reset();
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&Q);
for(int i=;i<=Q;i++)
q[i].Insert(i);
std::sort(q+,q+Q+,cmp);
int j=;
for(int i=;i<=n;i++)
{
for(int k=;k*k<=a[i];k++)
{
if(a[i]%k==&&lst[k])
update(,,n,k,lst[k]);
if(a[i]%k==&&lst[a[i]/k]&&k!=a[i]/k)
update(,,n,a[i]/k,lst[a[i]/k]);
if(a[i]%k==)
lst[k]=lst[a[i]/k]=i;
}
while(q[j].r==i)
{
if(j>Q)
break;
if(q[j].l>=q[j].r)
q[j].ans=;
else
q[j].ans=query(,n,q[j].l,q[j].r,);
j++;
}
}
std::sort(q+,q+Q+,cmq);
for(int i=;i<=Q;i++)
printf("%d\n",q[i].ans);
}
return ;
}

HDU4630-No Pain No Game(离线,线段树)的更多相关文章

  1. hdu 4288 离线线段树+间隔求和

    Coder Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  2. bzoj2333 离线 + 线段树

    https://www.lydsy.com/JudgeOnline/problem.php?id=2333 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来 ...

  3. HDU 5700 区间交 离线线段树

    区间交 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5700 Description 小A有一个含有n个非负整数的数列与m个区间.每个区间可以表示为 ...

  4. BZOJ 3626 [LNOI2014]LCA 树剖+(离线+线段树 // 在线+主席树)

    BZOJ 4012 [HNOI2015]开店 的弱化版,离线了,而且没有边权(长度). 两种做法 1 树剖+离线+线段树 这道题求的是一个点zzz与[l,r][l,r][l,r]内所有点的lcalca ...

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

    题意: 给你n个数的序列a,q个询问,每个询问给l,r,求在下标i在[l,r]的区间任意两个数的最大公约数中的最大值 分析: 有了hdu3333经验,我们从左向右扫序列,如果当前数的约数在前面出现过, ...

  6. HDU 4031 Attack(离线+线段树)(The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4031 Problem Description Today is the 10th Annual of ...

  7. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树

    题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...

  8. 【BZOJ 2333 】[SCOI2011]棘手的操作(离线+线段树)

    2333: [SCOI2011]棘手的操作 Description 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边 ...

  9. HDU 4605 Magic Ball Game (在线主席树|| 离线 线段树)

    转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove 题意:给出一棵二叉树,每个结点孩子数目为0或者2. ...

  10. 求余区间的求和类问题 离线+线段树 HDU4228

    题目大意:给一个数组a,他的顺序是严格的单调增,然后有如下三个操作 ①加入一个val到a数组里面去,加入的位置就是a[i-1]<val<a[i+1] ②删除一个a[i]=val的值 ③查询 ...

随机推荐

  1. uip UDP server广播模式(client能够随意port,而且主动向client发送数据)

    眼下移植uip,发现UDP server模式下,必须指定本地port以及clientport,否则仅仅能讲clientport设置为0,才干接收随意port的数据,可是无法发送数据,由于此时clien ...

  2. HIT 2255 Not Fibonacci(矩阵高速幂)

    #include <iostream> #include <cstdio> #include <cstring> using namespace std; cons ...

  3. tp5项目搭建思路

    按照需求,创建主体的目录结构,一般包括管理后台admin,前台展示index,app接口api. admin中又包含controller,model,view,其他等等. 一些js,css,image ...

  4. export和source的区别

    1.执行脚本是在一个子shell环境运行的,脚本执行完后该子shell自动退出. 2.执行脚本中的系统环境变量(用export定义的变量)才会被复制到子shell中. 3.一个shell中的系统环境变 ...

  5. Linux企业应用--RHAS 2.1 下安装中文 Lotus Domino R 6.5 图解

    原文请到ftp.jms165.com下载,是用上传用户                                                 (RHAS3+ksnapshot+OperOff ...

  6. 分享一下js正则中惰性与贪婪

    首先引入一个介绍比较详细的网站 http://www.cnblogs.com/yuaima/p/5258513.html http://www.jb51.net/article/31491.htm 接 ...

  7. SP1026 FAVDICE - Favorite Dice 数学期望

    题目描述: 一个n面的骰子,求期望掷几次能使得每一面都被掷到. 题解:先谈一下期望DP. 一般地,如果终止状态固定,我们都会选择逆序计算. 很多题目如果顺序计算会出现有分母为 0 的情况,而逆序计算中 ...

  8. Linux Cgroups

    目录 Linux Cgroups Cgroups中的三个组件 三个组件的关系 Kernel接口 Docker是如何使用Cgroups的 Go语言实现Cgroups限制容器资源 Linux Cgroup ...

  9. Linux局域网登陆响应时间过长

    在局域网中,使用ssh登陆到其他机器上时,有时会出现等待10s以上才能正常登陆的问题. 原因: Linux默认使用dns解析登陆IP,但是在局域网,并没有dns服务器,而且机器上也没有添加 IP与域名 ...

  10. Python: PS 滤镜--扇形变换

    本文用 Python 实现 PS 滤镜中的一种几何变换特效,称为扇形变换,将图像扭曲成一个扇形,具体的算法原理和效果图可以参考以前的博客: http://blog.csdn.net/matrix_sp ...