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 ...
随机推荐
- git push 提示
我运行git push -u origin master 时提示如下: To git@github.com:userName/project.git ! [rejected] master -> ...
- hdu 4882 ZCC Loves Codefires (贪心 推导)
题目链接 做题的时候凑的规律,其实可以 用式子推一下的. 题意:n对数,每对数有e,k, 按照题目的要求(可以看下面的Hint就明白了)求最小的值. 分析:假设现在总的是sum, 有两个e1 k1 e ...
- bzoj3983
显然我们得到这样几个结论 1.每次攻击对方一定是攻击最大的 2.自己合并也是合并最大和次大的 我们只要穷举下一开始是攻击还是合并,之后就是能攻击先攻击否则就合并 ..] of int64; var a ...
- Android listView如何判断数据多屏显示 (2012-03-06 09:47:04)
@Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int tot ...
- Azure SQL 数据库与新的数据库吞吐量单位
在这一期中,Scott 与 Azure SQL 数据库性能首席项目经理主管 Tobias Ternstrom 一起详细阐释了新的数据库吞吐量单位 (Database Throughput Unit, ...
- <一>SQL优化1-4
第一条:去除在谓词列上编写的任何标量函数 --->在select 显示列上使用标量函数是可以的.但在where语句后的过滤条件部分对列使用函数,需要考虑.因为执行sql的引擎会因为 ...
- 转载RabbitMQ入门(1)--介绍
目录[-] "Hello World" (使用java客户端) 发送 接收 把所有放在一起 前面声明本文都是RabbitMQ的官方指南翻译过来的,由于本人水平有限难免有翻译不当的地 ...
- delphi中计算指定日期是该月第几周的函数
NthDayOfWeek 计算并返回指定日期是该月第几周 Unit:DateUtils function NthDayOfWeek(const AValue: TDateTime): Word; ...
- VS2013密匙
在网上找到的,亲测有用: BWG7X-J98B3-W34RT-33B3R-JVYW9
- BLOCK 死循环
__weak typeof(self) weakSelf = self; myObj.myBlock = ^{ __strong typeof(self) strongSelf = weak ...