【Codeforces Round 271 (Div 2)F】【贪心 线段树】Ant colony 区间段内是其他所有数因子的数的个数

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 andr (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<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre(){freopen("c://test//input.in","r",stdin);freopen("c://test//output.out","w",stdout);}
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1,class T2>inline void gmax(T1 &a,T2 b){if(b>a)a=b;}
template <class T1,class T2>inline void gmin(T1 &a,T2 b){if(b<a)a=b;}
const int N=0,M=0,Z=1e9+7,ms63=1061109567;
struct A
{
	int l,r;
	int gcd,minv,num;
}a[1<<18];
int n,m;
int l,r;
int GCD,MINV,NUM;
int gcd(int x,int y)
{
	return y==0?x:gcd(y,x%y);
}
void pushup(int o)
{
	a[o].gcd=gcd(a[ls].gcd,a[rs].gcd);
	a[o].minv=min(a[ls].minv,a[rs].minv);
	a[o].num=0;
	if(a[o].minv==a[ls].minv)a[o].num+=a[ls].num;
	if(a[o].minv==a[rs].minv)a[o].num+=a[rs].num;
}
void build(int o,int l,int r)
{
	a[o].l=l;
	a[o].r=r;
	if(a[o].l==a[o].r)
	{
		scanf("%d",&a[o].gcd);
		a[o].minv=a[o].gcd;
		a[o].num=1;
		return;
	}
	int m=(l+r)>>1;
	build(ls,l,m);
	build(rs,m+1,r);
	pushup(o);
}
void get(int o,int l,int r)
{
	if(a[o].l==l&&a[o].r==r)
	{
		if(GCD==-1)GCD=a[o].gcd;
		else GCD=gcd(GCD,a[o].gcd);
		if(a[o].minv<MINV)
		{
			MINV=a[o].minv;
			NUM=a[o].num;
		}
		else if(a[o].minv==MINV)
		{
			NUM+=a[o].num;
		}
		return;
	}
	int m=(a[o].l+a[o].r)>>1;
	if(r<=m)get(ls,l,r);
	else if(l>m)get(rs,l,r);
	else
	{
		get(ls,l,m);
		get(rs,m+1,r);
	}
}
int main()
{
	while(~scanf("%d",&n))
	{
		build(1,1,n);
		scanf("%d",&m);
		for(int i=1;i<=m;++i)
		{
			scanf("%d%d",&l,&r);
			MINV=1e9;NUM=0;GCD=-1;get(1,l,r);
			if(GCD%MINV==0)printf("%d\n",r-l+1-NUM);
			else printf("%d\n",r-l+1);
		}
	}
	return 0;
}
/*
【trick&&吐槽】


【题意】
给你一个长度为n(1e5)的数列。
每个数的数值都在[1,1e9]范围。
然后有m(1e5)个询问。
对于每个询问,问你区间为[l,r]内,有多少个数,是这个区间内其他所有数的因子

【类型】
贪心 线段树

【分析】
呀嘿嘿,这题被我秒掉了。

很显然,有一个贪心结论,如果一个数,是区间内所有数的因子的话。
那么必要条件是,
1,这个数是这区间段最小的数
2,这个数是这区间段内所有数的因子,也就是说,这个数是这区间段所有数gcd的因子。

于是,我们只要求出区间最小数和(同时也记录个数),区间gcd,然后就可以AC这道题啦。

【时间复杂度&&优化】
O(mlogn)

*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值