Ant colony

题解:

因为一个数是合法数,那么询问区间内的其他数都要是这个数的倍数,也就是这个区间内的gcd刚好是这个数。

对于这个区间的gcd来说,不能通过前后缀来算。

所以通过ST表来询问这个区间的gcd。

那么题目就变成了询问一个区间内有多少个k。

我们对于每个数都离散化之后,在相应的数字存下下标。

然后二分一下个数。

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = 1e5 + ;
int a[N], b[N];
vector<int> vc[N];
int m, n;
int id(int x){
int pos = lower_bound(b+, b++m, x) - b;
if(b[pos] == x) return pos;
return -;
}
int Log[N];
struct ST {
int dp[N][], a[N];
void init(int n) {
for(int i = -(Log[]=-); i < N; i++)
Log[i] = Log[i - ] + ((i & (i - )) == );
for(int i = ; i <= n; ++i) dp[i][] = a[i];
for(int j = ; j <= Log[n]; j++)
for(int i = ; i+(<<j)- <= n; i++)
dp[i][j] = __gcd(dp[i][j-], dp[i+(<<(j-))][j-]);
}
int Query(int l, int r) {
int k = Log[r - l + ];
return __gcd(dp[l][k], dp[r-(<<k)+][k]);
}
}st;
int Ac(){
scanf("%d", &n);
for(int i = ; i <= n; ++i){
scanf("%d", &a[i]);
b[i] = a[i];
st.a[i] = a[i];
}
st.init(n);
sort(b+, b++n);
m = unique(b+, b++n) - (b+);
for(int i = ; i <= n; ++i){
vc[id(a[i])].pb(i);
}
int q;
scanf("%d", &q);
int l, r;
while(q--){
scanf("%d%d", &l, &r);
int x = st.Query(l, r);
x = id(x);
int ans = r - l + ;
if(~x){
ans -= upper_bound(vc[x].begin(), vc[x].end(), r) - lower_bound(vc[x].begin(), vc[x].end(), l);
}
printf("%d\n", ans);
}
return ;
} int main(){
Ac();
return ;
}

CodeForces 474F Ant colony ST+二分的更多相关文章

  1. Codeforces 474F - Ant colony

    注意到每个区间生存下来的蚂蚁的长度等于区间的gcd 于是可以先预处理出区间的gcd 然后二分查找就好了 预处理gcd我这里用的是倍增法 总的时间复杂度O(NlogN) /* Cf 271F 倍增求区间 ...

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

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

  3. Codeforces G. Ant colony

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

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

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

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

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

  6. Codeforces 474 F. Ant colony

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

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

  8. [BZOJ3872][Poi2014]Ant colony

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

  9. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

随机推荐

  1. 聊一聊Java的枚举enum

    一. 什么是枚举 枚举是一种数据类型,具有集合的一些特点,可以存放多个元素,但存储对象有限且固定,枚举也有比较常见的使用场景,如我们需要表达性别(男.女),颜色(红.黄.蓝),星期(星期一.星期二.. ...

  2. 面试必谈的哈希,.Net 程序员温故而知新

    引言: 作为资深老鸟,有事没事,出去面试:找准差距.定位价值. 面试必谈哈希, Q1:什么是哈希? Q2:哈希为什么快? Q3:你是怎么理解哈希算法利用空间换取时间的? Q4:你是怎么解决哈希冲突的? ...

  3. jboss 未授权访问漏洞复现

    jboss 未授权访问漏洞复现 一.漏洞描述 未授权访问管理控制台,通过该漏洞,可以后台管理服务,可以通过脚本命令执行系统命令,如反弹shell,wget写webshell文件. 二.漏洞环境搭建及复 ...

  4. Linq查找最大值max最小值min效率比较

    对linq查找极值的几种方法做一个效率上的比较 // 首先创建了一个10_000_000大小的PointF列表 var rdn = new Random(); var points = Enumera ...

  5. 假装前端工程师(一)Icework + GitHub pages 快速构建可自定义迭代开发的 react 网站

    icework + gh-pages 超快部署超多模版页面 项目地址:https://github.com/yhyddr/landingpage效果地址:https://yhyddr.github.i ...

  6. POI通用导出Excel数据(包括样式设计)

    前言 前一段时间我写过通用的导入Excel,前几天也写了导出pdf格式的,还有我之前搞得导出Word,我在之前的博客也都介绍了导出和导入是一个道理,无非是一个获取一个是赋值.昨天有一位同仁看了我的Ex ...

  7. hadoop开启Service Level Authorization 服务级认证-SIMPLE认证-过程中遇到的坑

    背景描述: 最近在进行安全扫描的时候,说hadoop存在漏洞,Hadoop 未授权访问[原理扫描],然后就参考官方文档及一些资料,在测试环境中进行了开启,中间就遇到了很多的坑,或者说自己没有想明白的问 ...

  8. websql操作类封装

    在之前,我写了一个websql的封装类库,代码如下: (function(win) { function smpWebSql(options){ options = options || {}; th ...

  9. c# 读取 txt 文件中数据(int)

    今天在学图的算法做测试是,需要读取文本文件中的点坐标,本来很简单的事情,折腾了半天,记录一下找到的一种简单粗暴的解决方法,以便以后查看. 第一种方法 : StringReader string lin ...

  10. c语言和c++的交换函数

    #include<iostream> using namespace std; namespace LiuGang{//在命名空间中写函数 void swap(int&aa,int ...