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内建方法
abs all any apply basestring bin bool buffer bytearray bytes callable chr classmethod cmp coerce com ...
- 2.C 基础
C 基础 原文地址:http://rypress.com/tutorials/objective-c/c-basics OC 可以说是C语言的一个超集,这样你可以无缝的和C语言结合编程也就是你可以这两 ...
- saltstack安装和配置
[root@web9 salt]# vi filetest.sls //ADD file_test: file.managed: - name: /tmp/lulu.com - source: sal ...
- 步骤一:下载jdk并安装和配置java环境变量
1.下载JDk地址: http://download.eclipse.org/oomph/jre/?vm=1_1_7_0_64_0 2.进入下载页面(下载的是jdk7),点击:Oracle JDK1. ...
- P2327
code[class*="language-"] { padding: .1em; border-radius: .3em; white-space: normal; backgr ...
- 【python】if __name__ == '__main__'
转载自:http://www.cnblogs.com/xuxm2007/archive/2010/08/04/1792463.html 当你打开一个.py文件时,经常会在代码的最下面看到if __na ...
- 《java并发编程实战》读书笔记4--基础构建模块,java中的同步容器类&并发容器类&同步工具类,消费者模式
上一章说道委托是创建线程安全类的一个最有效策略,只需让现有的线程安全的类管理所有的状态即可.那么这章便说的是怎么利用java平台类库的并发基础构建模块呢? 5.1 同步容器类 包括Vector和Has ...
- 前端获得session信息方式对比,优化
在开发中,页面 js 经常会遇到需要 当前登录用户信息(菜单权限,用户基本信息,配置信息) 的地方,一般情况我们可能对这些信息获取方式不是太在意,但是现在的前端通过webpack打包,即使做了代码分割 ...
- textarea 内容不可编辑 自是适应高度 只读相关属性配置
不可编辑: <textarea disabled="disabled"></textarea> 如果仅仅用于禁止输入(只读,但有用),使用readonly属 ...
- 基于Shiro,JWT实现微信小程序登录完整例子
小程序官方流程图如下,官方地址 : https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html ...