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)
Total Submission(s): 1465 Accepted Submission(s):
631
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.
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.
one line.
/**
题意:求解区间[ L , R ] max gcd() ; 如果只求一次,那么我们可以这样做。
把每个数字的因子刷一遍,满足>=2的最大就是结果。
如果多次,可以这样求解。
在区间[ L , R ] 如果素因子出现,更新它上次出现的位置的最大值,
保存当前出现的位置。第一次出现不更新,只保存。 当枚举到R的时候,我们就可以在[ L, R ]中找最大值即可。 所以这一题就可以对r 排序,然后进行更新。
**/
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<vector>
using namespace std; vector<int>yz[];
struct node
{
int l,r,len;
int maxn;
}f[*];
struct node2
{
int st,ed;
int i,val;
}a[];
int date[];
int pre[]; bool cmp(struct node2 n1,struct node2 n2)
{
return n1.ed<n2.ed;
}
bool cmp1(struct node2 n1,struct node2 n2)
{
return n1.i<n2.i;
}
void init()
{
for(int i=;i<=;i++)
for(int j=i;j<=;j=j+i)
yz[j].push_back(i);
}
void build(int l,int r,int n)
{
int mid = (l+r)/;
f[n].l = l;
f[n].r = r;
f[n].len = f[n].r-f[n].l +;
f[n].maxn = ;
if(l==r) return;
build(l,mid,n*);
build(mid+,r,n*+);
}
void update(int wz,int num,int n)
{
int mid=(f[n].l + f[n].r)/;
if(f[n].l == wz && f[n].r == wz)
{
if(f[n].maxn<num)
f[n].maxn = num;
return;
}
if(mid>=wz) update(wz,num,n*);
else if(mid<wz) update(wz,num,n*+);
f[n].maxn = f[n*].maxn>f[n*+].maxn ? f[n*].maxn : f[n*+].maxn;
}
int query(int l,int r,int n)
{
int mid=(f[n].l + f[n].r)/;
if(f[n].l == l && f[n].r == r)
{
return f[n].maxn;
}
if(mid>=r) return query(l,r,n*);
else if(mid<l) return query(l,r,n*+);
else return max(query(l,mid,n*),query(mid+,r,n*+));
}
int main()
{
int T , n , m;
init();
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
build(,n,);
for(int i=;i<=n;i++)
scanf("%d",&date[i]);
scanf("%d",&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&a[i].st,&a[i].ed);
a[i].i = i;
a[i].val = ;
}
sort(a+,a++m,cmp);
memset(pre,,sizeof(pre));
int k = ;
for(int i=;i<=n;i++)
{
int ans = yz[date[i]].size();
for(int j=;j<ans;j++)
{
int temp = yz[date[i]][j];
if(pre[temp])
update(pre[temp],temp,);
pre[temp] = i;
}
while(a[k].ed == i && k<=m)
{
a[k].val = query(a[k].st,a[k].ed,);
k++;
}
}
sort(a+,a++m,cmp1);
for(int i=;i<=m;i++) printf("%d\n",a[i].val);
}
return ;
}
HDU 4630 No Pain No Game 线段树 和 hdu3333有共同点的更多相关文章
- 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 (线段树 + 离线处理)
id=45786" style="color:blue; text-decoration:none">HDU - 4630 id=45786" style ...
- HDU 4630 No Pain No Game (线段树+离线)
题目大意:给你一个无序的1~n的排列a,每次询问[l,r]之间任取两个数得到的最大gcd是多少 先对所有询问离线,然后把问题挂在区间的左端点上(右端点也行) 在预处理完质数,再处理一个next数组,表 ...
- hdu 4630 No Pain No Game 线段树离线处理
题目链接 求出一个区间内任意两个数的gcd的最大值. 先将询问读进来然后按r值排序. 将每一个数因数分解, 对每一个因子x, 如果pre[x]!=-1, 那么就更新update(pre[x], x, ...
- hdu 5274 Dylans loves tree(LCA + 线段树)
Dylans loves tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- HDU 3074.Multiply game-区间乘法-线段树(单点更新、区间查询),上推标记取模
Multiply game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)
HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由 ...
- E - No Pain No Game 线段树 离线处理 区间排序
E - No Pain No Game HDU - 4630 这个题目很好,以后可以再写写.这个题目就是线段树的离线写法,推荐一个博客:https://blog.csdn.net/u01003321 ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
随机推荐
- 免安装版的MySQL的安装与配置
1. 将下载的 mysql-noinstall-5.1.69-win32.zip 解压至需要安装的位置, 如: C:\Program Files; 2. 在安装文件夹下找到 my-small.ini ...
- [原创] hadoop学习笔记:卸载和安装jdk
一,卸载jdk 1.确定jdk版本 #rpm -qa | grep jak 可能的结果: java-1.7.0-openjdk-1.7.0.75-2.5.4.2.el7_0.x86_64 java- ...
- c++命名规则
命名规则根据不同公司有略微不同,这里按照google c++的编程标准1.文件名-全部用小写字母和下划线或横线组成,例如my_useful_class.ccmy-useful-class.ccmyus ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON DirectFile
zw版[转发·台湾nvp系列Delphi例程]HALCON DirectFile unit Unit1;interfaceuses Windows, Messages, SysUtils, Varia ...
- js中把JSON字符串转换成JSON对象最好的方法
在JS中将JSON的字符串解析成JSON数据格式,一般有两种方式: 1.一种为使用eval()函数. 2. 使用Function对象来进行返回解析. 第一种解析方式:使用eval函数来解析,并且使用j ...
- javascript中字符串格式json如何转化成json对象
什么是JSON JSON(JavaScript Object Notation)是一种优美的JavaScript对象创建方法.JSON也是一种轻量级数据交换格式.JSON非常易于人阅读与编写,同时利于 ...
- C语言初学者代码中的常见错误与瑕疵(9)
题目 字母的个数 现在给你一个由小写字母组成字符串,要你找出字符串中出现次数最多的字母,如果出现次数最多字母有多个那么输出最小的那个. 输入:第一行输入一个正整数T(0<T<25) 随后T ...
- jquery 实践总结
Ready事件 对DOM操作之前需要监听页面加载进度,应当在页面加载完成之后再执行DOM编辑操作. $(document).ready(function(){ ... }); 或者 $(functio ...
- Mongodb 笔记06 副本集的组成、从应用程序连接副本集、管理
副本集的组成 1. 同步:MongoDB的复制功能是使用操作日志oplog实现的,操作日志包含了主节点的每一次写操作.oplog是主节点的local数据库中的一个固定集合.备份节点通过查询整个集合就可 ...
- JVM学习笔记(四)------内存调优【转】
转自:http://blog.csdn.net/cutesource/article/details/5907418 版权声明:本文为博主原创文章,未经博主允许不得转载. 首先需要注意的是在对JVM内 ...