线段树求某一段的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 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.

#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;
}

Codeforces 474 F. Ant colony的更多相关文章

  1. [CF] 474 F. Ant colony

    区间重复不会影响GCD,ST表当然是支持的啦,常数这么小. 学到了三个东西: 1.lower_bound返回的是大于等于的位置,要判是否不存在(end())和是否超出所求[x,y]范围. 2.ST表更 ...

  2. Codeforces Round #271 (Div. 2) F. Ant colony 线段树

    F. Ant colony time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  3. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

  4. CF #271 F Ant colony 树

    题目链接:http://codeforces.com/contest/474/problem/F 一个数组,每一次询问一个区间中有多少个数字可以整除其他所有区间内的数字. 能够整除其他所有数字的数一定 ...

  5. Codeforces G. Ant colony

    题目描述: F. Ant colonytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputo ...

  6. CodeForces 474F Ant colony ST+二分

    Ant colony 题解: 因为一个数是合法数,那么询问区间内的其他数都要是这个数的倍数,也就是这个区间内的gcd刚好是这个数. 对于这个区间的gcd来说,不能通过前后缀来算. 所以通过ST表来询问 ...

  7. [BZOJ3872][Poi2014]Ant colony

    [BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...

  8. bzoj 3872: [Poi2014]Ant colony -- 树形dp+二分

    3872: [Poi2014]Ant colony Time Limit: 30 Sec  Memory Limit: 128 MB Description   There is an entranc ...

  9. 【BZOJ3872】[Poi2014]Ant colony 树形DP+二分

    [BZOJ3872][Poi2014]Ant colony Description 给定一棵有n个节点的树.在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁.这些蚂蚁会相继进入树中, ...

随机推荐

  1. CloudStack全局配置參数

    參数 描写叙述 类型 默认值 account.cleanup.interval 清除用户账户所须要等待的时间(秒) 整数 86400 agent.lb.enabled If agent load ba ...

  2. JAVA编程相关:eclipse如何导入已有工程

    eclipse使用过程中,经常会遇到导入外部eclispe工程的情况,导入外部eclipse也就是将已有的eclipse工程导入到eclipse中,那么如何导入外部工程呢?下面为大家分享导入已有ecl ...

  3. 在Qt中如何使用QtDesigner创建的UI文件

    使用Qt有一些时间了,一直在IDE环境(qtcreator和VS2003+集成器)中使用,自然少了很多麻烦的步骤.但是在享受这种便利的同 时,我们也失去了理解更多知识背后的点滴.在IDE中,如果我们要 ...

  4. 遍历关联数组 index by varchar2

    --字符串序列要这样 declare     type t   is table of number(3) index by varchar2(3);    hash_t t;      l_row ...

  5. Linux设备驱动中的ioctl

    memdev.h #ifndef _MEMDEV_H #define _MEMDEV_H #define MEM_MAGIC 'm' #define MEM_RESTART _IO(MEM_MAGIC ...

  6. 配置rhel 6.4(64位)安装使用syslog-ng 3.5

    我基本的博客地址是:www.cppblog.com/zdhsoft 相应的CentOS 6.x也就可能使用. 下载地址: 第一步:安装 wget http://www.balabit.com/down ...

  7. RHEL Server 6.3下MySQL5.5.25a源码安装

    OS:RHEL Server 6.3 MySQL:mysql-5.5.25a.tar.gz 相关依赖包: ncurses-5.9.tar.gz bison-2.5.tar.gz 安装MySQL 一.安 ...

  8. CCNP交换实验(7) -- NAT

    1.静态NAT2.动态NAT3.复用内部全局地址的NAT(PAT) enableconf tno ip do loenable pass ciscoline con 0logg syncexec-t ...

  9. cocos2d-x在win32和iOS、android下获取当前系统时间的方法

    最近在游戏里要显示当前系统时间的功能,网上一搜很多写着获取的方法,大都是如下 struct cc_timeval now; CCTime::gettimeofdayCocos2d(&now, ...

  10. 某公司ASP.NET应聘上机试题

    ASP.NET笔试题是ASP.NET程序员面试必须经历的,一般会叫你填两个表 1个是你的详细信息表 1个是面试题答卷 两个都要注意反正面是否都有内容不要遗漏,如果考你机试一般也有两种,就是程序连接数据 ...