codeforces CF474F Ant colony 线段树 区间gcd

$ \Rightarrow $ 戳我进CF原题

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 $ s_i $ .
 
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 $ s_i $ divides $ s_j $
(also, ant j gets one battle point only if $ s_j $ divides $ s_i $ ).
 
After all fights have been finished, Mole makes the ranking.
An ant $ i $ , with $ v_i $ battle points obtained, is going to be freed only if $ v_i = 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 $ [l_i, r_i] $ 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 ≤ 10^5) $ , the size of the ant colony.
The second line contains n integers $ s_1, s_2, ..., s_n (1 ≤ s_i ≤ 10^9) $ , the strengths of the ants.
The third line contains one integer $ t (1 ≤ t ≤ 10^5) $ , the number of test cases.
Each of the next $ t $ lines contains two integers $ l_i $ and $ r_i (1 ≤ l_i ≤ r_i ≤ 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 $ [l_i, r_i] $ .
 

Examples

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

题目大意

  • 求区间 $ gcd $ 以及这个区间内的数字等于区间 $ gcd $ 的有多少个。

  • 数据范围:$ 1 \le n \le 10^5 $
     

思路

  • 线段树(倍增)求 $ gcd $
  • 对于每个数字,记录出现的位置,二分即可。
     

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 100010
#define int long long
struct tree{ int l,r,gcd,sum; }t[maxn<<2];
int n,m,k,l,r;
int gcd(int x,int y){ return !y?x:gcd(y,x%y); }
void pushup(int o){
	t[o].gcd=gcd(t[o<<1].gcd,t[o<<1|1].gcd);
	t[o].sum=0;
	if(t[o].gcd==t[o<<1].gcd) t[o].sum+=t[o<<1].sum;
	if(t[o].gcd==t[o<<1|1].gcd) t[o].sum+=t[o<<1|1].sum;
}
void build(int o,int l,int r){
	t[o].l=l; t[o].r=r;
	if(l==r){
		scanf("%lld",&t[o].gcd); t[o].sum=1;
		return;
	}
	int mid=l+r>>1;
	build(o<<1,l,mid); build(o<<1|1,mid+1,r);
	pushup(o);
}
int getgcd(int o,int l,int r){
	if(t[o].l==l&&t[o].r==r) return t[o].gcd;
	int mid=t[o].l+t[o].r>>1;
	if(r<=mid) return getgcd(o<<1,l,r);
	else if(l>mid) return getgcd(o<<1|1,l,r);
	else return gcd(getgcd(o<<1,l,mid),getgcd(o<<1|1,mid+1,r));
}
int query(int o,int l,int r){
	if(t[o].l==l&&t[o].r==r) return k==t[o].gcd?t[o].sum:0;
	int mid=t[o].l+t[o].r>>1;
	if(l>mid) return query(o<<1|1,l,r);
	else if(r<=mid) return query(o<<1,l,r);
	else return query(o<<1,l,mid)+query(o<<1|1,mid+1,r);
}
signed main(){
	scanf("%lld",&n);
	build(1,1,n);
	scanf("%lld",&m);
	while(m--){
		scanf("%lld %lld",&l,&r);
		k=getgcd(1,l,r);
		printf("%lld\n",r-l+1-query(1,l,r));
	}
	return 0;
}
/*
#        40224145
When     2018-07-12 11:03:12 
Who      PotremZ 
Problem  F - Ant colony
Lang     GNU C++ 
Verdict  Accepted
Time     234 ms
Memory   12500 KB
*/
posted @ 2018-09-19 19:48  potrem  阅读(293)  评论(0编辑  收藏  举报