[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 ...
随机推荐
- HDU 4542 小明系列故事——未知剩余系 (数论|反素数)
分析 kuangbin的blog已经讲的很好了,我做一点补充 1.当做x*y>z的比较时,如果x \(\ast\) y过大,可以写成x>z/y 2.分解质因数时选择用f[][0]保存质数, ...
- 纯JS阻止浏览器默认滚动事件,实现自定义滚动方法
首先该方法兼容IE7+以上浏览器,可以实现页面上下滚动,而且也可以实现页面左右滚动,每次滚动的距离为屏幕的大小,滚动为加速滚动 javaScript代码如下: //滚动实现方法,使用鼠标滚轮每次滚动浏 ...
- Centos 6.8安装 SVN
SVN SVN是Subversion的简称,是一个开放源代码的版本控制系统,相较于RCS.CVS,它采用了分支管理系统,它的设计目标就是取代CVS.互联网上很多版本控制服务已从CVS迁移到Subver ...
- 将tomcat添加到系统服务
一.安装服务 执行“service.bat install 二.卸载服务 在命令行中进入/Tomcat路径/bin/,执行“service.bat remove”
- [APIO2012]派遣 洛谷P1552 bzoj2809 codevs1763
http://www.codevs.cn/problem/1763/ https://www.lydsy.com/JudgeOnline/problem.php?id=2809 https://www ...
- 二分图匹配 + 构造 E. Arpa’s overnight party and Mehrdad’s silent entering
http://codeforces.com/contest/742/problem/E 跪着看题解后才会的. 对于任何一对BF[i]和GF[i] 连接了一条边后,那么他们和隔壁都是不会有边相连的了,这 ...
- postgresql版sde(10.4.1)新建数据库
使用sde pg版,第一次建sde库就是新安装sde的方法 然而从第二次开始可以有其他方式 新建一般的pg数据再转为sde数据库: 首先用建一般的pg数据库 首先要新建数据库 数据库名:新建的数据库名 ...
- 009全志R16平台tinav3.0下编译不过的问题
009全志R16平台tinav3.0下编译不过的问题 2018/11/13 11:39 版本:V1.0 开发板:SC3817R SDK:tina v3.0 1.01原始编译全志r16平台tinav3. ...
- Android CursorAdapter的使用
CursorAdapter继承于BaseAdapter,为Cursor和ListView连接提供了桥梁. 首先看一下CursorAdapter的部分源码: /** * @see android.wid ...
- Android利用融云做异地登录提醒
在RongCloudEvent下找到onChanged方法 @Override public void onChanged(ConnectionStatus connectionStatus) { s ...