No Pain No Game
hdu4630:http://acm.hdu.edu.cn/showproblem.php?pid=4630
题意:给定一个排序,求区间最大GCD。
题解:离散树状数组。首先把查询按左端点从大到小排序。然后用树状数组来维护每个位置出现的最大的公约数。枚举每个数的约数,记录到当前位置为止,上一个x的倍数出现的位置b[x];
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int N=5e4+;
int a[N],c[N],b[N],ans[N];
int n,m;
int lowbit(int x){
return x&(-x);
}
void add(int x,int val){
while(x<=n){
c[x]=max(c[x],val);
x+=lowbit(x);
}
}
int getsum(int x){
int sum=;
while(x>){
sum=max(sum,c[x]);
x-=lowbit(x);
}
return sum;
}
struct Node{
int l,r;
int id;
bool operator<(const Node a)const{
return l>a.l;
}
}num[N];
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
memset(a,,sizeof(a));
memset(c,,sizeof(c));
memset(b,,sizeof(b));
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%d%d",&num[i].l,&num[i].r);
num[i].id=i;
}
sort(num+,num+m+);
int j=n;
for(int i=;i<=m;i++){
for(;j>=num[i].l;j--){
for(int k=;k*k<=a[j];k++){
if(a[j]%k==){
if(b[k]){
add(b[k],k);
}
b[k]=j;
if(k*k!=a[j]){
if(b[a[j]/k]){
add(b[a[j]/k],a[j]/k);
}
b[a[j]/k]=j;
}
}
}
}
ans[num[i].id]=getsum(num[i].r);
}
for(int i=;i<=m;i++)
printf("%d\n",ans[i]);
}
}
No Pain No Game的更多相关文章
- Your pain
Your pain is the breaking of the shell that encloses your understanding. 你的痛苦是你那包裹知识的皮壳的破裂.
- 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 ...
- Pain for friend
For a guy who has experienced his fair share of mysteries,on mystery,I still can't figure out is why ...
- etymon word write alb pain high alt increase large agency ag lose weight fat assist out~3
1● alb 2● write =====>rait 1● alg 2● pain 痛,疼痛 1● alt 2● high 高 1 ...
- 41 Pain and Pain Management 疼痛与疼痛管理
Pain and Pain Management 疼痛与疼痛管理 ①Years ago,doctors often said that pain was a normal part of life.I ...
- 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 (线段树 + 离线处理)
id=45786" style="color:blue; text-decoration:none">HDU - 4630 id=45786" style ...
- E - No Pain No Game 线段树 离线处理 区间排序
E - No Pain No Game HDU - 4630 这个题目很好,以后可以再写写.这个题目就是线段树的离线写法,推荐一个博客:https://blog.csdn.net/u01003321 ...
- 「2014-2-8」Reading a blog on the pain points of Global Variables of C language
晚上读到一篇<C 语言全局变量那些事儿>.我先前对链接的理解不深,算是涨了一番姿势.此文吐槽的重点,是「非 static 限定的全局变量」带来的看似出人意料(实则可以被合理解释)的行为.虽 ...
随机推荐
- (转载)研究openvswitch的流量模式
最近又开始弄openvswitch,网上也有不少的资料,但是发觉都集中在openvswitch安装及简单使用或者一些原码分析,从心里来讲,感觉这些和心里得到的差距有点大啊,其实更希望能类似资料在ope ...
- 通过GitHub部署网站到Azure WebSite
一.将代码上传到GitHub存储库中 第一步:创建Github新账户 第二步:新建仓库 第三步:安装Github shell程序,地址:http://windows.github.com/ 第四步:打 ...
- Android线控的使用
实现方式一:只能在程序为前台时监控 在Activity中即可监听 @Override public boolean onKeyUp(int keyCode, KeyEvent event) { Log ...
- java.lang.RuntimeException: Unable to instantiate activity ComponentInfo异常总结
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo异常总结 做android开发的可能都碰到"j ...
- php 文件缓存
http://www.oschina.net/code/snippet_162279_6098 <?php class cache { private static $_instan ...
- java多线程总结四:volatile、synchronized示例
1.synchronized保证同步 先看一个生成偶数的类 <span style="font-size:16px;">package demo.thread; /** ...
- IOS之swift第一课基础代码
import Foundation //import Foundation 导入模块,专业术语也是导入 包,库的 意思. var str = "Hello World" //声明一 ...
- .net远程连接oracle数据库不用安装oracle客户端
asp.net远程连接oracle数据库不用安装oracle客户端的方法下面是asp.net连接远程Oracle数据库服务器步骤: 1.asp.net连接oracle服务器需要添加Sytem.Data ...
- mysql学习笔记4
用phpmyadmin创建数据库时出现 #1064 - You have an error in your SQL syntax; check the manual that corresponds ...
- MVC——分页控件
不管是什么类型的网站,分页都是必不可少的功能实现.在这里记录一下我自己接触过的分页控件: 一. MvcPager控件(记得项目里添加MvcPager.dll的引用) 这里面比较常用的就 ——@Html ...