codeforces474F. 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].

Examples

input

Copy

5
1 3 2 4 2
4
1 5
2 5
3 5
4 5

output

Copy

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.

 

一、原题地址

点我传送

 

二、大致题意

一个由蚂蚁组成的蚁群,连续排成一排。每个蚂蚁i(1≤i≤n)具有强度si。

为了让他的晚餐更有趣,Mole为蚂蚁组织了一个版本的“饥饿游戏”。他选择了两个数字l和r(1≤l≤r≤n),每对指数在l和r之间(包含)的蚂蚁将会战斗。当两只蚂蚁i和j战斗时,只有当si除以sj时,才得到一个战斗点(同样,只有当sj除以si时,蚂蚁j才会获得一个战斗点)。

在所有战斗结束后,Mole排名。获得vi战斗点的蚂蚁i只有在vi = r-l时才会被释放,换句话说只有在它参与的每场战斗中都占据了一个点时才会被释放。在那之后,鼹鼠吃掉了其余的蚂蚁。请注意,可以释放许多蚂蚁,甚至没有蚂蚁。

以上来自百度翻译。非常详细了。

 

三、大致思路

首先用线段树维护区间的gcd。每次询问区间内不会被吃掉的蚂蚁是那些在这个区间内gcd等于本身的蚂蚁,所以每次询问一个区间时,我们先求出区间的gcd记为Ansgcd,然后只需要知道这个区间内有多少只蚂蚁的能力等于Ansgcd就可以了,这里用可持久化划分树维护,得到了数量记为haveNum,那么答案就是区间长度减去haveNum。

 

四、丑陋代码

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int inf = 0x3f3f3f3f;
#define LL long long int
long long  gcd(long long  a, long long  b) { return a == 0 ? b : gcd(b % a, a); }



const int maxn = 100005 * 4;	//线段树范围要开4倍

struct Tree
{
	int l, r;
	LL sum;
};
Tree node[maxn];		//node[maxn]为线段树处理数组
LL a[maxn];			//a[maxn]为原数组
void PushUp(int i)
{
	node[i].sum = gcd( node[i << 1].sum , node[(i << 1) | 1].sum );
}

void build(int i, int l, int r)
{
	node[i].l = l;
    node[i].r = r;
	if (l == r)
	{
		node[i].sum = a[l];
		return;
	}
	int mid = (l + r) / 2;
	build(i << 1, l, mid);
	build((i << 1) | 1, mid + 1, r);
	PushUp (i);
}

LL getGcd(int i, int l, int r)
{
	if (node[i].l == l&&node[i].r == r)
		return node[i].sum;
	int mid = (node[i].l + node[i].r) / 2;
	if (r <= mid) return getGcd(i << 1, l, r);
	else if (l > mid) return getGcd((i << 1) | 1, l, r);
	else return gcd( getGcd(i << 1, l, mid) , getGcd((i << 1) | 1, mid + 1, r) );
}

int n;

struct Node
{
    int rs,ls,sum;
}tr[3300010];
int rt[100010],cnt;

void Insert(int pre,int &rt,LL l,LL r,LL num)
{
    rt=++cnt;
    tr[rt].ls=tr[pre].ls;
    tr[rt].rs=tr[pre].rs;
    tr[rt].sum=tr[pre].sum;
    if(l==r&&l==num)
    {
        tr[rt].sum++;return;
    }
    LL mid=(l+r)>>1;
    if(mid>=num)Insert(tr[pre].ls,tr[rt].ls,l,mid,num);
    else Insert(tr[pre].rs,tr[rt].rs,mid+1,r,num);
}

int Query(int rt,LL l,LL r,LL num)
{
    if(l==r&&l==num)
    {
        return tr[rt].sum;
    }
    LL mid=(l+r)>>1;
    if(mid>=num)return Query(tr[rt].ls,l,mid,num);
    else return Query(tr[rt].rs,mid+1,r,num);
}

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    build(1,1,n);

    for(int i=1;i<=n;i++)
    {
        Insert(rt[i-1],rt[i],1,1000000000,a[i]);
    }
    int Q;
    scanf("%d",&Q);
    for(int i=1;i<=Q;i++)
    {
        int ql,qr;
        scanf("%d %d",&ql,&qr);
        LL ansGcd=getGcd(1,ql,qr);
        LL haveNum=Query(rt[qr],1,1000000000,ansGcd)-Query(rt[ql-1],1,1000000000,ansGcd);
        printf("%d\n",qr-ql+1-haveNum);
    }
}

 

你可能感兴趣的:(codeforces474F. Ant colony(线段树+可持久化划分树))