[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. cookie、localstroage与sessionstroage的一些优缺点

    1.    Cookie 在前端开发中,尽量少用cooie,原因: (1)   cookie限制大小,约4k左右,不适合存储业务数据,尤其是数据量较大的值: (2)   cookie会每次随http请 ...

  2. 64_p6

    polkit-kde-5.10.1-1.fc26.x86_64.rpm 12-Jun-2017 13:45 84854 polkit-libs-0.113-8.fc26.i686.rpm 13-Apr ...

  3. Petrozavodsk Summer Training Camp 2017 Day 9

    Petrozavodsk Summer Training Camp 2017 Day 9 Problem A. Building 题目描述:给出一棵树,在树上取出一条简单路径,使得该路径的最长上升子序 ...

  4. URAL题解二

    URAL题解二 URAL 1082 题目描述:输出程序的输入数据,使得程序输出"Beutiful Vasilisa" solution 一开始只看程序的核心部分,发现是求快排的比较 ...

  5. 使用正则表达式匹配IP地址

    IP地址分为4段,以点号分隔.要对IP地址进行匹配,首先要对其进行分析,分成如下部分,分别进行匹配:   第一步:地址分析,正则初判 1.0-9 \d 进行匹配 2.10-99 [1-9]\d 进行匹 ...

  6. shell脚本自带变量的含义

    $0 Shell本身的文件名 $1-$n 添加到Shell的各参数值.$1是第1参数.$2是第2参数… $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process ...

  7. sort排序命令常见用法

    sort -n 按数字排序 [root@test88 ~]# cat test.txt 19036 6111 24039 3660 20610 10937 32408 20744 8248 28255 ...

  8. 如何在Cent OS上安装和部署jdk与tomcat?

    Cent OS是一款Linux系统.在商业应用中,Linux操作系统在服务器市场有着广泛的运用,这源于Linux系统的几大优点: 1.跨平台的硬件支持 由于Linux 的内核大部分是用C 语言编写的, ...

  9. PHP laravel 5.0 Blade 模板引擎 Api使用备注

    PHP laravel 5.0 Blade 模板引擎 Api使用备注 /** * PHP laravel 5.0 Blade 模板引擎 Api使用备注 **/ //子模版中开头,调用@extends( ...

  10. java基础12 抽象类(及关键字:abstract)

    抽象类:abstract 1.应用的场景 我们描述一类事物时,存在着某种行为,但这种行为目前不具体,那么我们就可以抽取这种行为的声明,但是不去实现这种行为,我们就需要使用抽象类. 2.抽象的好处 强制 ...