[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 内核信号量

    Linux内核的信号量在概念和原理上和用户态的System V的IPC机制信号量是相同的,不过他绝不可能在内核之外使用,因此他和System V的IPC机制信号量毫不相干. 信号量在创建时需要设置一个 ...

  2. [MySQL] AUTO_INCREMENT lock Handing in InnoDB

    MySQL AUTO_INCREMENT lock Handing in InnoDB 在MySQL的表设计中很普遍的使用自增长字段作为表主键, 实际生产中我们也是这样约束业务开发同学的, 其中的优势 ...

  3. 正排索引(forward index)与倒排索引(inverted index) (转)

    一.正排索引(前向索引) 正排索引也称为"前向索引".它是创建倒排索引的基础,具有以下字段. (1)LocalId字段(表中简称"Lid"):表示一个文档的局部 ...

  4. CMS(Concurrent Mark-Sweep)垃圾回收器

    http://www.iteye.com/topic/1119491 1.总体介绍: CMS(Concurrent Mark-Sweep)是以牺牲吞吐量为代价来获得最短回收停顿时间的垃圾回收器.对于要 ...

  5. Dockerfile 备份

    dotnet core app FROM microsoft/dotnet:1.1.0-runtime WORKDIR /mvcApp COPY ./out . ENTRYPOINT ["d ...

  6. [实战]MVC5+EF6+MySql企业网盘实战(24)——视频列表

    写在前面 上篇文章实现了文档列表,所以实现视频列表就依葫芦画瓢就行了. 系列文章 [EF]vs15+ef6+mysql code first方式 [实战]MVC5+EF6+MySql企业网盘实战(1) ...

  7. Node.js 部署到 CentOs

    配置服务器环境 3.把yum更新到最新版本: yum -y update 4.我们将使用最新源代码构建Node.js,要进行软件的安装,需要一组用来编译源代码的开发工具: yum -y groupin ...

  8. phantomjs2.1 初体验

    上次看了一下scrapy1.1的新手指南 决定写个小爬虫实验一下 目标网站是http://www.dm5.com/manhua-huofengliaoyuan准备爬取漫画火凤燎原的已有章节,将图片保存 ...

  9. 在Linux中设置UMASK值

    umask值用于设置用户在创建文件时的默认权限,当我们在系统中创建目录或文件时,目录或文件所具有的默认权限就是由umask值决定的. 对于root用户,系统默认的umask值是0022:对于普通用户, ...

  10. (16) go 面向对象

    一.封装 二.继承 1. 2. 3. 4. 5 6. 7.多重继承 三.接口