HDU4630-No Pain No Game(离线,线段树)
Problem Description
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
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
Sample Input
Sample Output
题目大意:
解题思路:
想总结一下,感觉自己好菜啊。
两两取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(离线,线段树)的更多相关文章
- hdu 4288 离线线段树+间隔求和
Coder Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- bzoj2333 离线 + 线段树
https://www.lydsy.com/JudgeOnline/problem.php?id=2333 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来 ...
- HDU 5700 区间交 离线线段树
区间交 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5700 Description 小A有一个含有n个非负整数的数列与m个区间.每个区间可以表示为 ...
- BZOJ 3626 [LNOI2014]LCA 树剖+(离线+线段树 // 在线+主席树)
BZOJ 4012 [HNOI2015]开店 的弱化版,离线了,而且没有边权(长度). 两种做法 1 树剖+离线+线段树 这道题求的是一个点zzz与[l,r][l,r][l,r]内所有点的lcalca ...
- HDU 4630-No Pain No Game(线段树+离线处理)
题意: 给你n个数的序列a,q个询问,每个询问给l,r,求在下标i在[l,r]的区间任意两个数的最大公约数中的最大值 分析: 有了hdu3333经验,我们从左向右扫序列,如果当前数的约数在前面出现过, ...
- 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 ...
- 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 ...
- 【BZOJ 2333 】[SCOI2011]棘手的操作(离线+线段树)
2333: [SCOI2011]棘手的操作 Description 有N个节点,标号从1到N,这N个节点一开始相互不连通.第i个节点的初始权值为a[i],接下来有如下一些操作: U x y: 加一条边 ...
- HDU 4605 Magic Ball Game (在线主席树|| 离线 线段树)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一棵二叉树,每个结点孩子数目为0或者2. ...
- 求余区间的求和类问题 离线+线段树 HDU4228
题目大意:给一个数组a,他的顺序是严格的单调增,然后有如下三个操作 ①加入一个val到a数组里面去,加入的位置就是a[i-1]<val<a[i+1] ②删除一个a[i]=val的值 ③查询 ...
随机推荐
- vim 脚本之快速打印log
" zsl_log.vim " Version: 1.0 if exists("g:zsl_loaded_log") || &cp || v:versi ...
- centos 7.3 配置vnc 服务 图形界面登录
1.检查系统是否有安装tigervnc-server软件包 rpm -qa |grep vnc 默认的系统未装tigervnc-server软件包 2.安装tigervnc-server软件包 yum ...
- Lambda表达式详细总结
(一)输入参数 在Lambda表达式中,输入参数是Lambda运算符的左边部分.它包含参数的数量可以为0.1或者多个.只有当输入参数为1时,Lambda表达式左边的一对小括弧才可以省略.输入参数的数量 ...
- ping 原理及ICMP协议简介
//Ping IP/域名 public static String pingIPCennect(String ipStr) { String result = ""; if ( ...
- Yeslab 华为安全HCIE七门之-防火墙基础(12篇)
Yeslab 华为安全HCIE七门之-防火墙基础(12篇) Yeslab 全套华为安全HCIE七门之第二门防火墙基础(12篇),第一门课论坛很早就有了,可自行下载,后面的陆续分享给大家. 华为安全HC ...
- Git 远程仓库默认权限问题的解决
多人共同开发维护一个项目时,对整个项目文件互有拉取.推送等行为.为防止操作时文件权限出现冲突,可有以下2种方法解决: 1. 本地git的远端设置中,连接远程仓库时多人使用同一个用户名,该用户名为git ...
- Jsp学习总结(1)——JSP九大内置对象和四种属性范围解读
一.四种属性范围 1.1.在JSP中提供了四种属性保存范围 page:在一个页面内保存属性,跳转之后无效 request:在一次服务请求范围内,服务器跳转后依然有效 session:-在一次会话范围内 ...
- IsoAlgo3d - IDF/PCF pipeline 3d viewer
IsoAlgo3d - IDF/PCF pipeline 3d viewer eryar@163.com Key Words. IDF, PCF, IsoAlgo, 3D 当前国际主流管道设计软件都可 ...
- vim 常用插件功能跟配置
在之前的公司,一直是使用别人配置好的vim 环境,他当时配置的功能很强大,查看源码的时候,非常的方便.至少我一直都是用它来看源码,从来没有使用过source insight.现在换了工作,但之前养成的 ...
- android-开发环境相关概念
Android中IDE.ADT.SDK.JDK.NDK的解释 1. IDE: Intelligent Development Environm的简称.即智能开发环境.是一种开发工具.常用的IDE有ad ...