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) ...
随机推荐
- commitlint那些事儿
这里主要介绍提交信息用到的 cz 工具集. 一.生成器 commitizen,cz`生成提交说明`,格式化 git commit message. # 全局安装cz npm install -g co ...
- 超级简单,把PuppyLinux安装到U盘
先说说使用感受:上网全是乱码!不支持中文 下载最新版puppylinux,从官网下载 现在U盘引导程序制作工具Unetbootin 打开下载的UNetbootin,进行下面的操作: 制作完毕后,修改U ...
- 在 Java 中不使用多余变量交换两个字符串
在 Java 中不使用多余变量交换两个字符串 public class Test { public static void main(String[] args) { String a = " ...
- [转帖]TimesTen与Redis的对比
TimesTen与Redis的对比 2017-02-23 17:25:27 dingdingfish 阅读数 3682更多 分类专栏: TimesTen Oracle Redis In-Memory ...
- 如何使用RedisTemplate访问Redis数据结构之Zset
Redis的ZSet数据结构 Redis 有序集合和无序集合一样也是string类型元素的集合,且不允许重复的成员. 不同的是每个元素都会关联一个double类型的分数.redis正是通过分数来为集合 ...
- Stack实现
栈的三种操作算法很简单 STACK-EMPTY(S) 1 if S.top == 0 2 return TRUE 3 else return FALSE PUSH(S, x) 1 S.top = ...
- 创建maven父项目以及子项目
创建maven父项目以及子项目(Eclipse创建Maven Project跟Maven Module)https://blog.csdn.net/Mrsanger/article/details/8 ...
- AOP & 拦截器
https://www.cnblogs.com/boywwj/p/7502185.html spring aop中@after-returning和@after,@afterThrowing,@Aro ...
- Entity framework Core 数据库迁移
本文转自https://www.cnblogs.com/zmaiwxl/p/9454177.html 初始化数据库 1.添加初始迁移 Add-Migration init 向“迁移”目录下的项目添加以 ...
- 2.NET Core设定数据库种子
1.使用以下代码在 Models 文件夹中创建一个名为 SeedData 的新类: using Microsoft.EntityFrameworkCore;using Microsoft.Extens ...