[CC-ANUGCD]Maximum number, GCD condition
[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的更多相关文章
- [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 ...
- 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.免费应用程序调试最 ...
- [LeetCode] Third Maximum Number 第三大的数
Given a non-empty array of integers, return the third maximum number in this array. If it does not e ...
- [LeetCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- LeetCode 414 Third Maximum Number
Problem: Given a non-empty array of integers, return the third maximum number in this array. If it d ...
- Failed to connect to database. Maximum number of conections to instance exceeded
我们大体都知道ArcSDE的连接数有 48 的限制,很多人也知道这个参数可以修改,并且每种操作系统能支持的最大连接数是不同的. 如果应用报错:超出系统最大连接数 该如何处理? 两种解决办法: 第一,首 ...
- POJ2699 The Maximum Number of Strong Kings
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2102 Accepted: 975 Description A tour ...
- [LintCode] Create Maximum Number 创建最大数
Given two arrays of length m and n with digits 0-9 representing two numbers. Create the maximum numb ...
- 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 说明:最大线 ...
随机推荐
- HDU 1166 敌兵布阵 (树状数组 单点修改+区间查询)
题目链接 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和T ...
- Jsoup爬取带登录验证码的网站
今天学完爬虫之后想的爬一下我们学校的教务系统,可是发现登录的时候有验证码.因此研究了Jsoup爬取带验证码的网站: 大体的思路是:(需要注意的是__VIEWSTATE一直变化,所以我们每个页面都需要重 ...
- weblogic 开启注意问题
1.关闭防火墙 service iptables stop chkconfig iptables off 2.weblogic unable to get file lock问题 我的解决办法是ps ...
- shell 监控磁盘使用率【转】
方案一: disks=(`df |sed 1d | awk '{print $1,$5}'|tr -d %`) len=${#disks[@]} ;i<=$len;i=i+));do ];the ...
- 使用angluar-cli的ng g component home指令出现的错误
Error: ELOOP: too many symbolic links encountered, stat '/Users/zzy/angular/taskmgr/node_modules/@an ...
- str.format() 格式化字符串函数
语法 它通过{}和:来代替%. “映射”示例 通过位置 In [1]: '{0},{1}'.format('kzc',18) Out[1]: 'kzc,18' In [2]: '{},{}'.form ...
- .htaccess教程:简介、访问控制、验证、目录浏览控制
一..htaccess简介 1.什么是.htaccess .htaccess是一个纯文本文件,里面存放着Apache服务器配置相关的一些指令,它类似于Apache的站点配置文件,如httpd.conf ...
- php PDO判断连接是否可用的方法
转载自:傲雪星枫 原文地址: http://blog.csdn.net/fdipzone/article/details/53117541 mysql_ping() 检查到服务器的连接是否正常.如果 ...
- [ python ] 字符串的操作及作业题
字符串的操作方法 capitalize() : 首字母大写 s1 = 'my heart will go on' print(s1.capitalize()) # 首字母大写 # 执行结果: # My ...
- 自定义事件的触发dispatchEvent
1. 对于标准浏览器,其提供了可供元素触发的方法:element.dispatchEvent(). 不过,在使用该方法之前,我们还需要做其他两件事,及创建和初始化.因此,总结说来就是: documen ...