CF474F Ant colony
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=;
int a[N],n,m,tmp;
struct tree{
int l,r,gcd,sum;
}tr[N*];
int gcd(int x,int y){
if(y==)return x;
else return gcd(y,x%y);
}
void update(int now){
int GCD=gcd(tr[now*].gcd,tr[now*+].gcd);
tr[now].gcd=GCD;
if(tr[now*].gcd==tr[now*+].gcd)tr[now].sum=tr[now*].sum+tr[now*+].sum;
else if(tr[now*].gcd==GCD)tr[now].sum=tr[now*].sum;
else if(tr[now*+].gcd==GCD)tr[now].sum=tr[now*+].sum;
else tr[now].sum=;
}
void build(int l,int r,int now){
tr[now].l=l;tr[now].r=r;
if(l==r){
tr[now].gcd=a[l];
tr[now].sum=;
return;
}
int mid=(l+r)>>;
build(l,mid,now*);
build(mid+,r,now*+);
update(now);
}
int getgcd(int l,int r,int now){
if(tr[now].l==l&&tr[now].r==r){
return tr[now].gcd;
}
int mid=(tr[now].l+tr[now].r)>>;
if(l>mid)return getgcd(l,r,now*+);
else if(r<=mid)return getgcd(l,r,now*);
else {
return gcd(getgcd(l,mid,now*),getgcd(mid+,r,now*+));
}
}
int check(int l,int r,int now){
if(tr[now].l==l&&tr[now].r==r){
if(tr[now].gcd==tmp)return tr[now].sum;
else return ;
}
int mid=(tr[now].l+tr[now].r)>>;
if(l>mid)return check(l,r,now*+);
else if(r<=mid)return check(l,r,now*);
else {
return check(l,mid,now*)+check(mid+,r,now*+);
}
}
int main(){
while(~scanf("%d",&n)){
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
build(,n,);
scanf("%d",&m);
for(int i=,l,r;i<=m;i++){
scanf("%d%d",&l,&r);
tmp=getgcd(l,r,);
printf("%d\n",r-l+-check(l,r,));
}
}
return ;
}
题意:
求区间gcd以及这个区间内的数字等于区间gcd的有多少个(其实是不等于,用区间长度减一下就好了)。
数据范围:1 ≤ n ≤ 105
题解
这题用线段树,但一眼望去这题似乎不满足合并,但实际上是可以的。
我们在线段树上维护两个东西,一个是区间gcd一个是区间中等于区间gcd的数的个数记为sum。
考虑如何合并,如果当前区间的两个字区间gcd相等,那么直接把sum相加就好,如果gcd等于两个gcd中的一个,那么sum就是相等gcd对应的那个,如果不相等,那么sum就为0。
为什么呢?gcd一定小于等于比区间所有数的最小值。gcd合并一定会减少或者不变,不变的话就是第一种情况,减少的话,gcd就比区间的所有数都小,所以区间中就不会有数等于gcd了。
CF474F Ant colony的更多相关文章
- [BZOJ3872][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...
- 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】Ant colony(二分,动态规划)
[BZOJ3872]Ant colony(二分,动态规划) 题面 又是权限题... Description There is an entrance to the ant hill in every ...
- 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 474F Ant colony ST+二分
Ant colony 题解: 因为一个数是合法数,那么询问区间内的其他数都要是这个数的倍数,也就是这个区间内的gcd刚好是这个数. 对于这个区间的gcd来说,不能通过前后缀来算. 所以通过ST表来询问 ...
- Codeforces G. Ant colony
题目描述: F. Ant colonytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputo ...
- BZOJ 3872 Ant colony
Description There is an entrance to the ant hill in every chamber with only one corridor leading int ...
随机推荐
- ZBrush中如何把模型的细节映射到低模上
我们在ZBrush®雕刻模型的时候,发现模型布线不利于雕刻,这使我们不得不对模型进行重建细分,而重建细分之后的模型细节已经没有了,这个时候我们就需要把原来高模的细节映射到新的模型上面. 接下来我们介绍 ...
- Python3.7中的常用关键字
本文是在学习Python中遇到的一些关键字,作为日常总结的笔记. Python中有保留字/关键字 保留字就是在Python中预先保留的标识符,这些标识符在Python程序中具有特定用途,不能被程序员作 ...
- github删除项目or仓库
1. 登录 github (要注册账号) 2. 登录后点击右上侧头像,选择 Your profile . 3. 选择Repositories,可以查看已有的库,选择要删除的库进入. 4. 选择Sett ...
- freeswitch 编码协商
编辑 /usr/local/freeswitch/conf/sip_profiles/internal.xml 添加注释 <param name="inbound-zrtp-p ...
- [读书笔记] Python 数据分析 (八)画图和数据可视化
ipython3 --pyplot pyplot: matplotlib 画图的交互使用环境
- debian 9 安装Virtual Box
1.去官网下载deb包,例如包名: virtualbox-.2_5.2.18-124319_Debian_stretch_amd64.deb 2.安装 .2_5.2.18-124319_Debian_ ...
- Map的四种遍历方法
1.取值遍历 for(String key:map.keySet()){ System.out.println("key="+key+"and value=" ...
- VUE:列表的过滤与排序
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- reac实现控制输入框字符长度
reac实现控制输入框字符长度 代码思路:
- 【codeforces 190C】STL
[题目链接]:http://codeforces.com/problemset/problem/190/C [题意] 让你根据去掉标点符号的; pair 以及 int这两种类型; 确定出一种类型; 使 ...