注意到每个区间生存下来的蚂蚁的长度等于区间的gcd

于是可以先预处理出区间的gcd

然后二分查找就好了

预处理gcd我这里用的是倍增法

总的时间复杂度O(NlogN)

/*
Cf 271F
倍增求区间GCD
对下标二分
时间复杂度O(NlogN)
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std; const int MAXN = ; int st[MAXN][];
int n, t; map<int, int> pos;
vector<int> f[MAXN]; inline int gcd (int x, int y) {
return y == ? x : gcd (y, x % y);
} inline void ST() {
for (int i = n - ; i > ; --i)
for (int j = ; i + ( << j) <= n; j++)
st[i][j] = gcd (gcd (st[i][j], st[i][j - ]), st[i + ( << j - )][j - ]);
}
inline int getgcd (int l, int r) {
int tem = st[l][];
for (int k = ; l + ( << k) <= r; k++)
tem = gcd (gcd (tem, st[l][k]), st[r - ( << k) + ][k]);
return tem;
}
int main() {
ios::sync_with_stdio ();
cin >> n;
int tol = ;
for (int i = ; i <= n; i++) {
cin >> st[i][];
if (pos.find (st[i][]) == pos.end() ) tol++, pos[st[i][]] = tol;
f[pos[st[i][]]].push_back (i);
}
ST();
cin >> t;
for (int i = , l, r; i <= t; i++) {
cin >> l >> r;
int key = getgcd (l, r), k = pos[key];
int d = upper_bound (f[k].begin(), f[k].end(), r) - lower_bound (f[k].begin(), f[k].end(), l);
cout << r - l - d +<< endl;
}
}

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

  1. CodeForces 474F Ant colony ST+二分

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

  2. Codeforces G. Ant colony

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

  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. codeforces 704B - Ant Man 贪心

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

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

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

  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. HDU-2188 悼念512汶川大地震遇难同胞——选拔志愿者

    http://acm.hdu.edu.cn/showproblem.php?pid=2188 巴什博奕(Bash Game)的转换:换一种说法而已 悼念512汶川大地震遇难同胞——选拔志愿者 Time ...

  2. PHP的哲学:Simple Is Hard

    原帖地址 PHP框架的繁荣是正确的发展方向吗?   作者 正文 poshboytl    发表时间:2009-01-19   最后修改:2009-04-06   做ROR有一年了, 感觉非常好.配合敏 ...

  3. JavaScript高级程序设计5.pdf

    队列方法访问规则是FIFO(First-In-First-Out,先进先出),数组方法shift()能够移除数组中第一个项并返回该项,同时将数组长度减1,结合使用shift()和push(),可以像队 ...

  4. asp json

    <script language="JScript" runat="Server">function toObject(json) {    eva ...

  5. liststack——链表栈(procedure)

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include "list.h&q ...

  6. bootbox.js [v4.2.0]设置确认框 按钮语言为中文

    Bootbox.js (http://bootboxjs.com/)是一个小型的 JavaScript 库用来创建简单的可编程对话框,基于 Twitter 的 Bootstrap 开发. 弹出确认框方 ...

  7. 读书笔记-《基于Oracle的SQL优化》-第一章-3

    优化器: 1.优化器的模式: 用于决定在Oracle中解析目标SQL时所用优化器的类型,以及决定当使用CBO时计算成本值的侧重点.这里的“侧重点”是指当使用CBO来计算目标SQL各条执行路径的成本值时 ...

  8. 【protobuf进阶】通过.proto文件导出C#支持的.cs类文件

    protobuf是一个跨平台的消息交互协议,类似xml.json等. protocolbuffer(以下简称PB)是google 的一种数据交换的格式,它独立于语言,独立于平台.google 提供了多 ...

  9. HDU 3065 AC自动机 裸题

    中文题题意不再赘述 注意 失配数组 f  初始化一步到位 #include <stdio.h> #include <string.h> #include <queue&g ...

  10. Android如何创建背景透明的Dialog

    一:控制Dialog 的背景方法:1.定义一个无背景主题主题<!--去掉背景Dialog--> <style name="NobackDialog" parent ...