Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F
题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数就是答案。
要是符合条件的话,那这个数的大小一定是等于gcd(a[l]...a[r])。
我们求区间gcd的话,既可以利用线段树性质区间递归下去然后返回求解,但是每次查询是log的,所以还可以用RMQ,查询就变成O(1)了。
然后求解区间内有多少个数的大小等于gcd的话,也是利用线段树的性质,区间递归求解之,复杂度也是log的。
//#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e5 + ;
int gcd[N][];
struct SegTree {
int l , r , Min , num;
}T[N << ]; int GCD(int a, int b) {
return b ? GCD(b, a % b) : a;
} void ST(int n) {
for(int k = ; k < ; ++k) {
for(int i = ; i + ( << k) - <= n; ++i) {
gcd[i][k] = GCD(gcd[i][k - ], gcd[i + ( << (k - ))][k - ]);
}
}
} void build(int p , int l , int r) {
int mid = (l + r) >> ;
T[p].l = l , T[p].r = r , T[p].num;
if(l == r) {
T[p].Min = gcd[l][];
T[p].num = ;
return ;
}
build(p << , l , mid);
build((p << )| , mid + , r);
if(T[p << ].Min == T[(p << )|].Min) {
T[p].Min = T[p << ].Min;
T[p].num = T[p << ].num + T[(p << )|].num;
}
else if(T[p << ].Min < T[(p << )|].Min) {
T[p].Min = T[p << ].Min;
T[p].num = T[p << ].num;
}
else {
T[p].Min = T[(p << )|].Min;
T[p].num = T[(p << )|].num;
}
} int query(int p , int l , int r , int g) {
int mid = (T[p].l + T[p].r) >> ;
if(T[p].l == l && T[p].r == r) {
return T[p].Min == g ? T[p].num : ;
}
if(r <= mid) {
return query(p << , l , r , g);
}
else if(l > mid) {
return query((p << )| , l , r , g);
}
else {
return query(p << , l , mid , g) + query((p << )| , mid + , r , g);
}
} int main()
{
int n, q, l, r;
scanf("%d", &n);
for(int i = ; i <= n; ++i)
scanf("%d", &gcd[i][]);
ST(n);
build( , , n);
scanf("%d", &q);
while(q--) {
scanf("%d %d", &l, &r);
int k = log2(r - l + );
int g = GCD(gcd[l][k], gcd[r - ( << k) + ][k]);
printf("%d\n", r - l + - query( , l , r , g));
}
return ;
}
Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)的更多相关文章
- 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 ...
- Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)
题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...
- Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)
题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...
- Codeforces Round #271 (Div. 2) F ,E, D, C, B, A
前言:最近被线段树+简单递推DP虐的体无完肤!真是弱! A:简单题,照着模拟就可以,题目还特意说不用处理边界 B:二分查找即可,用lower_lound()函数很好用 #include<stri ...
- Codeforces Round #332 (Div. 2) C. Day at the Beach 线段树
C. Day at the Beach Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/599/p ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸
D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- Codeforces Round #343 (Div. 2) D - Babaei and Birthday Cake 线段树+DP
题意:做蛋糕,给出N个半径,和高的圆柱,要求后面的体积比前面大的可以堆在前一个的上面,求最大的体积和. 思路:首先离散化蛋糕体积,以蛋糕数量建树建树,每个节点维护最大值,也就是假如节点i放在最上层情况 ...
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp
D. Babaei and Birthday Cake 题目连接: http://www.codeforces.com/contest/629/problem/D Description As you ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模
D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his h ...
随机推荐
- php有些系统会报错或提示 Cannot modify header information - headers already sent by
Warning: Cannot modify header information - headers already sent by (output started at /home/test/do ...
- JS省队集训记
不知不觉省队集训已经结束,离noi也越来越近了呢 论考前实战训练的重要性,让我随便总结一下这几天的考试 Day 1 T1 唉,感觉跟xj测试很像啊?meet in middle,不过这种题不多测是什么 ...
- Qt之自定义界面(添加自定义标题栏)
简述 通过上节内容,我们实现了自定义窗体的移动,但是我们缺少一个标题栏来显示窗体的图标.标题,以及控制窗体最小化.最大化.关闭的按钮. 自定义标题栏后,所有的控件我们都可以定制,比如:在标题栏中添加换 ...
- org.tigris.subversion.javahl.ClientException: Attempted to lock an already-locked dir异常解决方法
myeclipse用svn提交的时候报错: Attempted to lock an already-locked dir svn: Working copy 'D:/Program Files/My ...
- Hibernate映射集合属性
Hibernate要求持久化集合属性字段必须声明为接口,实际的接口可以是java.util.Set,java.util.Collection,java.util.List,java.util.Map, ...
- UVA 11090 Going in Cycle!!(二分答案+判负环)
在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...
- php yii框架使用MongoDb
1.安装 运行 php composer.phar require --prefer-dist yiisoft/yii2-mongodb or add "yiisoft/yii2-mongo ...
- 基于phpExcel写的excel类
<?php /* * 类的功能 * 传入二位数组导出excel * 传入excel 导出二位数组 * @author mrwu */ require('PHPExcel.php'); requi ...
- python - pip 从已有的安装列表 安装
已经安装好的机器:sudo pip freeze > install_list.list 需要安装的机器:sudo pip install -r install_list.list
- mssql的delete用用到被delete的表的别名
+' delete m from '+@strDBName +'.dbo.m_device as m where not exists ' +' (select 1 from @tmpDevice w ...