[CF] 474 F. Ant colony
区间重复不会影响GCD,ST表当然是支持的啦,常数这么小。
学到了三个东西:
1.lower_bound返回的是大于等于的位置,要判是否不存在(end())和是否超出所求[x,y]范围。
2.ST表更新时存在一个i+(1<<j-1)的下标,存在极大越界隐患,以前数组开的大就无所谓,讲道理边界是要判的。
3.及时存代码,又蓝屏了qwq
#include<iostream>
#include<cstdio>
#include<vector>
#include<map>
using namespace std; inline int rd(){
int ret=,f=;char c;
while(c=getchar(),!isdigit(c))f=c=='-'?-:;
while(isdigit(c))ret=ret*+c-'',c=getchar();
return ret*f;
}
int LOG2 (unsigned int x) {
static const int log_2[] = {
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
};
int l = -;
while (x >= ) { l += ; x >>= ; }
return l + log_2[x];
} const int MAXN=; int n,m;
int a[MAXN];
int f[MAXN][]; int id;
map<int,int> M;
vector<int> V[MAXN]; int gcd(int x,int y){return y?gcd(y,x%y):x;} int query(int x,int y){
if(x==y) return f[x][];
int len=LOG2(y-x+);
return gcd(f[x][len],f[y-(<<len)+][len]);
} int main(){
n=rd();
for(int i=;i<=n;i++) f[i][]=a[i]=rd();
for(int i=;i<=n;i++){
if(!M.count(a[i])) M[a[i]]=++id;
V[M[a[i]]].push_back(i);
}
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<(j-))<=n;i++)
f[i][j]=gcd(f[i][j-],f[i+(<<(j-))][j-]);
m=rd();
int x,y,g,tmp,l,r,ans;
for(int i=;i<=m;i++){
x=rd();y=rd();
g=query(x,y);
tmp=M[g];
r=lower_bound(V[tmp].begin(),V[tmp].end(),y)-V[tmp].begin();
l=lower_bound(V[tmp].begin(),V[tmp].end(),x)-V[tmp].begin();
if(r+V[tmp].begin()==V[tmp].end()||V[tmp][r]>y) r--;
cout<<y-x+-(r-l+)<<endl;
} return ;
}
[CF] 474 F. Ant colony的更多相关文章
- Codeforces 474 F. Ant colony
线段树求某一段的GCD..... F. Ant colony time limit per test 1 second memory limit per test 256 megabytes inpu ...
- CF #271 F Ant colony 树
题目链接:http://codeforces.com/contest/474/problem/F 一个数组,每一次询问一个区间中有多少个数字可以整除其他所有区间内的数字. 能够整除其他所有数字的数一定 ...
- 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 ...
- Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)
题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...
- Codeforces G. Ant colony
题目描述: F. Ant colonytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputo ...
- [BZOJ3872][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...
- 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]只蚂蚁.这些蚂蚁会相继进入树中, ...
- CF 633 F. The Chocolate Spree 树形dp
题目链接 CF 633 F. The Chocolate Spree 题解 维护子数答案 子数直径 子数最远点 单子数最长直径 (最长的 最远点+一条链) 讨论转移 代码 #include<ve ...
随机推荐
- POJ2488【DFS】
阿西吧,搞清楚谁是行,谁是列啊!!! #include <stdio.h> #include <string.h> #include <math.h> #inclu ...
- poj 1733 Parity game【hash+带权并查集】
hash一下然后用带权并查集做模2下的前缀和 #include<iostream> #include<cstdio> #include<map> #include& ...
- pyrcharm 编程规范
正常变量赋值, 等号左右各一个空格: 参数赋值, 等号左右都没有空格: 注释#后面一个空格 类定义和函数定义,前后各两行,而在类的里面定义成员函数,只需要空一行 文件最后一个空行 变量.函数.类最好都 ...
- springboot修改项目不需要重启服务器
一.spring-boot-devtools 在pom中直接引入依赖 <dependency> <groupId>org.springframework.boot ...
- HDU6438(贪心技巧)
第一眼喜闻乐见的股票问题dp可以暴力,然鹅时间不允许. 于是考虑怎么贪. 这篇题解说得很生动了. 因为每支股票都有买入的潜力所以肯定都加在优先队列里. 然后考虑的是哪些需要加入两次.这是我第二次见到类 ...
- Linux文件和目录的777、755、644权限解释
Linux文件和目录的权限 1.文件权限 在linux系统中,文件或目录的权限可以分为3种: r:4 读 w:2 写 x:1 执行(运行)-:对应数值0 数字 4 .2 和 1表示读.写.执行权限 ...
- luogu P3371 & P4779 单源最短路径spfa & 最大堆优化Dijkstra算法
P3371 [模板]单源最短路径(弱化版) 题目背景 本题测试数据为随机数据,在考试中可能会出现构造数据让SPFA不通过,如有需要请移步 P4779. 题目描述 如题,给出一个有向图,请输出从某一点出 ...
- 使用PreparedStatement接口
- C. Memory and De-Evolution 逆向思维
http://codeforces.com/contest/712/problem/C 要使得把三角形由边长n变成m,等价于由m变成n 如果是从n变成m,则很难判断每次判断变成多少.比如22的变成4, ...
- C. Alyona and mex
http://codeforces.com/contest/740/problem/C 构造思维题. 第一直觉就是区间长度+1的最小值就是答案. 然而不知道怎么去构造这个序列. 其实就是每个区间,都要 ...