CodeForces 474F Ant colony ST+二分
题解:
因为一个数是合法数,那么询问区间内的其他数都要是这个数的倍数,也就是这个区间内的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+二分的更多相关文章
- Codeforces 474F - Ant colony
注意到每个区间生存下来的蚂蚁的长度等于区间的gcd 于是可以先预处理出区间的gcd 然后二分查找就好了 预处理gcd我这里用的是倍增法 总的时间复杂度O(NlogN) /* Cf 271F 倍增求区间 ...
- 【BZOJ3872】Ant colony(二分,动态规划)
[BZOJ3872]Ant colony(二分,动态规划) 题面 又是权限题... Description There is an entrance to the ant hill in every ...
- Codeforces G. Ant colony
题目描述: F. Ant colonytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputo ...
- bzoj 3872: [Poi2014]Ant colony -- 树形dp+二分
3872: [Poi2014]Ant colony Time Limit: 30 Sec Memory Limit: 128 MB Description There is an entranc ...
- 【BZOJ3872】[Poi2014]Ant colony 树形DP+二分
[BZOJ3872][Poi2014]Ant colony Description 给定一棵有n个节点的树.在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁.这些蚂蚁会相继进入树中, ...
- Codeforces 474 F. Ant colony
线段树求某一段的GCD..... F. Ant colony time limit per test 1 second memory limit per test 256 megabytes inpu ...
- 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 ...
- [BZOJ3872][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...
- codeforces 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...
随机推荐
- .net持续集成测试篇之Nunit常见断言
系列目录 Nunit测试基础之简单断言 在开始本篇之前需要补充一些内容,通过前面搭建Nunit测试环境我们知道要使一个方法成为单元测试方法首先要在此方法所在类加上TestFixture注解,并且在该方 ...
- PHP xdebug API接口优化揪出了getimagesize这个鬼
在API优化list中,公司客户系统的服务号客服有个获取聊天消息的接口getHistory请求时间很长,就去优化了下,记下过程. 一,配置环境,追踪使用Xdebug: 1.在https://xdebu ...
- Redis Sentinel基本实现原理
一.出现的背景: Redis 主从复制模式下一旦主节点由于故障不能提供服务,需要人工将从节点晋升为主节点,同时还要通知应用方更新主节点地址,对于很多应用这种场景的这种故障处理方式是非常浪费人力的.为了 ...
- java学习笔记(中级篇)—java实现高质量图片压缩
使用java几十行代码实现一个高质量图片压缩程序,再也不用去自己找网络的压缩程序啦!而且很多网上的工具还有水印或者其他的限制,自己动手写一个简单的应用,是再合适不过了. 一.实现原理 1.声明两个字符 ...
- JVM系列(2)- jmap+mat实战内存溢出
熟悉几个监控JVM的常用命令 1. jps -l 查出当前服务器运行的java进程 --- 2. jinfo用法(结合jps -l查到进程ID) 1).查看最大堆内存:jinfo -flag MaxH ...
- SQL Labs刷题补坑记录(less1-less30)
补坑加1,这几天快速刷一下sqllabs 来巩固下sql注入基础吧,也算是把很久以前没刷的过一遍,do it! 第一部分: LESS1: 直接报错,有回显的注入, http://localhost/s ...
- 【POJ - 2387】Til the Cows Come Home(最短路径 Dijkstra算法)
Til the Cows Come Home 大奶牛很热爱加班,他和朋友在凌晨一点吃完海底捞后又一个人回公司加班,为了多加班他希望可以找最短的距离回到公司.深圳市里有N个(2 <= N < ...
- 维恩贝特面试JAVA后台开发
1 自我介绍 2 链表和数组区别(数组空间连续,且有下标,查找快,但是增删数据效率不高,链表的空间不连续,查找起来慢,但是对数据的增删效率高,链表可以随意扩大,数组不能) 3 sort方法的实现 (A ...
- Linux配置部署_新手向(二)——Nginx安装与配置
目录 前言 Nginx 配置(后续补充) 小结 @ 前言 上一篇整完Linux系统的安装,紧接着就开始来安装些常用的东西吧,首先Nginx. Nginx 简介 Nginx作为转发,负载均衡,凭着其高性 ...
- 为什么不建议在hbase中使用过多的列簇
我们知道,hbase表可以设置一个至多个列簇(column families),但是为什么说越少的列簇越好呢? 官网原文: HBase currently does not do well with ...