题目描述:

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.

思路:

题目的意思是说,给一个数列,看里面有多少个数,这样的数可以被数列中的其他所有数整除。显然这个数就是数列的gcd啦!为什么呢?首先gcd可以满足条件,然后如果不是gcd,那就是gcd的因数,可是数列中的数如果有一个是gcd的因数那它小于等于gcd,而它又不可能比gcd小,只能相等。(为什么,它要是比gcd小,那它才会是gcd)。我怎么会有这么奇怪的想法怀疑它不是gcd (キ`゚Д゚´)!!

又因为是区间查询问题,整一个线段树来维护区间gcd,和等于gcd的数目。注意的是build函数里这么pushup,还有查询函数怎么统计结果。

pushup就是对一个节点求左右两个节点的gcd,如果左边的节点的gcd与这个gcd相等,统计数目加左边的相等数目,如果右边的等就再加右边的数目,不等就是零。

query函数求答案的时候要看一下当前区间答案来自哪里,是左区间,还是右区间,还是两边都有?分别处理一下就好。

这道题竟然连懒标记都没用,就是静态查询√

代码:

 #include <iostream>
#define max_n 100005
using namespace std;
int n;
int t;
struct node
{
int num;
int gcd;
int id;
}tree[max_n<<];
int a[max_n]; int GCD(int a,int b)
{
if(a<b) swap(a,b);
int r = a%b;
if(r==)
{
return b;
}
return GCD(b,r);
}
void build(int id,int l,int r)
{
if(l==r)
{
tree[id].gcd = a[l];
tree[id].num = ;
return;
}
int mid = (l+r)>>;
build(id<<,l,mid);
build(id<<|,mid+,r);
int gcd = GCD(tree[id<<].gcd,tree[id<<|].gcd);
tree[id].num = ;
tree[id].gcd = gcd;
if(tree[id<<].gcd==gcd)
{
tree[id].num += tree[id<<].num;
}
if(tree[id<<|].gcd==gcd)
{
tree[id].num += tree[id<<|].num;
}
}
pair<int,int> query(int id,int L,int R,int l,int r)
{
//cout << "l " << l << " r " << r << endl;
if(L<=l&&r<=R)
{
int gcd = tree[id].gcd;
int num = tree[id].num;
//cout << "gcd " << gcd << "num " << num << endl;
return pair<int,int>(gcd,num);
}
int mid = (l+r)>>;
int ans = ;
pair<int,int> res1,res2;
if(L<=mid){ res1 = query(id<<,L,R,l,mid); }
if(mid<R) {res2 = query(id<<|,L,R,mid+,r);}
int gcd;
if(L<=mid)
{
if(mid<R)
{
gcd = GCD(res1.first,res2.first);
//cout << "gcd " << gcd << endl;
if(res1.first==gcd)
ans+=res1.second;
if(res2.first==gcd)
ans+=res2.second;
}
else
{
gcd = res1.first;
ans += res1.second;
}
}
else
{
gcd = res2.first;
ans += res2.second;
}
//cout << "gcd " << gcd << " ans " << ans << endl;
return pair<int,int>(gcd,ans);
}
int main()
{
//cout << GCD(1,3) << endl;
cin >> n;
for(int i = ;i<=n;i++)
{
cin >> a[i];
}
build(,,n);
cin >> t;
for(int q = ;q<t;q++)
{
int L,R;
cin >> L >> R;
cout << R-L+-query(,L,R,,n).second << endl;;
}
return ;
}

Codeforces G. Ant colony的更多相关文章

  1. CodeForces 474F Ant colony ST+二分

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

  2. Codeforces 474F - Ant colony

    注意到每个区间生存下来的蚂蚁的长度等于区间的gcd 于是可以先预处理出区间的gcd 然后二分查找就好了 预处理gcd我这里用的是倍增法 总的时间复杂度O(NlogN) /* Cf 271F 倍增求区间 ...

  3. Codeforces 474 F. Ant colony

    线段树求某一段的GCD..... F. Ant colony time limit per test 1 second memory limit per test 256 megabytes inpu ...

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

  5. [BZOJ3872][Poi2014]Ant colony

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

  6. 【BZOJ3872】Ant colony(二分,动态规划)

    [BZOJ3872]Ant colony(二分,动态规划) 题面 又是权限题... Description There is an entrance to the ant hill in every ...

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

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

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

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

  9. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

随机推荐

  1. C# .net 提升 asp.net mvc, asp.net core mvc 并发量

    1.提升System.Net.ServicePointManager.DefaultConnectionLimit 2.提升最小工作线程数 ------ DefaultConnectionLimit在 ...

  2. docker添加mongo4.0.3并配置复制集

    1.创建docker 具体略过 自行百度 2.创建数据持久化目录文件(/data/mongo0是个例子 命名随意)  拉取mongo docker pull mongo:4.0.3 3.启动容器 do ...

  3. c#中特性Attribute

    接上篇: 特性介绍: 特性是一个类,需要间接或者直接继承Attribute父类,在标记特性时以中括号包裹,可以标记在元素之前.AttributeTargets.Class设置标记的元素,需要明确指定标 ...

  4. 2019最新版Java程序员零基础入门视频教程资料(全套)

    为了解决Java学习初学者在网上找视频难的事情,本人整理了一份2019年度最新版的Java学习视频教程.希望看到这份视频的你们都能找到一份称心的工作,技术上都能得到进一步的提升,好东西就要分享给你们, ...

  5. Spark学习(3) SparkSQL

    什么事sparkSQL Spark SQL是Spark用来处理结构化数据的一个模块,它提供了一个编程抽象叫做DataFrame并且作为分布式SQL查询引擎的作用, 它是将Spark SQL转换成RDD ...

  6. CORS解决跨域问题(403问题)

    1.什么是跨域问题? 跨域问题是浏览器对于ajax请求的一种安全限制:一个页面发起的ajax请求,只能是用当前页同域名同端口的路径,这能有效的阻止跨站攻击. 2.跨域问题出现的条件: 1.跨域问题是a ...

  7. Opencv颜色识别与追踪

    这是基于颜色识别的物体追踪 不废话 直接看代码 这是Opencv3的代码 //---------------------------------[头文件.命名空间包含部分]-------------- ...

  8. C++ Primer 第五版示例gcc源码

    官方资源,原封不动的.对应于GCC,因此文件名是以此命名的. 门牌号: https://github.com/ZeroPhong/Learning-Resource/blob/master/GCC_4 ...

  9. 记一次奇怪的python多个变量拼接后的字符串丢失事件

    在一次脚本运行中出现了多个变量拼接后的值出现丢失情况. a = "hello " b = "ketty" c = a + b + "!" 预 ...

  10. 使用NPOI进行Excel操作

    一.NPOI组件导入 右键项目菜单,“管理NuGet程序包” 直接搜索“NPOI”即会出现列表,下载第一个进行安装即可 安装完成后项目引用会出现以下几项 二.基础使用 添加引用 using NPOI. ...