Codeforces 474F - Ant colony

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

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

然后二分查找就好了

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

总的时间复杂度O(NlogN)

/*
       Cf 271F
       倍增求区间GCD
       对下标二分
       时间复杂度O(NlogN)
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
using namespace std;

const int MAXN = 100009;

int st[MAXN][20];
int n, t;

map<int, int> pos;
vector<int> f[MAXN];

inline int  gcd (int x, int y) {
    return y == 0 ? x : gcd (y, x % y);
}

inline void ST() {
    for (int i = n - 1; i > 0; --i)
        for (int j = 1; i + (1 << j) <= n; j++)
            st[i][j] = gcd (gcd (st[i][j], st[i][j - 1]), st[i + (1 << j - 1)][j - 1]);
}
inline int getgcd (int l, int r) {
    int tem = st[l][0];
    for (int k = 0; l + (1 << k) <= r; k++)
        tem = gcd (gcd (tem, st[l][k]), st[r - (1 << k) + 1][k]);
    return tem;
}
int main() {
    ios::sync_with_stdio (0);
    cin >> n;
    int tol = 0;
    for (int i = 1; i <= n; i++)  {
        cin >> st[i][0];
        if (pos.find (st[i][0]) == pos.end() ) tol++, pos[st[i][0]] = tol;
        f[pos[st[i][0]]].push_back (i);
    }
    ST();
    cin >> t;
    for (int i = 1, 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 +1<< endl;
    }
}

时间: 2024-08-02 11:05:01

Codeforces 474F - Ant colony的相关文章

Codeforces G. Ant colony

题目描述: F. Ant colonytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard 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.

Codeforces Round #271 (Div. 2) F.Ant colony(线段树 + 统计区间内某数的个数)

F. Ant colony 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 c

Codeforces 29D Ant on the Tree 树的遍历 dfs序

题目链接:点击打开链接 题意: 给定n个节点的树 1为根 则此时叶子节点已经确定 最后一行给出叶子节点的顺序 目标: 遍历树并输出路径,要求遍历叶子节点时按照给定叶子节点的先后顺序访问. 思路: 给每个节点加一个优先级. 把最后一个叶子节点到父节点的路径上的点优先级改为1 把倒数第二个叶子节点到父节点的路径上的点优先级改为2 如此每个点就有一个优先级,每个访问儿子节点时先访问优先级大的即可 对于无解的判断:得到的欧拉序列不满足输入的叶子节点顺序即是无解. #include <cstdio> #

CodeForces 29D Ant on the Tree

给一颗树,1为根,要求遍历树上所有点,给出叶子结点的访问顺序,限制每条边至多访问两次. 首先这是一棵树,那么两点之间的路线是确定的,所以我第一遍dfs预处理出从根节点到每个叶子的路径保存,以便后面输出. 那么就按照题目要求输出叶子结点的顺序依次输出,然后从一个叶子到下一个叶子的时候,从他们的最近公共祖先转折,所以我还预处理了相邻两个叶子结点的LCA. #include <iostream> #include <cstdlib> #include <cstring> #i

ant colony algorithm &amp;&amp; decision tree

// AntColony.cpp : 定义控制台应用程序的入口点. // //#include "stdafx.h" #include<iostream> #include<math.h> #include<time.h> #include <fstream> #include <string> #include <iostream> #include <vector> using namespace

[bzoj3872][Poi2014]Ant colony_树形dp

Ant colony bzoj-3872 Poi-2014 题目大意:说不明白.....题目链接 注释:略. 想法:两个思路都行. 反正我们就是要求出每个叶子节点到根节点的每个路径权值积. 可以将边做为代理根.或者将边断掉. 最后,附上丑陋的代码... ... #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; t

【智能算法】粒子群寻优算法

1.理论基础 粒子群算法(particle swarm optimization,PSO)是计算智能领域中的一种生物启发式方法,属于群体智能优化算法的一种,常见的群体智能优化算法主要有如下几类: (1)蚁群算法(Ant Colony Optimization,简称ACO)[1992年提出]: (2)粒子群优化算法(Particle Swarm Optimization,简称PSO)[1995年提出](简单易于实现,也是目前应用最为广泛的群体智能优化算法): (3)菌群优化算法(Bacterial

计算机会议排名等级

http://blog.sina.com.cn/s/blog_9c411c310102vs2g.html 附件是计算机领域的学术会议等级排名情况,分为A+, A, B, C, L 共5个档次.其中A+属于顶级会议,基本是这个领域全世界大牛们参与和关注最多的会议.国内的研究者能在其中发表论文的话,是很值得骄傲的成就.A类也是非常好的会议了,尤其是一些热门的研究方向,A类的会议投稿多录用率低,部分A类会议影响力逐步逼近A+类会议.B类的会议分两种,一种称为盛会级,参与的人多,发表的论文也多,论文录用

【转载】发个有用的:国际学术期刊会议大排名

Rank Conference Full Name1 OSDI Operating Systems Design and Implementation2 SOSP Symposium on Operating Systems Principles3 SIGCOMM Special Interest Group on Data Communication4 MOBICOM Mobile Computing and Networking5 SIGGRAPH Annual Conference on