Codeforces 474 F. Ant colony
线段树求某一段的GCD.....
1 second
256 megabytes
standard input
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.
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.
Print to the standard output t lines. The i-th line
contains number of ants that Mole eats from the segment [li, ri].
5
1 3 2 4 2
4
1 5
2 5
3 5
4 5
4
4
1
1
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的更多相关文章
- [CF] 474 F. Ant colony
区间重复不会影响GCD,ST表当然是支持的啦,常数这么小. 学到了三个东西: 1.lower_bound返回的是大于等于的位置,要判是否不存在(end())和是否超出所求[x,y]范围. 2.ST表更 ...
- 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 ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- CF #271 F Ant colony 树
题目链接:http://codeforces.com/contest/474/problem/F 一个数组,每一次询问一个区间中有多少个数字可以整除其他所有区间内的数字. 能够整除其他所有数字的数一定 ...
- Codeforces G. Ant colony
题目描述: F. Ant colonytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputo ...
- CodeForces 474F Ant colony ST+二分
Ant colony 题解: 因为一个数是合法数,那么询问区间内的其他数都要是这个数的倍数,也就是这个区间内的gcd刚好是这个数. 对于这个区间的gcd来说,不能通过前后缀来算. 所以通过ST表来询问 ...
- [BZOJ3872][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...
- bzoj 3872: [Poi2014]Ant colony -- 树形dp+二分
3872: [Poi2014]Ant colony Time Limit: 30 Sec Memory Limit: 128 MB Description There is an entranc ...
- 【BZOJ3872】[Poi2014]Ant colony 树形DP+二分
[BZOJ3872][Poi2014]Ant colony Description 给定一棵有n个节点的树.在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁.这些蚂蚁会相继进入树中, ...
随机推荐
- opencv视频播放
在一个界面上显示一张图片.是一件非常easy的事情,但说到要显示视频.刚開始学习的人可能不知道怎么处理,事实上,一般来说能够理解为视频就是图片以人眼察觉不到的速度高速更新. 曾经用摄像头採集视频显示在 ...
- traceroute工作原理
traceroute, 也就是 trace route,跟踪路由.这个程序最早是Van Jacobson实现的.源代码在网上能够找到,只是我还没有去找.基本的原理是IP路由过程中对数据包TTL(T ...
- iOS开发 - 应用内打开第三方应用并传值
首先说下这个功能, 应该都有接触过. 比方,你下载了一个电子书,然后选择打开方式的时候,可能会看到你手机中已经安装的阅读类App. 或者,你的QQ收到了某个文件,你也能够选择本地的应用来打开. 那这种 ...
- FxMaker用法
第一步:选sceneFxMaker幕后,执行 第二步:执行界面,选中EffectParticle制作粒子特效 第三步:随便点中一个粒子特效.例如以下所看到的 第四步:点中右側的"Explos ...
- [置顶] 浅谈Android的资源编译过程
Android APK 一.APK的结构以及生成 APK是Android Package的缩写,即Android application package文件或Android安装包.每个要安装到Andr ...
- 【linux】内核+文件系统下载到开发板
K开发 欢迎转载,转载时请保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:htt ...
- axure网格设置
Axure默认的界面是没有吧网格显示出来,没有网格在制作原型的时候,对齐方面不是很好,个人习惯还是把网格显示出来,便于组件对齐和布局. 其实本来这篇文章应该叫做网格与参考线,只是本人对参考线的应用还很 ...
- high volume logging
logging 是现在系统中必不可少的组件了.市面上已经有很多很多非常成熟的日志产品,log*系列就是一个典型代表.对于erlang系统来说,也有很多,比如error_logger, disk_log ...
- 性能测试之LoardRunner工作原理
概述: 1.VuGen 2.控制器 3.负载发生器 4.分析器 VuGen,它的作用是捕捉用户的业务流,并最终将其录制成一个脚本.在录制脚本前首先选择一种协议,接着在客户端模拟客户实际使用过程中的业务 ...
- webdynpro 组件重用 传值问题
组件zwd1,需要调用组件zwd2的时候,zwd2组件控制器中需要定义一个方法,定义所要传输的参数,并且该方法需要定义为interface方法. 组件zwd1可以通过代码向导调用组件zwd2,的该方法 ...