Codeforces G. Ant colony
题目描述:
F. Ant colony
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Mole is hungry again. He found one ant colony, consisting of n ants, ordered in a row. Each ant i (1 ≤ i ≤ n) has a strength si.
In order to make his dinner more interesting, Mole organizes a version of «Hunger Games» for the ants. He chooses two numbers l and r(1 ≤ l ≤ r ≤ n) and each pair of ants with indices between l and r (inclusively) will fight. When two ants i and j fight, ant i gets one battle point only if si divides sj (also, ant j gets one battle point only if sj divides si).
After all fights have been finished, Mole makes the ranking. An ant i, with vi battle points obtained, is going to be freed only if vi = r - l, or in other words only if it took a point in every fight it participated. After that, Mole eats the rest of the ants. Note that there can be many ants freed or even none.
In order to choose the best sequence, Mole gives you t segments [li, ri] and asks for each of them how many ants is he going to eat if those ants fight.
Input
The first line contains one integer n (1 ≤ n ≤ 105), the size of the ant colony.
The second line contains n integers s1, s2, ..., sn (1 ≤ si ≤ 109), the strengths of the ants.
The third line contains one integer t (1 ≤ t ≤ 105), the number of test cases.
Each of the next t lines contains two integers li and ri (1 ≤ li ≤ ri ≤ n), describing one query.
Output
Print to the standard output t lines. The i-th line contains number of ants that Mole eats from the segment [li, ri].
Sample test(s)
input
5
1 3 2 4 2
4
1 5
2 5
3 5
4 5
output
4
4
1
1
Note
In the first test battle points for each ant are v = [4, 0, 2, 0, 2], so ant number 1 is freed. Mole eats the ants 2, 3, 4, 5.
In the second test case battle points are v = [0, 2, 0, 2], so no ant is freed and all of them are eaten by Mole.
In the third test case battle points are v = [2, 0, 2], so ants number 3 and 5 are freed. Mole eats only the ant 4.
In the fourth test case battle points are v = [0, 1], so ant number 5 is freed. Mole eats the ant 4.
思路:
题目的意思是说,给一个数列,看里面有多少个数,这样的数可以被数列中的其他所有数整除。显然这个数就是数列的gcd啦!为什么呢?首先gcd可以满足条件,然后如果不是gcd,那就是gcd的因数,可是数列中的数如果有一个是gcd的因数那它小于等于gcd,而它又不可能比gcd小,只能相等。(为什么,它要是比gcd小,那它才会是gcd)。我怎么会有这么奇怪的想法怀疑它不是gcd (キ`゚Д゚´)!!
又因为是区间查询问题,整一个线段树来维护区间gcd,和等于gcd的数目。注意的是build函数里这么pushup,还有查询函数怎么统计结果。
pushup就是对一个节点求左右两个节点的gcd,如果左边的节点的gcd与这个gcd相等,统计数目加左边的相等数目,如果右边的等就再加右边的数目,不等就是零。
query函数求答案的时候要看一下当前区间答案来自哪里,是左区间,还是右区间,还是两边都有?分别处理一下就好。
这道题竟然连懒标记都没用,就是静态查询√
代码:
#include <iostream>
#define max_n 100005
using namespace std;
int n;
int t;
struct node
{
int num;
int gcd;
int id;
}tree[max_n<<];
int a[max_n]; int GCD(int a,int b)
{
if(a<b) swap(a,b);
int r = a%b;
if(r==)
{
return b;
}
return GCD(b,r);
}
void build(int id,int l,int r)
{
if(l==r)
{
tree[id].gcd = a[l];
tree[id].num = ;
return;
}
int mid = (l+r)>>;
build(id<<,l,mid);
build(id<<|,mid+,r);
int gcd = GCD(tree[id<<].gcd,tree[id<<|].gcd);
tree[id].num = ;
tree[id].gcd = gcd;
if(tree[id<<].gcd==gcd)
{
tree[id].num += tree[id<<].num;
}
if(tree[id<<|].gcd==gcd)
{
tree[id].num += tree[id<<|].num;
}
}
pair<int,int> query(int id,int L,int R,int l,int r)
{
//cout << "l " << l << " r " << r << endl;
if(L<=l&&r<=R)
{
int gcd = tree[id].gcd;
int num = tree[id].num;
//cout << "gcd " << gcd << "num " << num << endl;
return pair<int,int>(gcd,num);
}
int mid = (l+r)>>;
int ans = ;
pair<int,int> res1,res2;
if(L<=mid){ res1 = query(id<<,L,R,l,mid); }
if(mid<R) {res2 = query(id<<|,L,R,mid+,r);}
int gcd;
if(L<=mid)
{
if(mid<R)
{
gcd = GCD(res1.first,res2.first);
//cout << "gcd " << gcd << endl;
if(res1.first==gcd)
ans+=res1.second;
if(res2.first==gcd)
ans+=res2.second;
}
else
{
gcd = res1.first;
ans += res1.second;
}
}
else
{
gcd = res2.first;
ans += res2.second;
}
//cout << "gcd " << gcd << " ans " << ans << endl;
return pair<int,int>(gcd,ans);
}
int main()
{
//cout << GCD(1,3) << endl;
cin >> n;
for(int i = ;i<=n;i++)
{
cin >> a[i];
}
build(,,n);
cin >> t;
for(int q = ;q<t;q++)
{
int L,R;
cin >> L >> R;
cout << R-L+-query(,L,R,,n).second << endl;;
}
return ;
}
Codeforces G. Ant colony的更多相关文章
- CodeForces 474F Ant colony ST+二分
Ant colony 题解: 因为一个数是合法数,那么询问区间内的其他数都要是这个数的倍数,也就是这个区间内的gcd刚好是这个数. 对于这个区间的gcd来说,不能通过前后缀来算. 所以通过ST表来询问 ...
- Codeforces 474F - Ant colony
注意到每个区间生存下来的蚂蚁的长度等于区间的gcd 于是可以先预处理出区间的gcd 然后二分查找就好了 预处理gcd我这里用的是倍增法 总的时间复杂度O(NlogN) /* Cf 271F 倍增求区间 ...
- 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][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...
- 【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 704B - Ant Man 贪心
codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...
随机推荐
- K8S使用入门-创建第一个容器
前面两个教程我们已经使用kubekit将K8S搭建起来了.但是,没有将实际使用中需要在K8S上部署我们的容器创建起来的教程,都是耍流氓.所以,经过几番折腾,我回来给自己洗白了.之前一直卡在创建第一个容 ...
- Activiti Service介绍
原文地址:https://www.cnblogs.com/lyh421/p/6419518.html 第一章 认识Activiti 内容概览:讲解activiti的特点.接口概览.架构等基本信息. 1 ...
- mysql全量+增量备份脚本
cat xtrabackup_mysql.sh #!/bin/bash #title :xtrabackup_mysql.sh #description :backup mysql by using ...
- CSS选择符、伪类、层叠
主题,架子(时间架子,空间架子,三角架),素材. CSS 三种方式 行内样式 嵌入样式 链接样式 上下文选择符 祖父 孙 p em {color:red;} 父 子 p > em {color: ...
- 如何申请高德地图用户Key
打开网页https://lbs.amap.com/,进入高德开发平台. 单击箭头处[注册],打开注册页面.(如果您已注册为高德地图开发者可跳过此步骤,直接登录即可). 选择[成为个人开发者],如果您是 ...
- html 打开新页面
设置 target 页面 这样会点击一次就产生一个页面 页面 填任意名称,多个点击只产生于一个页面
- python 递归\for循环_斐波那契数列
# 递归 def myAdd(a, b): c = a + b print(c) if c > 100: return return myAdd(a + 1, c) #最大递归深度是1000 m ...
- react-navigation 的抽屉效果 createDrawerNavigator (DrawerNavigator)
一.前言: react-navigation 3.x 版本中, 使用createDrawerNavigator 替换 原先的DrawerNavigator 方法: 那么,当前createBottom ...
- Mybatis中使用association进行关联的几种方式
这里以一对一单向关联为例.对使用或不使用association的配置进行举例. 实体类: @Data @ToString @NoArgsConstructor public class IdCard ...
- Django:信号的使用
信号 Django中提供了"信号调度",用于在框架执行操作时解耦.通俗来讲,就是一些动作发生的时候,信号允许特定的发送者去提醒一些接受者. 应用:比如插入数据到数据库,插入之前写日 ...