Codeforces 474 F. Ant colony

Codeforces 474 F. Ant colony

大家好,又见面了,我是全栈君。

线段树求某一段的GCD…..

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 2345.

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.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>

using namespace std;

const int maxn=100100;
typedef pair<int,int> pII;

pII b[maxn];
int n,s[maxn];
int g[maxn<<2];

int gcd(int x,int y)
{
	if(x==0) return y;
	return gcd(y%x,x);
}

void build(int l,int r,int rt)
{
	if(l==r)
	{
		g[rt]=s[l];
		return ;
	}
	int mid=(l+r)/2;
	build(l,mid,rt<<1); build(mid+1,r,rt<<1|1);
	g[rt]=gcd(g[rt<<1],g[rt<<1|1]);
}

int query(int L,int R,int l,int r,int rt)
{
	if(r<L||l>R) return 0;
	if(L<=l&&r<=R)
	{
		return g[rt];
	}
	int mid=(l+r)/2;
	int u=query(L,R,l,mid,rt<<1);
	int v=query(L,R,mid+1,r,rt<<1|1);
	return gcd(u,v);
}

int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",s+i);
		b[i]=(make_pair(s[i],i));
	}
	sort(b+1,b+1+n);
	build(1,n,1);
	int m;
	scanf("%d",&m);
	while(m--)
	{
		int Left,Right;
		scanf("%d%d",&Left,&Right);
		int G=query(Left,Right,1,n,1);
		int from=lower_bound(b+1,b+n+1,make_pair(G,Left))-(b+1);
		int to=lower_bound(b+1,b+1+n,make_pair(G,Right+1))-(b+1);
		printf("%d\n",(Right-Left+1)-(to-from));
	}
	return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116374.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 好看又有趣的404页面设计[通俗易懂]

    好看又有趣的404页面设计[通俗易懂]404页面作为提示错误的媒介,起着承上启下的作品,既要保证用户在看到错误页面的同时不会直接退出,又要给用户提供新的操作选择,两者兼顾才会有更好的用户体验。现在更多设计师愿意到可以到乐于分享学习经验的集设网www.ijishe.com,优秀的成功案例,可以开拓设计师的思路,值得在下一个项目中积攒经验!404页面这样设计好看又有趣-集设设计没思路,那么就需要从这几个方面着手:1、了解产品的背景,产品未来发展方向。2、去搜UI

    2022年7月27日
    7
  • JLINK V9项目启动【jlink接口定义】【开启VCOM(虚拟串口)功能】「建议收藏」

    JLINK V9项目启动【jlink接口定义】【开启VCOM(虚拟串口)功能】「建议收藏」jlink接口定义摘录于:https://blog.csdn.net/u014124220/article/details/50829713仿真器端口 连接目标板 备注 1.VCC MCU电源VCC VCC 2.VCC MCU电源VCC VCC 3.TRST TRST Tes…

    2022年5月28日
    257
  • 关于局域网和广域网的叙述中正确的是_城域网是介于广域网和局域网

    关于局域网和广域网的叙述中正确的是_城域网是介于广域网和局域网关于局域网和广域网

    2022年10月18日
    3
  • oracle级plsql安装教程,PLSQL 数据下载库安装使用教程,步骤解析

    oracle级plsql安装教程,PLSQL 数据下载库安装使用教程,步骤解析安装Oracle数据库一、首先安装Oracle数据库*64。二、下载并安装安装PLSQLDeveloper根据计算机的系统位数,从下载的安装包中选择合适的程序,双击“运行”以打开下一个PLSQL软件安装向导。四、根据协议,选中“我接受…”选项,然后单击以下内容。五、选择默认情况下安装在C盘目录中的安装位置,单击“更改”按钮自定义软件安装路径,确认后单击“下一步”。选择安装方式,标准(程序设置…

    2022年6月22日
    32
  • IdeaVim-常用操作「建议收藏」

    IdeaVim-常用操作「建议收藏」IdeaVim简介IdeaVim是IntelliJIDEA的一款插件,他提高了我们写代码的速度,对代码的跳转,查找也很友好。安装位置安装之后它在Tools>VimEmulator具体操作i模式i模式即为编辑模式,按下字母i开启就可以打字。Esc从i模式切换为Vim,按下键盘的Esc键切回Vim。方向键上:k,下:j,左:h,右:…

    2022年5月5日
    196
  • 苹果怎么设置侧边滑返回键_苹果X的按键怎么拆

    苹果怎么设置侧边滑返回键_苹果X的按键怎么拆iPhoneX上市也已经有一段时间了,众所周知它去除了我们熟悉的睡眠/唤醒按钮——home键。很多用户肯定对觉得这个设计很反人类,其实现实是恰恰相反,苹果只是简单地把home键的功能分配到了Side按钮,除了消失的指纹识别其他功能一应俱全。现在就让小编带大家看看iPhoneX侧边按钮有多牛X。1、睡眠/唤醒用惯了iPhone的人总喜欢按圆圆的home键唤醒手机,不过现在你仍然可以使用侧面按…

    2022年8月10日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号