区间重复不会影响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的更多相关文章

  1. Codeforces 474 F. Ant colony

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

  2. CF #271 F Ant colony 树

    题目链接:http://codeforces.com/contest/474/problem/F 一个数组,每一次询问一个区间中有多少个数字可以整除其他所有区间内的数字. 能够整除其他所有数字的数一定 ...

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

  4. Codeforces Round #271 (Div. 2) F. Ant colony (RMQ or 线段树)

    题目链接:http://codeforces.com/contest/474/problem/F 题意简而言之就是问你区间l到r之间有多少个数能整除区间内除了这个数的其他的数,然后区间长度减去数的个数 ...

  5. Codeforces G. Ant colony

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

  6. [BZOJ3872][Poi2014]Ant colony

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

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

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

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

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

  9. CF 633 F. The Chocolate Spree 树形dp

    题目链接 CF 633 F. The Chocolate Spree 题解 维护子数答案 子数直径 子数最远点 单子数最长直径 (最长的 最远点+一条链) 讨论转移 代码 #include<ve ...

随机推荐

  1. Codeforces - 702A - Maximum Increase - 简单dp

    DP的学习计划,刷 https://codeforces.com/problemset?order=BY_RATING_ASC&tags=dp 遇到了这道题 https://codeforce ...

  2. Tasks 多核查找最大最小值问题

    先贴下代码 _Datas.ParallelForEach(arg_nDataStartIndex, arg_nDataCount, (data) => { dMax = dMax.Max(dat ...

  3. 洛谷P2564 [SCOI2009]生日礼物(单调队列)

    传送门 准确的来说这个应该叫尺取法? 先对所有的点按$x$坐标进行排序 我们维护两个指针$l,r$,每一次令$r$不断右移直到所有颜色齐全,再不断右移$l$直到颜色数不足,那么此时$[l-1,r]$这 ...

  4. Springboot配置类

    配置类 MyAppConfig  import com.test.springboot.service.HelloService; import org.springframework.context ...

  5. spring @InitBinder

    /** * 将字符串日期转化为Date类型 * @param binder */ @InitBinder protected void initBinder(WebDataBinder binder) ...

  6. bzoj1854 [Scoi2010]游戏【构图 并查集】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1854 没想到怎么做真是不应该,看到每个武器都有两个属性,应该要想到连边构图的!太不应该了! ...

  7. HDU6446(树上、排列的贡献计算)

    关键点在于:全排列中,任意两点u.v相邻的次数一定是(n - 1)! * 2次,即一个常数(可以由高中数学知识计算,将这两个点捏一起然后全排列然后乘二:或者用n! / C(2, n)). 这之后就好算 ...

  8. 题解报告:hdu 4135 Co-prime(容斥定理入门)

    Problem Description Given a number N, you are asked to count the number of integers between A and B ...

  9. Myisamchk使用

    Myisam损坏的情况: . 服务器突然断电导致数据文件损坏;强制关机,没有先关闭mysql 服务;mysqld 进程在写表时被杀掉.因为此时mysql可能正在刷新索引. . 磁盘损坏. . 服务器死 ...

  10. innobackupex的使用

    优点: 不暂停服务器创建Innodb热备份 为mysql做增量的备份 在mysql服务器之间做在线表迁移 使创建mysql replication更加容易 备份mysql但不增加服务器的负载 安装:x ...