Codeforces Round #271 (Div. 2) F. Ant colony

F. Ant colony
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Mole is hungry again. He found one ant colony, consisting of n ants, ordered in a row. Each ant i (1 ≤ i ≤ n) has a strength si.

In order to make his dinner more interesting, Mole organizes a version of «Hunger Games» for the ants. He chooses two numbers l and r(1 ≤ l ≤ r ≤ n) and each pair of ants with indices between l and r (inclusively) will fight. When two ants i and j fight, ant i gets one battle point only if si divides sj (also, ant j gets one battle point only if sj divides si).

After all fights have been finished, Mole makes the ranking. An ant i, with vi battle points obtained, is going to be freed only if vi = r - l, or in other words only if it took a point in every fight it participated. After that, Mole eats the rest of the ants. Note that there can be many ants freed or even none.

In order to choose the best sequence, Mole gives you t segments [li, ri] and asks for each of them how many ants is he going to eat if those ants fight.

Input

The first line contains one integer n (1 ≤ n ≤ 105), the size of the ant colony.

The second line contains n integers s1, s2, ..., sn (1 ≤ si ≤ 109), the strengths of the ants.

The third line contains one integer t (1 ≤ t ≤ 105), the number of test cases.

Each of the next t lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), describing one query.

Output

Print to the standard output t lines. The i-th line contains number of ants that Mole eats from the segment [li, ri].

Sample test(s)
input
5
1 3 2 4 2
4
1 5
2 5
3 5
4 5
output
4
4
1
1
Note

In the first test battle points for each ant are v = [4, 0, 2, 0, 2], so ant number 1 is freed. Mole eats the ants 2, 3, 4, 5.

In the second test case battle points are v = [0, 2, 0, 2], so no ant is freed and all of them are eaten by Mole.

In the third test case battle points are v = [2, 0, 2], so ants number 3 and 5 are freed. Mole eats only the ant 4.

In the fourth test case battle points are v = [0, 1], so ant number 5 is freed. Mole eats the ant 4.

思路:我们用L[i]表示a[i],能向左被整除的最远,R[i],向右被整除的最远,

这个直接搞是会超时的,我们用pre[a[i]]表示,前一次出现a[i]出现的位置,如果R[pre[a[i]]] >= i ,就不用求 L[i],R[i] 了

然后用线段树维护区间最小值,最小值的个数,最小值出现的id(记录一个就好)

对于 ql,qr询问,查找 区间最小值的下标id,如果 L[id] <= ql && R[id] >= qr,说明这个最小值可以整除区间所有数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<ctime>
#include<bitset>
#define LL long long
#define INF 89898989
#define mod 1000000007
#define maxn 100010
#define pi acos(-1.0)
#define eps 1e-6
using namespace std;struct node
{int Min,num,id;
}tree[maxn*3] ;
int a[maxn],ql,qr ;
int L[maxn],R[maxn] ;
map<int,int>pre;
void build(int L,int R,int o )
{if(L==R){tree[o].Min = a[L] ;tree[o].num = 1 ;tree[o].id = L ;return ;}int mid=(L+R)>>1;build(L,mid,o<<1) ;build(mid+1,R,o<<1|1);if(tree[o<<1].Min<tree[o<<1|1].Min){tree[o] = tree[o<<1] ;}else if(tree[o<<1].Min > tree[o<<1|1].Min){tree[o] = tree[o<<1|1] ;}else{tree[o].Min = tree[o<<1].Min ;tree[o].num = tree[o<<1].num+tree[o<<1|1].num ;tree[o].id = tree[o<<1].id ;}
}
node find(int L,int R,int o)
{if(ql <= L && qr >=R){return tree[o] ;}int mid=(L+R)>>1 ;if(qr<= mid) return find(L,mid,o<<1) ;if(ql > mid) return find(mid+1,R,o<<1|1) ;node a = find(L,mid,o<<1) ;node b = find(mid+1,R,o<<1|1) ;if(a.Min < b.Min) return a ;else if(a.Min > b.Min) return b ;a.num += b.num ;return a ;
}
int main()
{int i,j,k,n,m;int x,y;while(scanf("%d",&n) != EOF){for( i = 1 ; i <= n ;i++)scanf("%d",&a[i]) ;build(1,n,1) ;pre.clear();for(i = 1 ; i <= n ;i++){if(pre[a[i]]){j = pre[a[i]] ;if(R[j]>=i){L[i] = L[j] ;R[i] = R[j] ;   pre[a[i]] = i ;continue ;}}j = i+1;while(j<=n && a[j]%a[i]==0)j++;R[i] = j-1;j = i-1;while(j>=1&&a[j]%a[i]==0)j--;L[i] = j+1;pre[a[i]] = i ;}node ans;int num ;cin >> m ;for( i = 1 ; i <= m;i++){scanf("%d%d",&ql,&qr) ;ans=find(1,n,1) ;x = L[ans.id] ;y = R[ans.id] ;if(x <= ql && y >= qr){num = qr-ql+1-ans.num;}else num = qr-ql+1;printf("%d\n",num);}}return 0 ;
}

  

转载于:https://www.cnblogs.com/20120125llcai/p/4009084.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:https://dhexx.cn/news/show-17202.html

如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网进行投诉反馈,一经查实,立即删除!


相关文章:

  • 离散化+BFS HDOJ 4444 Walk
  • jenkins持续集成入门5 - Maven项目 编译和打包-方式2
  • SQL Server 内存压力解决方案
  • APP端和PHP端进行数据互通
  • jenkins持续集成入门6 - 后缀名为.WAR的项目(tomcat运行的) 编译和打包
  • vb.net WPF webbrowser window.close 关闭后不触发 WindowClosing 事件 WNDPROC解决方式
  • 优先队列运用 TOJ 4123 Job Scheduling
  • jenkins持续集成入门7 - Pipeline流水线项目 两种语法方式Demo讲解
  • Leetcode: Spiral Matrix
  • Composite Pattern
  • jenkins持续集成入门8 - Pipeline流水线项目 构建maven类型项目案例 从gitlab拉取代码,编译代码
  • ClickOnce部署疑难杂症:更新时部署与应用程序标识不一致问题。要安装此应用程序,请修改此文件的清单版本或卸载之前存在的应用程序。...
  • 你的背景,是这个时代 张璁
  • 《移山之道》Reading Task
  • jenkins持续集成入门9 - Pipeline流水线项目 构建TOMCAT运行的WAR类型项目案例 从gitlab拉取代码,编译代码,发布到TOMCAT
  • CSS学习笔记——简述
  • 基于SpringBoot+Vue的逍遥大药房管理系统设计与实现【源码(完整源码请私聊)+论文+演示视频+包运行成功】
  • NSArray的枚举使用方法
  • 使用PHP获取根域名的方法!
  • jenkins持续集成入门10 - (Pipeline Script from SCM)流水线项目 构建TOMCAT运行的WAR类型项目案例 从gitlab拉取代码,编译代码,发布到TOMCAT
  • 【移动开发】Service类onStartCommand()返回值和参数
  • JAVA学习课第五十三届 — IO流程(七)File打靶 amp; Properties设置
  • 多层陶瓷电容器用处_【连载】手机常见电子元件介绍多层陶瓷电容器
  • jenkins持续集成入门11 - Jenkins项目构建-常用的构建触发器
  • Java对象的强、软、弱和虚引用详解
  • 如何使用conda安装的nvcc_如何安装使用Switch自制主题
  • jenkins持续集成入门12 - 构建项目方式--触发远程构建
  • jenkins持续集成入门13 - 构建项目方式--其他工程构建后触发
  • confluence jira crowd mysql等资源补充-单点登录等
  • php:curl
  • cad2017怎么改变选择方式_得了抑郁怎么办?抑郁症治疗方式的选择
  • Miller_Rabin (米勒-拉宾) 素性测试
  • jenkins持续集成入门14 - 构建项目方式--定时构建
  • 优秀员工的做法-领先的专业、道路管理
  • 数据传输服务包年包月_阿里云云服务器ECS购买流程(附图文)
  • app data权限_iOS开发:Archive、ipa 和 App 包瘦身
  • 新的起点
  • jenkins持续集成入门15 - 构建项目方式--轮询SCM
  • 职业规划
  • jenkins持续集成入门16 - 构建项目方式--Gitlab配置webhook
  • mysql数据库应用与开发姜桂洪 课后答案_MySQL数据库应用与开发习题解答与上机指导...
  • 数据库事务
  • Linux电源管理(1)_整体架构(转自蜗窝科技,www.wowotech.net)
  • jsonarray存list_javax.json:从List Integer构建JSONArray并将其添加到
  • 【IDF实验室】图片里的英语
  • jenkins持续集成入门17 - Jenkins的参数化构建
  • 短信api服务接口
  • jsp输出金字塔_实验二 JSP语法及内置对象.doc
  • harbor仓库安装和基本命令使用
  • 一款简单实用的jQuery图片画廊插件