[hdu4630] No Pain No Game
某次模拟赛的T1。
刚开始怀疑是RMQ......我真是太弱了QAQ
正解是离线操作,把所有询问按r从小到大排序。
然后把数从左到右处理,处理完第i个数,就可以回答所有r==i的询问了。
分解因数,记录下来每个因数上一次出现的位置。
又遇到这个因数,就把上一次的位置加进线段树,把这一次的位置记录下来。
这样线段树里有这个数当且仅当这个数出现了两次以上。
此时这个数显然是备选的合法答案之一,然后询问的时候询问区间最大值就行了。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; int t;
int n,m;
int a[];
int pre[]; struct query
{
int ql,qr,ans,id;
void get(){scanf("%d%d",&ql,&qr);}
}q[]; int cmp(query w,query e){return w.qr<e.qr;}
int cmpb(query w,query e){return w.id<e.id;} int mx[],lb[],rb[]; void build(int p,int l,int r)
{
lb[p]=l,rb[p]=r,mx[p]=-;
if(l==r)return;
int mid=(l+r)>>;
build(p<<,l,mid);
build(p<<|,mid+,r);
} void pushup(int p)
{
mx[p]=max(mx[p<<],mx[p<<|]);
} void change(int p,int pos,int val)
{
if(lb[p]==rb[p])
{
mx[p]=max(mx[p],val);
return;
}
int mid=(lb[p]+rb[p])>>;
if(pos<=mid)change(p<<,pos,val);
else change(p<<|,pos,val);
pushup(p);
} int ask(int p,int l,int r)
{
if(l<=lb[p]&&r>=rb[p])return mx[p];
int ret=-,mid=(lb[p]+rb[p])>>;
if(l<=mid)ret=max(ask(p<<,l,r),ret);
if(r>mid)ret=max(ask(p<<|,l,r),ret);
return ret;
} void work()
{
scanf("%d",&n);
build(,,n);
memset(pre,,sizeof(pre));
for(int i=;i<=n;i++)scanf("%d",&a[i]);
scanf("%d",&m);
for(int i=;i<=m;i++)q[i].get(),q[i].id=i;
sort(q+,q+m+,cmp);
int nw=;
for(int i=;i<=n;i++)
{
for(int k=;k*k<=a[i];k++)
{
if(a[i]%k)continue;
if(pre[k])change(,pre[k],k);
if(pre[a[i]/k]&&a[i]!=k*k)
change(,pre[a[i]/k],a[i]/k);
pre[k]=pre[a[i]/k]=i;
}
while(q[nw].qr==i)
{
if(q[nw].ql>=q[nw].qr)q[nw].ans=;
else q[nw].ans=ask(,q[nw].ql,q[nw].qr);
nw++;
}
}
sort(q+,q+m+,cmpb);
for(int i=;i<=m;i++)printf("%d\n",q[i].ans);
} int main()
{
scanf("%d",&t);
while(t--)work();
return ;
}
[hdu4630] No Pain No Game的更多相关文章
- 【HDU4630 No Pain No Game】 dp思想+线段树的离线操作
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4630 题意:给你n个数据范围在[1,n]中的数,m个操作,每个操作一个询问[L,R],让你求区间[L, ...
- 2013 Multi-University Training Contest 3
HDU-4622 Reincarnation 题意:给定一个字符串,有Q次询问,每次询问得出区间[L, R]内有多少个不同的子串. 分析:后缀数组搞,不过hash+dp也能够搞定这题,详解见http: ...
- Your pain
Your pain is the breaking of the shell that encloses your understanding. 你的痛苦是你那包裹知识的皮壳的破裂.
- 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 ...
- No Pain No Game
hdu4630:http://acm.hdu.edu.cn/showproblem.php?pid=4630 题意:给定一个排序,求区间最大GCD. 题解:离散树状数组.首先把查询按左端点从大到小排序 ...
- Pain for friend
For a guy who has experienced his fair share of mysteries,on mystery,I still can't figure out is why ...
- etymon word write alb pain high alt increase large agency ag lose weight fat assist out~3
1● alb 2● write =====>rait 1● alg 2● pain 痛,疼痛 1● alt 2● high 高 1 ...
- 41 Pain and Pain Management 疼痛与疼痛管理
Pain and Pain Management 疼痛与疼痛管理 ①Years ago,doctors often said that pain was a normal part of life.I ...
- HDU 4630 No Pain No Game(2013多校3 1010题 离线处理+树状数组求最值)
No Pain No Game Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
随机推荐
- JQuery select,checkbox用法 文本框只能输入数字
记录一下,方便查找 a.文本框只能输入数字 onkeyup='this.value=this.value.replace(/\D/gi,"")' eg: <input typ ...
- [Algo] 117. Array Deduplication III
Given a sorted integer array, remove duplicate elements. For each group of elements with the same va ...
- 吴裕雄--天生自然ShellX学习笔记:Shell 数组
数组中可以存放多个值.Bash Shell 只支持一维数组(不支持多维数组),初始化时不需要定义数组大小(与 PHP 类似). 与大部分编程语言类似,数组元素的下标由0开始. Shell 数组用括号来 ...
- Problem for Nazar CodeForces - 1151C (前缀和)
Problem for Nazar Nazar, a student of the scientific lyceum of the Kingdom of Kremland, is known for ...
- Promoter complex|转录组水平RNA的复杂度|
生命组学 Promoter complex Tata box识别位点 Enhancer加入之后增强转录 不确定性与确定性之间的关系,原因中存在这不确定性,但是结果表达又是确定的.因为promoter的 ...
- 推荐:MongoDB学习资料
http://www.mongodb.org/display/DOCS/Production+Deployments Official MongoDBProject Website Getting S ...
- Java常见异常说明汇总
1. java.lang.nullpointerexception 这个异常大家肯定都经常遇到,异常的解释是"程序遇上了空指针",简单地说就是调用了未经初始化的对象或者是不存在的对 ...
- [Algo] 397. Right Shift By N Characters
Right shift a given string by n characters. Assumptions The given string is not null. n >= 0. Exa ...
- Nuxt.js学习(二) --- Nuxt目录结构详解、Nuxt常用配置项、Nuxt路由配置和参数传递
[TOC] 1.Nuxt目录结构详解 Nuxt项目文件目录结构 |-- .nuxt // Nuxt自动生成,临时的用于编辑的文件,build |-- assets // 用于组织未编译的静态资源入LE ...
- 模型层字段-多表查询-神奇的双下划线查询-F,Q查询
Django ORM中常用的字段和参数 常用字段 AutoField int自增列,必须填入参数 primary_key=True.当model中如果没有自增列,则自动会创建一个列名为id的列. In ...