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]只蚂蚁.这些蚂蚁会相继进入树中, ...
随机推荐
- Light OJ 1036 - A Refining Company
题目大意: 一个m*n的矩阵,里面有两种矿物质铀和镭,现在要把铀和镭运送到指定位置.北边是炼镭厂,西边是了炼铀厂. 现在要建立传送带,传送带有两种,一种是从东到西,另一种是从南到北,传送带不能交叉,并 ...
- Unity3d shader之次表面散射(Subsurface Scattering)
次表面散射是一种非常常用的效果,可以用在很多材质上如皮肤,牛奶,奶油奶酪,番茄酱,土豆等等 初衷是想做一个牛奶shader的,但后来就干脆研究了sss这是在vray上的次表面散射效果 这是本文在un ...
- 【转】OpenGL基础图形编程(二)
原文:http://blog.chinaunix.net/uid-20638550-id-1909184.html 分类: 十一.位图与图像 11.1.位图 11.1.1 位图(Bitmap)与字符 ...
- Hdu 3966-Aragorn's Story LCT,动态树
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3966 Aragorn's Story Time Limit: 10000/3000 MS (Java/Ot ...
- AMBA总线介绍
The Advanced Microcontroller Bus Architecture (AMBA) specification defines an on- chip communication ...
- [JIT_APP]Java基础知识总结
一.Java语言的基础知识 1. 开发Java语言的公司 美国Sun(Sum Microsystems)公司开发. 2.Java的3个版本 J2SE(Java2 Standard Edition) ...
- mysql 表设计
- jQuery各种效果举例
jQuery 所有jQuery详细使用说明请见:http://www.php100.com/manual/jquery/ jQuery的作用是操作浏览器html,从而达到用户的可视化效果,按照功能可分 ...
- 大牛博客!Spark / Hadoop / Kafka / HBase / Storm
在这里,非常感谢下面的著名大牛们,一路的帮助和学习,给予了我很大的动力! 有了Hadoop,再次有了Spark,一次又一次,一晚又一晚的努力相伴! HBase简介(很好的梳理资料) 1. 博客主页:h ...
- Linux开启相关端口及查看已开启端口
防火墙层面: /sbin/iptables -I INPUT -p tcp --dport 8011 -j ACCEPT #开启8011端口 /etc/rc.d/init.d/iptables ...