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 ...
随机推荐
- Unity 三维软件单位导入资源单位比例
三维软件 内部米制尺寸/m 默认设置导入unity中的尺寸/m 与unity单位比例 Maya 1 100 1:100 3DS MAX 1 0.01 100:1 Cinema 4D 1 100 1:1 ...
- 使用面向对象技术创建高级 Web 应用程序
作者: 出处: 使用面向对象技术创建高级 Web 应用程序 来源:开源中国社区 作者:oschina 最近,我面试了一位具有5年Web应用开发经验的软件开发人员.她有4年半的JavaScript编程经 ...
- js倒计时demo 天/时/分/秒
<html><head> <meta charset="UTF-8"> <title>js简单时分秒倒计时</title> ...
- 20121124.Nodejs创建HTTP程序.md
####1.源代码: var http=require('http');//读取http模块 http.createServer(function(req,res){//创建一个服务,接受一个回 ...
- Virtual Box 新建一个虚拟机安装系统(补充:WIN7 64 bit 系统虚拟机无法安装 64 bit 系统问题)
1.安装Virtual Box好后,点击新建 2.配置内存大小,这个根据自己需要配置就好 3.创建虚拟硬盘 这里选择固定分配.动态分配都可以,接下来就分配硬盘大小了 4.新建好后我们点击刚才建立的虚拟 ...
- mysql5.7官网直译SQL语句优化--分组优化
1.14Group By Optimization 分组优化 大多数方法为了满足分组查询需要扫描整个表并且创建一个临时表,其中每组中的值都是连续的,如果可以使用聚合函数和临时表获取各个分组.在某些情况 ...
- ASP.NET-常用插件集合
001.输入表单验证插件FluentValidation ( 这个组建可以直接验证实体类,达到和验证model相同的效果,如果类很少可以直接使用这个省去model) https://github.co ...
- HDU 2475 Box 树型转线型 + 伸展树
树型转线型.第一次听说这个概念. . . , 可是曾经已经接触过了,如LCA的预处理部分和树链剖分等.可是没想到还能这么用,三者虽说有不同可是大体思想还是非常相近的,学习了. 推荐博客http://b ...
- 使用malloc分别分配2KB的空间,然后用realloc调整为6KB的内存空间,打印指针地址
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<malloc.h> i ...
- JAVA设计模式之【职责链模式】
职责链模式 专门处理请求链式传递的模式 角色 Handler抽象处理者 ConcreteHandler具体处理者 在职责链模式中,很多对象由每一个对象对其下家的引用而连接成一条链,请求在这条链上传递, ...