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 限定的全局变量」带来的看似出人意料(实则可以被合理解释)的行为.虽 ...
随机推荐
- 【MINA】用protobuf做编解码协议
SOCKET协议 支持java serial 与 AMF3的混合协议,目前没有基于xml 与 json的实现. 协议说明: * 9个字节协议头+协议体. * * 协议头1-4字节表示协议长度 =协议体 ...
- webform处理过程
一.post/get传值注意几点 post提交的时候,只有写了name属性且没有写disable=true表单元素(input,select,textarea)才会被提交. 如果不确定是get还是po ...
- C#学习笔记6:各种字符串问题
1.字符串 逐字字符串字面量:字符串前加@,不仅将反斜杠当做普通字符来处理,而且还会逐字解释所有空白字符. 如: Class Triangle { Static void Main() { Syste ...
- Spring切面处理
切面类DictionaryAOP 里面有一个全局变量systemTestDomain <bean id="dictionaryAOP" class="test.Di ...
- struts2中<s:property>的用法
1,访问Action值栈中的普通属性: <s:property value="attrName"/> 2,访问Action值栈中的对象属性(要有get set方法): ...
- jquery load
$('#loadFooter').click(function() { $('#footer').load('footer.html'); });
- 关于Masonry框架(AutoLayout)的用法--面向初学者
Masonry作为目前较为流行的自动布局第三方框架,简单易用,大大减少了程序员花在UI布局和屏幕适配的精力与时间. 1 基本用法 1.1 事例1: 图1-1 // 首先是view1自动布局 [view ...
- java新手笔记25 日期格式化
1.系统时间 package com.yfs.javase; import java.sql.Time; import java.sql.Timestamp; import java.util.Cal ...
- Java实战之02Hibernate-06处理并发
十三.处理并发 1.事务的隔离级别 不考虑隔离级别出现的问题: 脏读:一个线程中的事务读到了另外一个线程中未提交的数据. 不可重复读:一个线程中的事务读到了另外一个线程中提交的update(更新)的数 ...
- Head First 设计模式系列之一----模板模式(java版)
开篇序言:四人帮的设计模式对于我这个菜鸟看着打瞌睡,后面果断买了一本head first的,感觉还可以像看报纸似的,花了一个寒假的晚上看了大半,确实内容也挺吸引人的,讲的很风趣.否则我也不可能,大过年 ...