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 = ; 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的更多相关文章
- CodeForces 474F Ant colony ST+二分
Ant colony 题解: 因为一个数是合法数,那么询问区间内的其他数都要是这个数的倍数,也就是这个区间内的gcd刚好是这个数. 对于这个区间的gcd来说,不能通过前后缀来算. 所以通过ST表来询问 ...
- Codeforces G. Ant colony
题目描述: F. Ant colonytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputo ...
- Codeforces 474 F. Ant colony
线段树求某一段的GCD..... F. Ant colony time limit per test 1 second memory limit per test 256 megabytes inpu ...
- 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 ...
- [BZOJ3872][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...
- codeforces 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...
- 【BZOJ3872】Ant colony(二分,动态规划)
[BZOJ3872]Ant colony(二分,动态规划) 题面 又是权限题... Description There is an entrance to the ant hill in every ...
- 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]只蚂蚁.这些蚂蚁会相继进入树中, ...
随机推荐
- [QT]Qt+VS2012+Win8 64Bit安装
学习Qt鸟,当年没听@Coding_Peon(http://weibo.com/u/1764451551?topnav=1&wvr=5&topsug=1)话好好学习QT和Python之 ...
- [转载]函数getopt(),及其参数optind
最近用到了getopt()这个函数,对它进行了一些了解.这篇博文还是写的非常清楚的.值得学习.最近在改进一个开源项目,希望自己能静下心好好分析代码. ------------------------- ...
- ACM1228_STL的应用
#include<iostream> #include<string> #include<map> using namespace std; map<stri ...
- 《Numerical Methods》-chaper7-解线性方程组的直接方法和最小二乘问题
基于我们在线性代数中学习过的知识,我们知道解线性方程组本质上就是Gauss消元,也就是基于增广矩阵A的矩阵初等变换.关于数学层面的内容这里不做过多的介绍,这里的侧重点是从数值计算的角度来看这些常见的问 ...
- 《University Calculus》-chape10-向量和空间几何学-叉积
叉积概念的引入: 在平面中我们为了度量一条直线的倾斜状态,为引入倾斜角这个概念.而通过在直角坐标系中建立tan α = k,我们实现了将几何关系和代数关系的衔接,这其实也是用计算机解决几何问题的一个核 ...
- Redis教程02——管道(Pipelining)
请求/响应协议和RTT Redis是一个使用客户端/服务器模型(也被称作请求/响应协议)的TCP服务器. 这说明通常来讲一个一个请求的实现有以下步骤: 客户端发送请求到服务器,并从socket中以堵塞 ...
- rnqoj-82-又上锁妖塔-dp
又是一个敢想就敢做的题目... 同时记录更新两个状态 dp[i] :第i层是飞上去的 df[i] :第i层是走上去的 dp[i]=min(df[i-1],df[i-2]); df[i]=min(dp ...
- Windows 10 Technical Preview 10041 使用 IIS Express 运行网站应用程序异常
在 Windows 10 中使用 Visual Studio 2013 Ultimate with Update 4 开发网站,9926 的时候还好好的,升到 10041 就不能调试了: “Syste ...
- TortoiseSVN搭建本地版本库及简单操作使用
TortoiseSVN是windows上一款著名的版本控制软件,对于我们管理自己的代码,特别是对一个团队来说,非常重要. 本文探讨的是如何搭建本地的版本库. (1)安装TortoiseSVN之后需要创 ...
- Tomcat配置多个端口号或多个应用
一.在Tomcat下配置一个应用服务(service)中,配置多个端口号. 即一个service配置多个端口,项目可以通过多个端口访问. 修改tomcat-home\conf下的server.xml, ...