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)
Total Submission(s): 17 Accepted Submission(s): 5
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.
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.
10
8 2 4 9 5 7 10 6 1 3
5
2 10
2 4
6 9
1 4
7 10
2
2
4
3
题目给出n个数,每个数的范围是1~n的。
n<=50000;
然后查询m次,m<=50000
每次查询[l,r]区间内,两个数的gcd的最大值.
n个数,如果把n个数的约数全部写出来。查询[l,r]之间的gcd的最大值,就相当于找一个最大的数,使得这个数是[l,r]之间至少是两个的约数。
对于一个数n,在sqrt(n)内可以找出所有约数。
我的做法是对查询进行离线处理。
将每个查询按照 l 从大到小排序。
然后 i 从 n~0 ,表示从后面不断扫这些数。
对于数a[i],找到a[i]的所有约数,对于约数x,在x上一次出现的位置加入值x.
这样的查询的时候,只要差值前 r 个数的最大值就可以了。
看代码吧,不解释了。
这么水竟然想了这么久,非常sad
/*
* Author:kuangbin
* 1010.cpp
*/ #include <stdio.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <map>
#include <vector>
#include <queue>
#include <set>
#include <string>
#include <math.h>
using namespace std; const int MAXN = ;
int c[MAXN];
int n;
int lowbit(int x)
{
return x&(-x);
}
void add(int i,int val)
{
while(i <= n)
{
c[i] = max(c[i],val);
i += lowbit(i);
}
}
int Max(int i)
{
int s = ;
while(i > )
{
s = max(s,c[i]);
i -= lowbit(i);
}
return s;
} int a[MAXN];
int b[MAXN];
int ans[MAXN]; struct Node
{
int l,r;
int index;
}node[MAXN]; bool cmp(Node a,Node b)
{
return a.l > b.l;
} int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
int m;
int l,r;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i = ;i <= n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(int i = ;i < m;i++)
{
scanf("%d%d",&node[i].l,&node[i].r);
node[i].index = i;
}
sort(node,node+m,cmp);
int i = n;
int j = ;
memset(b,,sizeof(a));
memset(c,,sizeof(c));
while(j < m)
{
while(i > && i>= node[j].l)
{
for(int k =;k*k <= a[i];k++)
{
if(a[i]%k == )
{
if(b[k]!=)
{
add(b[k],k);
} b[k] = i;
if(k != a[i]/k)
{
if(b[a[i]/k]!=)
{
add(b[a[i]/k],a[i]/k);
} b[a[i]/k]=i;
}
}
}
i--;
}
while(j < m && node[j].l > i)
{
ans[node[j].index]=Max(node[j].r);
j++;
}
}
for(int i = ;i < m;i++)
printf("%d\n",ans[i]);
}
return ;
}
HDU 4630 No Pain No Game(2013多校3 1010题 离线处理+树状数组求最值)的更多相关文章
- HDU 4685 Prince and Princess (2013多校8 1010题 二分匹配+强连通)
Prince and Princess Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- HDU 4675 GCD of Sequence (2013多校7 1010题 数学题)
GCD of Sequence Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)T ...
- HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number ...
- HDU 1394 Minimum Inversion Number (树状数组求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...
- HDU 4705 Y (2013多校10,1010题,简单树形DP)
Y Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submiss ...
- HDU 4638 Group (2013多校4 1007 离线处理+树状数组)
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...
- HDU 5792:World is Exploding(树状数组求逆序对)
http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Problem Description Given a sequ ...
随机推荐
- python实战===一句python代码搭建FTP服务
环境搭建: python windows/linux pip install pyftpdlib (安装失败请到这里下载:https://pypi.python.org/pypi/pyftpdlib ...
- python设计模式之单例模式(二)
上次我们简单了解了一下什么是单例模式,今天我们继续探究.上次的内容点这 python设计模式之单例模式(一) 上次们讨论的是GoF的单例设计模式,该模式是指:一个类有且只有一个对象.通常我们需要的是让 ...
- Android IPC
1. 什么是Android IPC IPC:inter-process Commnication跨进程的通信,多进程之间的通信,不同的操作系统有不同的通信方式,Android继承自Linux,但其IP ...
- Kettle使用介绍——Kettle的安装与基本使用
下面的链接是原文 http://www.cnblogs.com/limengqiang/archive/2013/01/16/KettleApply1.html
- JAVA中的数据存储(堆及堆栈)
转自:http://www.iteye.com/topic/6345301.寄存器:最快的存储区, 由编译器根据需求进行分配,我们在程序中无法控制.2. 栈:存放基本类型的变量数据和对象的引用,但对象 ...
- 3:django models Making queries 高级进阶--聚合运算
在前一遍文章django models Making queries里面我们提到了django常用的一些检索数据库的内容, 下面我们来看一下更为高级的检索聚合运算 这是我们要用到的模型 class A ...
- 使用Guava retryer优雅的实现接口重调机制
API 接口调用异常, 网络异常在我们日常开发中经常会遇到,这种情况下我们需要先重试几次调用才能将其标识为错误并在确认错误之后发送异常提醒.guava-retry可以灵活的实现这一功能.Guava r ...
- 机器学习方法:回归(二):稀疏与正则约束ridge regression,Lasso
欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.net/xbinworld. "机器学习方法"系列,我本着开放与共享(open and share)的精神撰写,目的是 ...
- hdu 1399(水题)
Starship Hakodate-maru Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- hdu 1506(好题+DP或者RMQ)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...