[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 ...
随机推荐
- Codeforces - 773A - Success Rate - 二分 - 简单数论
https://codeforces.com/problemset/problem/773/A 一开始二分枚举d,使得(x+d)/(y+d)>=p/q&&x/(y+d)<= ...
- git 命令参考手册
你的本地仓库由 git 维护的三棵“树”组成.第一个是你的 工作目录,它持有实际文件:第二个是 缓存区(Index),它像个缓存区域,临时保存你的改动:最后是 HEAD,指向你最近一次提交后的结果. ...
- springcloud(一) 服务拆分
一般我们的项目如果需要从单应用服务升级到微服务,必须要将原来的服务做拆分,我这边的拆分也是基于将之前spb-demo的springboot单应用做拆分,拆分出三个应用,spb-brian-query- ...
- LuoguP3964 [TJOI2013]松鼠聚会【切比雪夫距离/前缀和】
题目传送门 前置知识:切比雪夫距离和曼哈顿距离的相互转化--自为风月马前卒 有了这个知识,我们便可以在读入松鼠的家的坐标时,先把他转化一下,然后把最后的总式化简,我们会得到一个充满后缀和以及前缀和的式 ...
- Hexo瞎折腾系列(4) - 站点首页不显示文章全文
文章摘要设置 打开主题配置文件 _config.yml 文件,找到如下: # Automatically Excerpt. Not recommend. # Please use <!-- mo ...
- IDEA Maven Hadoop调试hdfs程序
IDEA 远程调试 Hadoop 两大特色:一是采用maven的pom配置:二是直接连接hdfs:9000端口,无须另外在服务端配置参数. 其实内容包含了两种方式:本地与远程调试.这里仅仅只是使用远程 ...
- Magic Numbers CodeForces - 628D
Magic Numbers CodeForces - 628D dp函数中:pos表示当前处理到从前向后的第i位(从1开始编号),remain表示处理到当前位为止共产生了除以m的余数remain. 不 ...
- 502 IPO 上市
详见:https://leetcode.com/problems/ipo/description/ C++: class Solution { public: int findMaximizedCap ...
- 上交oj1219 重要的逆序数对
题意: https://acm.sjtu.edu.cn/OnlineJudge/problem/1219 思路: 在经典的归并排序求逆序数对算法基础上稍作修改. 实现: #include <io ...
- IOS问题
#import "EXFifthViewController.h" @interface EXFifthViewController () @end @implementation ...