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<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
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);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
旅游社交小程序功能有管理员和用户。管理员有个人中心,用户管理,每日签到管理,景点推荐管理,景点分类管理,防疫查询管理,美食推荐管理,酒店推荐管理,周边推荐管理,分享圈管理,我的收藏管理,系统管理。用户可以在微信小程序上注册登录,进行每日签到,防疫查询,可以在分享圈里面进行分享自己想要分享的内容,查看和收藏景点以及美食的推荐等操作。因而具有一定的实用性。 本站后台采用Java的SSM框架进行后台管理开发,可以在浏览器上登录进行后台数据方面的管理,MySQL作为本地数据库,微信小程序用到了微信开发者工具,充分保证系统的稳定性。系统具有界面清晰、操作简单,功能齐全的特点,使得旅游社交小程序管理工作系统、规范。 管理员可以管理用户信息,可以对用户信息添加修改删除。管理员可以对景点推荐信息进行添加修改删除操作。管理员可以对分享圈信息进行添加,修改,删除操作。管理员可以对美食推荐信息进行添加,修改,删除操作。管理员可以对酒店推荐信息进行添加,修改,删除操作。管理员可以对周边推荐信息进行添加,修改,删除操作。 小程序用户是需要注册才可以进行登录的,登录后在首页可以查看相关信息,并且下面导航可以点击到其他功能模块。在小程序里点击我的,会出现关于我的界面,在这里可以修改个人信息,以及可以点击其他功能模块。用户想要把一些信息分享到分享圈的时候,可以点击新增,然后输入自己想要分享的信息就可以进行分享圈的操作。用户可以在景点推荐里面进行收藏和评论等操作。用户可以在美食推荐模块搜索和查看美食推荐的相关信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值