题目链接: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. KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架之koahub-skip

    koahub-skip koahub skip middleware koahub skip Conditionally skip a middleware when a condition is m ...

  2. SqlCommandBuilder类是如何构建T-Sql语句

    本篇博客默认你看了[DataTable中AcceptChanges()方法的DataRowRowState属性]这篇博客. 在使用SqlCommandBuilder很简单,就是创建一个SqlComma ...

  3. Spring定时任务解决博客缓存数据更新问题

    最近在做博客系统的时候,由于很多页面都有右边侧边栏,内容包括博客分类信息,归档日志,热门文章,标签列表等,为了不想每次访问页面都去查询数据库,因为本身这些东西相对来说是比较固定的,但是也有可能在网站后 ...

  4. TypeScript入门-接口

    ▓▓▓▓▓▓ 大致介绍 在TypeScript里,接口的作用就是为这些类型命名和为你的代码或第三方代码定义契约. ▓▓▓▓▓▓ 接口 例子: function printLabel(labelledO ...

  5. linux系统各种乱码问题

    linux系统乱码问题 最近使用ubuntu操作系统(客户端)在ssh连接linux服务器的时候发现乱码问题,但是本机查看中文显示中文没有问题,只是在使用终端more查看本地或远端gbk之类中文编码的 ...

  6. 使用github+hexo搭建博客笔记

    听说github上可以搭博客,而且不用自己提供空间和维护,哈哈哈 作为一名程序猿,github搭博客对我有种神奇的吸引力,赶紧动手试一试 关于如何使用hexo搭建博客网上好的教程多如牛毛,而且这篇博客 ...

  7. Java中元组的使用

    元组在计算机领域有着特殊的意义,这个名字听起来似乎有些陌生, 平时在写代码也基本没什么应用场景, 然而, 出人意料的是, 元组跟程序设计密切相关, 可能有的同学不知道, 关系数据库中的「纪录」的另一个 ...

  8. 跨站的艺术-XSS Fuzzing 的技巧

    作者 | 张祖优(Fooying)  腾讯云 云鼎实验室 对于XSS的漏洞挖掘过程,其实就是一个使用Payload不断测试和调整再测试的过程,这个过程我们把它叫做Fuzzing:同样是Fuzzing, ...

  9. python之pymysql模块学习(待完善...)

    pymysql介绍 pymysql是在python3.x版本中用于连接mysql服务器的一个库.python2中则使用mysqldb. 安装方法: pip install pymysql 使用实例: ...

  10. iOS开发寻找最近公共view

    新技能 #pragma mark --寻找最近公共view + (NSArray *)superViews:(UIView *)view{ if (view==nil) { return @[]; } ...