注意到每个区间生存下来的蚂蚁的长度等于区间的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. java生成随机整数

    1. 使用Random类的nextInt方法: Random rand = new Random(); rand.nextInt(max);, 此时输出[0,max),注意右边是开区间,如果需要设定最 ...

  2. MyEclipse中导入Spring 4.0源码

    http://www.cnblogs.com/shi-blog/p/4132183.html

  3. Windows作业

    1.什么是Windows作业 Windows作业实际上一个进程组,可以给作业设置权限,一旦进程加入到作业内,进程的权限将会被作业限制. 2.创建一个作业 HANDLE CreateJobObject( ...

  4. main函数的3个参数

    最早是在MCU中使用C语言,MCU中的main函数没有参数. 后来在PC上面使用C语言,一般教程都写的是main函数有2个参数: int main(int argc, const char **arg ...

  5. C#开发学习——ADO.NET几个重要对象

    ADO.NET包括多个对象模型,有Connection/Command/DataReader/DataAdapter/DataSet/DataTable/DataView等. 命名空间System.D ...

  6. Maven 打胖jar

    自己去看原版:http://www.mkyong.com/maven/create-a-fat-jar-file-maven-assembly-plugin/ 一个Eclipse的工程,在pom中配置 ...

  7. Boost.Asio c++ 网络编程翻译(20)

    异步服务端 这个图表是相当复杂的:从Boost.Asio出来你能够看到4个箭头指向on_accept.on_read,on_write和on_check_ping. 着也就意味着你永远不知道哪个异步调 ...

  8. ※C++随笔※=>☆C++基础☆=>※№→C++中 #include<>与#include""

    #include<> 使用尖括号表示在包含文件目录中去查找(包含目录是由用户在设置环境时设置的),而不在源文件目录去查找: #include"" 使用双引号则表示首先在 ...

  9. [Redux] Persisting the State to the Local Storage

    We will learn how to use store.subscribe() to efficiently persist some of the app’s state to localSt ...

  10. 吴柄锡 github----MHA helper

    https://github.com/wubx http://www.cnblogs.com/kissdb/p/4009620.html