题目链接: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. 移动端利用rem实现自适应布局

    好久没有写博客了,刚好说说最近遇到的移动端布局问题吧. 本来一直是觉得我的页面布局能力还是不错的,当然,是相对于较基础的来说还是不错的.不过,自己写的案例终归是跟实际开发有区别的,自己写案例的是觉得这 ...

  2. 购买的wemall 6.0商城系统源码分享

    使用方法 1.解压目录 2.cd wemall6 && npm i 3.配置config下的config.json 4.npm start 摒弃以往的开发框架thinkphp,使用no ...

  3. 【前端】ACE Editor 简易使用示例

    身为一个早已退役的Oier,当然忘不了当年一个个OJ页面上的代码显示和代码编辑器. 其中,洛谷使用的ACE Editor就是之一,非常的简洁美观.以及实际上在前端页面上搭建一个ACE Editor也是 ...

  4. web从入门开始(7)-----css

    CSS的语法格式 l  一个CSS规则,有"选择器"和"格式声明语句"构成 l  选择器:就是选择HTML标记,换句话说就是给那个HTML标记加样式 l  格式 ...

  5. php线程安全与非线程安全版的区别

    Thread Safe(线程安全)和 None Thread Safe(非线程安全) 背景: Linux/Unix系统采用多进程的工作方式,而Windows系统采用多线程的工作方式. CGI模式是建立 ...

  6. 百度推出 MIP Shell 链接

    在站长将站点 MIP 化时,需要关注 URL 的一共有三个:MIP URL, MIP-Cache URL 以及 MIP-Shell URL. 从 URL 说起 在互联网中,URL 定义页面的地址,每个 ...

  7. TCP/IP笔记(三)数据链路层

    数据链路的作用 数据链路层的协议定义了通过通信媒介互连的设备之间传输的规范.通信媒介包括双绞线电缆.同轴电缆.光纤.电波以及红外线等介质.此外,各个设备之间有时也会通过交换机.网桥.中继器等中转数据. ...

  8. iOS开发之清除缓存

    NSFileManager *mgr = [NSFileManager defaultManager]; NSString *cachePath = [NSSearchPathForDirectori ...

  9. vue-miniQQ——基于Vue2实现的仿手机QQ单页面应用(接入了聊天机器人,能够进行正常对话)

    使用Vue2进行的仿手机QQ的webapp的制作,作品由个人独立开发,源码中进行了详细的注释. 由于自己也是初学Vue2,所以注释写的不够精简,请见谅. 项目地址 https://github.com ...

  10. ES6 学习笔记(一)let,const和解构赋值

    let和const let和const是es6新增的两个变量声明关键字,与var的不同点在于: (1)let和const都是块级作用域,在{}内有效,这点在for循环中非常有用,只在循环体内有效.va ...