hdu 4630 No Pain No Game
http://acm.hdu.edu.cn/showproblem.php?pid=4630
离散化+树状数组
将数组 *a 从后向前遍历 遍历到 a[x] 的时候 再枚举a[x]的约数 假如 约数 k
last[k] 对应到 上一个k出现的位置 那么可被公约数 k 更新的区间是 last[k] --- 最后
把所有a[x]的约数都处理完之后 从 x 到任意 y(y>=x)形成的段的 最优解都可求
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#include<set>
#include<vector>
#include<list>
using namespace std; typedef long long ll;
typedef pair<double,double>ppd;
const double PI = acos(-1.);
const double eps = (1e-9);
const int N=50001;
struct node
{
int l,r;
int index;
}q[N];
int a[N],last[N],ans[N];
int c[N];
int lowbit(int x)
{
return x&(-x);
}
void update(int i,int k)
{
while(i<N)
{
c[i]=max(c[i],k);
i+=lowbit(i);
}
}
int getM(int i)
{
int tmp=0;
while(i>=1)
{
tmp=max(tmp,c[i]);
i-=lowbit(i);
}
return tmp;
}
bool cmp(const node &x,const node &y)
{
return x.l>y.l;
}
int main()
{
//freopen("data.in","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d",&a[i]);
int Q;
scanf("%d",&Q);
for(int i=0;i<Q;++i)
{
scanf("%d %d",&q[i].l,&q[i].r);
q[i].index=i;
}
sort(q,q+Q,cmp);
memset(c,0,sizeof(c));
memset(last,-1,sizeof(last));
int ln=0;
for(int i=n;i>=1;--i)
{
for(int x=1;x*x<=a[i];++x)
if(a[i]%x==0)
{
if(last[x]!=-1)
update(last[x],x);
last[x]=i;
int y=a[i]/x;
if(x==y)
continue;
if(last[y]!=-1)
update(last[y],y);
last[y]=i;
}
while(ln<Q&&q[ln].l==i)
{
ans[q[ln].index]=getM(q[ln].r);
++ln;
}
}
for(int i=0;i<Q;++i)
printf("%d\n",ans[i]);
}
return 0;
}
hdu 4630 No Pain No Game的更多相关文章
- HDU - 4630 No Pain No Game (线段树 + 离线处理)
id=45786" style="color:blue; text-decoration:none">HDU - 4630 id=45786" style ...
- 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 ...
- 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 ...
- hdu 4630 No Pain No Game(线段树+离线操作)
No Pain No Game Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 4630 No Pain No Game 树状数组+离线操作
题意:给一串数字,每次查询[L,R]中两个数的gcd的最大值. 解法:容易知道,要使取两个数让gcd最大,这两个数最好是倍数关系,所以处理出每个数的所有倍数,两两间根据倍数关系形成一条线段,值为该数. ...
- HDU 4630 No Pain No Game(树状数组)
题目链接 看的别人的题解,离线之后,按r排序,枚举1-n,利用pre[j],存上次j的倍数出现的位置,树状数组里统计的当前位置到最后的最大值,树状数组是求区间最值其实应该很麻烦的,但是此题用法只是求到 ...
- HDU 4630 No Pain No Game 树状数组+离线查询
思路参考 这里. #include <cstdio> #include <cstring> #include <cstdlib> #include <algo ...
- hdu 4630 No Pain No Game 线段树离线处理
题目链接 求出一个区间内任意两个数的gcd的最大值. 先将询问读进来然后按r值排序. 将每一个数因数分解, 对每一个因子x, 如果pre[x]!=-1, 那么就更新update(pre[x], x, ...
- HDU 4630 No Pain No Game (线段树+离线)
题目大意:给你一个无序的1~n的排列a,每次询问[l,r]之间任取两个数得到的最大gcd是多少 先对所有询问离线,然后把问题挂在区间的左端点上(右端点也行) 在预处理完质数,再处理一个next数组,表 ...
随机推荐
- [转载] tmux的使用tips
原文: http://tangosource.com/blog/a-tmux-crash-course-tips-and-tweaks/
- 11 Indexes
本章提要--------------------------------------索引会影响 DML 与 select 操作, 要找到平衡点最好从一开始就创建好索引索引概述B*索引其他一些索引索引使 ...
- poj3565Ants(KM-几何与图论的结合)
链接 可以看出蓝的之和一定比红的之和要大,也就是说符合条件的匹配一定是权值最小的,所以二分图的最佳完美匹配..KM #include <iostream> #include<cstd ...
- Jmeter使用之常用函数介绍
“_csvRead”函数 CsvRead函数是从外部读取参数,CsvRead函数可以从一个文件中读取多个参数. 下面具体讲一下如何使用csvread函数: 1. 新建一个csv或者text文件 ...
- 转:c的回归-云风
C 的回归 周末出差,去另一个城市给公司的一个项目解决点问题.回程去机场的路上,我用手机上 google reader 打发时间.第一眼就看到孟岩大大新的一篇:Linux之父话糙理不糙 .主题是 C ...
- Oracle 修改一行数据内存主要变化
向Oracle 数据库发出请求,修改一行数据,在内存中主要有以下变化: 1. 服务器进程将包含该行数据的块读取到内存中 2. 写redo日志.将内存中该数据块指向undo表空间中数据块的变更向量(Ch ...
- hiho1096_divided_product
题目 给出两个正整数N和M, N <= 100, M <= 50, 可以将N分解成若干个不相等的正整数A1, A2... Ak的和,且A1, A2 ... Ak的乘积为M的倍数.即 N = ...
- Hbase之使用回调函数进行批处理操作
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; impo ...
- B2车
晚上10点之后,杭州快速公交B2的司机,把2节车厢,开的像是跑车一样,每次启动都是弹射出去的,好像是在报复白天蜗牛般的速度.真乃是见神杀神,见佛杀佛.
- innodb_fast_shutdown中值为1或者2的区别是?
innodb_fast_shutdown=0 , 1 , 2时的意思分别是 0 把buffer pool中的脏页刷到磁盘和合并insert buffer,当然包括redo log也会写到磁盘中. 2 ...