[CC-ANUGCD]Maximum number, GCD condition

题目大意:

一个长度为\(n(n\le10^5)\)的数列\(A(A_i\le10^5)\),\(m(m\le10^5)\)次询问,每次询问\(l\sim r\)中不与\(g\)互质的数中的最大数以及最大数的个数。

思路:

对于每个质数维护一棵线段树,记录区间内包含这个质数的数的和。询问时将\(g\)分解质因数,在线段树上寻找最大值。

统计个数时将所有数以数值为第一关键字、下标为第二关键字排序后二分即可。

源代码:

#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=1e5+1,P=9593;
int p[P];
bool vis[N];
std::pair<int,int> v[N];
inline void sieve() {
for(register int i=2;i<N;i++) {
if(!vis[i]) p[++p[0]]=i;
for(register int j=1;j<=p[0]&&i*p[j]<N;j++) {
vis[i*p[j]]=true;
if(i%p[j]==0) break;
}
}
}
class SegmentTree {
#define mid ((b+e)>>1)
private:
struct Node {
int val;
Node *left,*right;
Node() {
val=-1;
left=right=NULL;
}
};
public:
Node *root;
void modify(Node* &p,const int &b,const int &e,const int &x,const int &y) {
if(!p) p=new Node();
p->val=std::max(p->val,y);
if(b==e) return;
if(x<=mid) modify(p->left,b,mid,x,y);
if(x>mid) modify(p->right,mid+1,e,x,y);
}
int query(const Node* const &p,const int &b,const int &e,const int &l,const int &r) const {
if(!p) return -1;
if(b==l&&e==r) return p->val;
int ret=-1;
if(l<=mid) ret=std::max(ret,query(p->left,b,mid,l,std::min(mid,r)));
if(r>mid) ret=std::max(ret,query(p->right,mid+1,e,std::max(mid+1,l),r));
return ret;
}
#undef mid
};
SegmentTree t[P];
int main() {
sieve();
const int n=getint(),m=getint();
for(register int i=1,b;i<=n;i++) {
v[i].first=b=getint();
v[i].second=i;
for(register int j=1;p[j]*p[j]<=b;j++) {
if(b%p[j]!=0) continue;
while(b%p[j]==0) b/=p[j];
t[j].modify(t[j].root,1,n,i,v[i].first);
}
if(b!=1) {
const int j=std::lower_bound(&p[1],&p[p[0]]+1,b)-p;
t[j].modify(t[j].root,1,n,i,v[i].first);
}
}
std::sort(&v[1],&v[n]+1);
for(register int i=0;i<m;i++) {
int g=getint();
const int l=getint(),r=getint();
int max=-1;
for(register int i=1;p[i]*p[i]<=g;i++) {
if(g%p[i]!=0) continue;
while(g%p[i]==0) g/=p[i];
max=std::max(max,t[i].query(t[i].root,1,n,l,r));
}
if(g!=1) {
const int &i=std::lower_bound(&p[1],&p[p[0]]+1,g)-p;
max=std::max(max,t[i].query(t[i].root,1,n,l,r));
}
const int cnt=std::upper_bound(&v[1],&v[n]+1,std::make_pair(max,r))-std::lower_bound(&v[1],&v[n]+1,std::make_pair(max,l));
printf("%d %d\n",max,cnt?:-1);
}
return 0;
}

[CC-ANUGCD]Maximum number, GCD condition的更多相关文章

  1. [Coding Practice] Maximum number of zeros in NxN matrix

    Question: Input is a NxN matrix which contains only 0′s and 1′s. The condition is no 1 will occur in ...

  2. iOS---The maximum number of apps for free development profiles has been reached.

    真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...

  3. [LeetCode] Third Maximum Number 第三大的数

    Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...

  4. [LeetCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  5. LeetCode 414 Third Maximum Number

    Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...

  6. Failed to connect to database. Maximum number of conections to instance exceeded

    我们大体都知道ArcSDE的连接数有 48 的限制,很多人也知道这个参数可以修改,并且每种操作系统能支持的最大连接数是不同的. 如果应用报错:超出系统最大连接数 该如何处理? 两种解决办法: 第一,首 ...

  7. POJ2699 The Maximum Number of Strong Kings

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2102   Accepted: 975 Description A tour ...

  8. [LintCode] Create Maximum Number 创建最大数

    Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...

  9. tomcat 大并发报错 Maximum number of threads (200) created for connector with address null and port 8080

    1.INFO: Maximum number of threads (200) created for connector with address null and port 8091 说明:最大线 ...

随机推荐

  1. linux 在命令行中通过conda使用anaconda

    在 ~/.bash_profile中添加 export PATH="/home/taoke/anaconda/bin:$PATH"

  2. python中随机数生成

    1.random.random random.random()用于生成一个0到1的随机符小数: 0 <= n < 1.0 2.random.uniform random.uniform的函 ...

  3. 数据库与sql注入的相关知识

    数据库与sql注入的相关知识 sql语句明显是针对数据库的一种操作,既然想通过sql注入的方法来拿取数据那么就要先了解一下如何的去操作数据库,这方面并不需要对数据库有多么的精通但是如果了解掌握了其中的 ...

  4. 用ELK搭建简单的日志收集分析系统【转】

    缘起 在微服务开发过程中,一般都会利用多台服务器做分布式部署,如何能够把分散在各个服务器中的日志归集起来做分析处理,是一个微服务服务需要考虑的一个因素. 搭建一个日志系统 搭建一个日志系统需要考虑一下 ...

  5. falsk注册etcd

    部署web服务集群时,我们希望能动态调整集群大小.当一个新的节点启动时,可以将自己的信息注册给master, 让master把它加入到集群里, 关闭之后也可以把自己从集群中删除.我这里使用的是flas ...

  6. LFM隐语义模型Latent Factor Model

    实际应用 LFM 模型在实际使用中有一个困难,就是很难实现实时推荐.经典的 LFM 模型每次训练都需要扫描所有的用户行为记录,并且需要在用户行为记录上反复迭代来优化参数,所以每次训练都很耗时,实际应用 ...

  7. Spring mvc知识点总结——面试篇

    一.MVC思想MVC(Model-View-Controller)三元组的概念:1.Model(模型):数据模型,提供要展示的数据,因此包含数据和行为,可以认为是领域模型或JavaBean组件(包含数 ...

  8. EF – 4.CRUD与事务

    5.6.1 <Entity Framework数据更新概述>  首先介绍Entity Framework实现CRUD的基本方法,接着介绍了如何使用分部类增强和调整数据实体类的功能与行为特性 ...

  9. 将dubbo框架里的服务提供者迁移进k8s里,以docker提供服务时要注意的细节

    在将dubbo框架里的服务提供者迁移进k8s时,有几个注意事项: 1, 要为默认情况下,dubbo会将k8s里的pod ip地址作为注册服务的地址,所以这个地址需要被改写(ip测试过,行得通,而dns ...

  10. Netty性能调优

    1. 减少内存allocation和deallocation.通过静态实例和内存缓存,减少IO的次数. 2. 使用gather write和scatter read 3. 使用jDK7,因为他的byt ...