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 ...
随机推荐
- MySQL学习笔记二
Ø function 函数 函数的作用比较大,一般多用在select查询语句和where条件语句之后.按照函数返回的结果, 可以分为:多行函数和单行函数:所谓的单行函数就是将每条数据进行独立的计算,然 ...
- 1008. Image Encoding(bfs)
1008 没营养的破题 #include <iostream> #include<cstdio> #include<cstring> #include<alg ...
- 【Todo】深入理解Javascript系列
真的很好,要看 http://www.cnblogs.com/TomXu/archive/2011/12/15/2288411.html
- 宏UT_LIST_ADD_FIRST
/*******************************************************************//** Adds the node as the first ...
- css动画集合地址
CSS3 UI Lib库是由腾讯AlloyTeam前端开发团队建立,主要收集国内外友好体验和创意的界面组件Demo. 它除了使用CSS3技术外,还使用了HTML5,JS,JX,jQuery等技术,来达 ...
- codeforces 334B - Eight Point Sets
题意难懂,其实就是x1<x2<x3与y1<y2<y3两两组合成九个点,去掉(x2,y2),剩余八个.这样的八个点才是满足要求的. 忘去重了 #include<cstdio ...
- HDU 2433 Travel (最短路,BFS,变形)
题意: 给出一个图的所有边,每次从图中删除一条边,求任意点对的路径总和(求完了就将边给补回去).(有重边) 思路: #include <bits/stdc++.h> using names ...
- H.264学习笔记之一(层次结构,NAL,SPS)
一 H.264句法 1.1元素分层结构 H.264编码器输出的Bit流中,每个Bit都隶属于某个句法元素.句法元素被组织成有层次的结构,分别描述各个层次的信息. 图1 H.264分层结构由五层组成,分 ...
- 持有对象:总结JAVA中的常用容器和迭代器,随机数 速查
JAVA使用术语“Collection”来指代那些表示集合的对象,JAVA提供的接口很多,首先我们先来记住他们的层次结构: java集合框架的基本接口/类层次结构 java.util.Collecti ...
- HDU 5818 Joint Stacks
Joint Stacks Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...