题目链接:http://codeforces.com/contest/474/problem/F

一个数组,每一次询问一个区间中有多少个数字可以整除其他所有区间内的数字。

能够整除其他所有数字的数一定是这些数字的gcd,所以可以用一个线段树来查询区间gcd。

接着需要统计区间内这个数字的出现个数,在读取数组时,顺便保存一个pair<int,int>键为数字,val为位置的数组,再sort一下,统计区间内某个数字出现次数,直接upper_bound-lower_bound即可。

 #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#include <numeric>
#include <cassert> using namespace std; const int N=1e5+; int s[N];
pair<int,int> d[N];
int t[N<<]; int gcd(int a,int b){
if (b==) return a;
return gcd(b,a%b);
}
void up(int rt) {
t[rt]=gcd(t[rt<<],t[rt<<|]);
}
void build(int l,int r,int rt) {
if (l==r){
t[rt]=s[l];
return;
}
int m=(l+r)>>;
build(l,m,rt<<);
build(m+,r,rt<<|);
up(rt);
}
int ask(int L,int R,int l,int r,int rt) {
if (L<=l&&r<=R) {
return t[rt];
}
int m=(l+r)>>;
int ret=,u=,v=;
if (L<=m)
u=ask(L,R,l,m,rt<<);
if (R>m)
v=ask(L,R,m+,r,rt<<|);
return gcd(u,v);
}
int main () {
int n;
scanf("%d",&n);
for (int i=;i<=n;i++) {
scanf("%d",s+i);
d[i-]=make_pair(s[i],i);
}
build(,n,);
sort(d,d+n);
int m;
scanf("%d",&m);
while (m--) {
int l,r;
scanf("%d %d",&l,&r);
int g=ask(l,r,,n,);
int u=lower_bound(d,d+n,make_pair(g,l))-d;
int v=lower_bound(d,d+n,make_pair(g,r+))-d;
printf("%d\n",r-l+-(v-u));
//printf("%d %d %d\n",g,u,v);
}
return ;
}

CF #271 F Ant colony 树的更多相关文章

  1. [CF] 474 F. Ant colony

    区间重复不会影响GCD,ST表当然是支持的啦,常数这么小. 学到了三个东西: 1.lower_bound返回的是大于等于的位置,要判是否不存在(end())和是否超出所求[x,y]范围. 2.ST表更 ...

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

  3. Codeforces 474 F. Ant colony

    线段树求某一段的GCD..... F. Ant colony time limit per test 1 second memory limit per test 256 megabytes inpu ...

  4. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

  5. Codeforces G. Ant colony

    题目描述: F. Ant colonytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputo ...

  6. [BZOJ3872][Poi2014]Ant colony

    [BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...

  7. bzoj 3872: [Poi2014]Ant colony -- 树形dp+二分

    3872: [Poi2014]Ant colony Time Limit: 30 Sec  Memory Limit: 128 MB Description   There is an entranc ...

  8. 【BZOJ3872】[Poi2014]Ant colony 树形DP+二分

    [BZOJ3872][Poi2014]Ant colony Description 给定一棵有n个节点的树.在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁.这些蚂蚁会相继进入树中, ...

  9. 【BZOJ3872】Ant colony(二分,动态规划)

    [BZOJ3872]Ant colony(二分,动态规划) 题面 又是权限题... Description There is an entrance to the ant hill in every ...

随机推荐

  1. BUG,带给我的思考

    今天打开EverNote时,翻到了四年前在anjuke时做的一些bug分析总结.现在回过头看看也是有些价值所在,挑选出部分bug分享,希望能有所启发. 一. iOS新房APP4.4由于在91市场进行试 ...

  2. loadrunner:判断是否服务器连接池瓶颈

    分析Web Resources中的Connections per second可以判断是否服务器连接池瓶颈. connections per second会给出两种不同状态的连接数:中断的连接和新建的 ...

  3. “Dynamic Web Module 3.0 requires Java 1.6 or newer.”错误 (转别人)

    eclipse maven 在项目的pom.xml的<build></build>标签中加入: <plugins> <plugin> <group ...

  4. eclipse中以debug方式启动tomcat报错

    在eclipse中debug  Tomcat报错,错误如下: FATAL ERROR in native method: JDWP No transports initialized, jvmtiEr ...

  5. oracle表空间创建

    /*分为四步 *//*第1步:创建临时表空间  */create temporary tablespace user_temp  tempfile 'D:\oracle\oradata\Oracle9 ...

  6. web简易MP3播放插件 Aplayer篇章一

    效果如图所示: 写入初始代码,定义一个id唯一的div player1 <!DOCTYPE html> <html> <head> <meta charset ...

  7. SLF4J 的几种实际应用模式--之三:JCL-Over-SLF4J+SLF4J

    我们前面已经讲过了 SLF4J 的两种用法:SLF4J+Log4J  和 SLF4J+Logback,那是在比较理想的情况下,所用组件只使用了 SLF4J 这一种统一日志框架的时候.可是 JCL 一直 ...

  8. spring-AOP-基于@AspectJ切面的小例子

    条件: 1.jdk的版本在5.0或者以上,否则无法使用注解技术 2.jar包: aspectjweaver-1.7.4.jar aspectjrt-1.7.4.jar spring-framework ...

  9. Angular 2的12个经典面试问题汇总(文末附带Angular测试)

    Angular作为目前最为流行的前端框架,受到了前端开发者的普遍欢迎.不论是初学Angular的新手,还是有一定Angular开发经验的开发者,了解本文中的12个经典面试问题,都将会是一个深入了解和学 ...

  10. webStorm在Node.js平台下服务器配置及Express配置

    ************************************** 本博客从此篇开始,将从零基础开始逐渐深入地向各位博友分享node.js学习经验,如有需要请通过新浪微博@牙疼格尔联系博主, ...