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

Problem Description
Life is a game,and you lose it,so you suicide.
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.
 
Input
First line contains a number T(T <= 5),denote the 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.
 
Output
For each test cases,for each query print the answer in one line.
 
Sample Input
1
10
8 2 4 9 5 7 10 6 1 3
5
2 10
2 4
6 9
1 4
7 10
 
Sample Output
5
2
2
4
3
 
Source
 
Recommend
zhuyuanchen520
 

题目给出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题 离线处理+树状数组求最值)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  4. HDU 1394 Minimum Inversion Number (树状数组求逆序对)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题目让你求一个数组,这个数组可以不断把最前面的元素移到最后,让你求其中某个数组中的逆序对最小是多 ...

  5. HDU 4705 Y (2013多校10,1010题,简单树形DP)

    Y Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submiss ...

  6. HDU 4638 Group (2013多校4 1007 离线处理+树状数组)

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  7. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  8. HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...

  9. HDU 5792:World is Exploding(树状数组求逆序对)

    http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Problem Description   Given a sequ ...

随机推荐

  1. PhysX SDK src

    PhysX SDK src Physx3.3 source code http://download.csdn.net/download/qq122252656/9427387 Nvidia CUDA ...

  2. 微信小程序时钟(xx年xx月xx日xx:xx格式)

    wxml: <view>时间:{{newTime}}</view> js: page({ data:{ newTime:'' }, onLoad: function (opti ...

  3. vsftpd 虚拟用户配置

    vsftpd 虚拟用户的作用是 通过不同的虚拟用户可以有不同的根目录. 从 2.3.5 版本之后,vsftpd增强了安全检查,如果用户被限定在了其主目录下,则该用户的主目录不能在具有写权限了,如果检查 ...

  4. mui框架 页面无法滚动解决方法

    只需要初始化一下就可以了 mui.init(); 加下面这段代码即可: (function($){ $(".mui-scroll-wrapper").scroll({ //boun ...

  5. POJ 2492 A Bug's Life(带权并查集)

    题目链接:http://poj.org/problem?id=2492 题目大意:有n只虫子,m对关系,m行每行有x y两个编号的虫子,告诉你每对x和y都为异性,先说的是对的,如果后面给出关系与前面的 ...

  6. LocalStorage、SessionStorage使用详解

    https://blog.csdn.net/zhongzh86/article/details/55504381

  7. AC日记——[SDOI2017]相关分析 洛谷 P3707

    [SDOI2017]相关分析 思路: 裸线段树: (玄学ac): 代码: #include <bits/stdc++.h> using namespace std; #define max ...

  8. 友盟移动开发平台.NET版本SDK

    由于项目需要给安卓.ios提供提送消息服务.找到了umeng这个平台,官方竟然没有提供.net版本的SDK,同时项目需要就拿出来和大家分享一下需要的同学们可以做个参考,建议官方提供.net版本. 这里 ...

  9. 自建yum源(只演示nginx服务,其它都一样)

    (1)概述 (2)yum server端配置 1)关闭防火墙和selinux systemctl stop firewalld systemctl disable firewalld sed -ri ...

  10. docker集群

    http://blog.csdn.net/zhaoguoguang/article/details/51161957