题目链接: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 线段树)的更多相关文章

  1. 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 ...

  2. Codeforces Round #530 (Div. 2)F Cookies (树形dp+线段树)

    题:https://codeforces.com/contest/1099/problem/F 题意:给定一个树,每个节点有俩个信息x和t,分别表示这个节点上的饼干个数和先手吃掉这个节点上一个饼干的的 ...

  3. Codeforces Round #271 (Div. 2) F题 Ant colony(线段树)

    题目地址:http://codeforces.com/contest/474/problem/F 由题意可知,最后能够留下来的一定是区间最小gcd. 那就转化成了该区间内与区间最小gcd数相等的个数. ...

  4. Codeforces Round #271 (Div. 2) F ,E, D, C, B, A

    前言:最近被线段树+简单递推DP虐的体无完肤!真是弱! A:简单题,照着模拟就可以,题目还特意说不用处理边界 B:二分查找即可,用lower_lound()函数很好用 #include<stri ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #343 (Div. 2) D - Babaei and Birthday Cake 线段树+DP

    题意:做蛋糕,给出N个半径,和高的圆柱,要求后面的体积比前面大的可以堆在前一个的上面,求最大的体积和. 思路:首先离散化蛋糕体积,以蛋糕数量建树建树,每个节点维护最大值,也就是假如节点i放在最上层情况 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. mysql定时计划任务,ON COMPLETION [NOT] PRESERVE 当单次计划任务执行完毕后或当重复性的计划任务执行到了ENDS阶段。而声明PRESERVE的作用是使事件在执行完毕后不会被Drop掉

    当为on completion preserve 的时候,当event到期了,event会被disable,但是该event还是会存在当为on completion not preserve的时候,当 ...

  2. Effective C++学习笔记 条款06:如不想使用编译器自动生成的函数,就该明确拒绝

    一.为驳回编译器自动提供的机能,可将相应成员函数声明为private并且不予实现.(如果你仅仅是自己不实现的话,编译器会帮你实现) 如: class A { public: A(const strin ...

  3. 函数ut_2_log

    计算某个数的对数(最大的) 例如 16 计算后为 4 2的4次方为16 例如15 计算后为3 2的3次方为8 /******************************************** ...

  4. HDU 1280 前m大的数【哈希入门】

    题意:中文的题目= =将各种组合可能得到的和作为下标,然后因为不同组合得到的和可能是一样的, 所以再用一个数组num[]数组,就可以将相同的和都记录下来 #include<iostream> ...

  5. asp.net读excle的数据类型不统一取出空值问题

    如果表格里某列全是数字或是字符没问题,但如果混合了全是数字和部分字符就会有部分读取为空连接EXCEL方式如下 string strConn = "Provider=Microsoft.Jet ...

  6. UVA 1660 Cable TV Network 电视网络(无向图,点连通度,最大流)

    题意:给一个无向图,求其点连通度?(注意输入问题) 思路: 如果只有1个点,那么输出“1”: 如果有0条边,那么输出“0”: 其他情况:用最大流解决.下面讲如何建图: 图的连通度问题是指:在图中删去部 ...

  7. memcache的最佳实践方案。

    基本问题 1.memcached的基本设置 1)启动Memcache的服务器端 # /usr/local/bin/memcached -d -m 10 -u root -l 192.168.0.200 ...

  8. 【JSP】JSP向MySQL写入|读出中文数据——乱码问题

    注意第14行

  9. JS 代码编一个倒时器

    有时候在生活中,你需要一个JavaScript倒计时时钟,而不是一个末日装置设备.不管你是否有一次约会,销售.促销.或者游戏,你可以受益于使用原生JavaScript构建一个时钟,而不是拿到一个现成的 ...

  10. Oracle :一次数据库连接,返回多个结果集

    1. 一次数据库连接,返回多个结果集 1.1 建立包规范 create or replace package QX_GDJTJ is -- Author : xxx -- Created : 2012 ...