题目描述:

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的更多相关文章

  1. CodeForces 474F Ant colony ST+二分

    Ant colony 题解: 因为一个数是合法数,那么询问区间内的其他数都要是这个数的倍数,也就是这个区间内的gcd刚好是这个数. 对于这个区间的gcd来说,不能通过前后缀来算. 所以通过ST表来询问 ...

  2. Codeforces 474F - Ant colony

    注意到每个区间生存下来的蚂蚁的长度等于区间的gcd 于是可以先预处理出区间的gcd 然后二分查找就好了 预处理gcd我这里用的是倍增法 总的时间复杂度O(NlogN) /* Cf 271F 倍增求区间 ...

  3. Codeforces 474 F. Ant colony

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

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

  5. [BZOJ3872][Poi2014]Ant colony

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

  6. 【BZOJ3872】Ant colony(二分,动态规划)

    [BZOJ3872]Ant colony(二分,动态规划) 题面 又是权限题... Description There is an entrance to the ant hill in every ...

  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. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

随机推荐

  1. 双写mq后碰到没有消费问题记录

    上周双写mq后碰到遇到个问题,mq双写的一台机器有produce,另一台一直没有,但是有的那台机器没有消费者,导致另一个服务 一直没有可以消费的mq.原因是 mq在双写初始化配置的时候两个类文件重复了 ...

  2. C++ & OpenCV 零散学习总结

    OpenCV中Mat基本用法: Mat类 (Matrix的缩写) 是OpenCV用于处理图像而引入的一个封装类.从功能上讲,Mat类在IplImage结构的基础上进一步增强,并且,由于引入C++高级编 ...

  3. linux安装jira

    JIRA配置本地MYSQL数据库 https://blog.csdn.net/coin_one/article/details/78376238 jira7.3.6 linux安装及破解 https: ...

  4. [LeetCode] 734. Sentence Similarity 句子相似度

    Given two sentences words1, words2 (each represented as an array of strings), and a list of similar ...

  5. (CSDN迁移) html中的rel属性

    在页面上若需要同时存在多个页面,就通过一个button开启一个页面时,就需要指定不同的 rel="relName". 否则新页面就会将就原有的页面给覆盖掉.

  6. adb 常用命令汇总

    adb 常用命令: adb –help 查看帮助手册 adb devices 检测连接到电脑的安卓设备或安卓模拟器设备 adb pull  <手机路径>  <本机路径>  从手 ...

  7. MySQL [Err] 1055--1064 - Expression #1 of ORDER BY clause is not in GROUP BY clause

    1055错误: 方案1: 修改sql_mode的值 set sql_mode = '';set sql_mode = 'NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABL ...

  8. Go MongoDB官方数据库驱动之增删改查

    package main import ( "context" "fmt" "log" "go.mongodb.org/mongo ...

  9. javascript document.createElement() document.createTextNode() appendChild()

    //--------------document.createElement("div") var div = document.createElement("div&q ...

  10. Java-手动搭建SSH(maven版)

    创建maven项目 把maven项目变为动态网站,步骤如下: 项目结构图如下: 开始搭建spring+springmvc+Hibernate项目 环境版本就不多说了,直接贴出pom.xml文件 < ...