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函数求答案的时候要看一下当前区间答案来自哪里,是左区间,还是右区间,还是两边都有?分别处理一下就好。

这道题竟然连懒标记都没用,就是静态查询√

代码:

  1 #include <iostream>
  2 #define max_n 100005
  3 using namespace std;
  4 int n;
  5 int t;
  6 struct node
  7 {
  8     int num;
  9     int gcd;
 10     int id;
 11 }tree[max_n<<2];
 12 int a[max_n];
 13 
 14 int GCD(int a,int b)
 15 {
 16     if(a<b) swap(a,b);
 17     int r = a%b;
 18     if(r==0)
 19     {
 20         return b;
 21     }
 22     return GCD(b,r);
 23 }
 24 void build(int id,int l,int r)
 25 {
 26     if(l==r)
 27     {
 28         tree[id].gcd = a[l];
 29         tree[id].num = 1;
 30         return;
 31     }
 32     int mid = (l+r)>>1;
 33     build(id<<1,l,mid);
 34     build(id<<1|1,mid+1,r);
 35     int gcd = GCD(tree[id<<1].gcd,tree[id<<1|1].gcd);
 36     tree[id].num = 0;
 37     tree[id].gcd = gcd;
 38     if(tree[id<<1].gcd==gcd)
 39     {
 40         tree[id].num += tree[id<<1].num;
 41     }
 42     if(tree[id<<1|1].gcd==gcd)
 43     {
 44         tree[id].num += tree[id<<1|1].num;
 45     }
 46 }
 47 pair<int,int> query(int id,int L,int R,int l,int r)
 48 {
 49     //cout << "l " << l << " r " << r << endl;
 50     if(L<=l&&r<=R)
 51     {
 52         int gcd = tree[id].gcd;
 53         int num = tree[id].num;
 54         //cout << "gcd " << gcd << "num " << num << endl;
 55         return pair<int,int>(gcd,num);
 56     }
 57     int mid = (l+r)>>1;
 58     int ans = 0;
 59     pair<int,int> res1,res2;
 60     if(L<=mid){ res1 = query(id<<1,L,R,l,mid); }
 61     if(mid<R) {res2 = query(id<<1|1,L,R,mid+1,r);}
 62     int gcd;
 63     if(L<=mid)
 64     {
 65         if(mid<R)
 66         {
 67             gcd = GCD(res1.first,res2.first);
 68             //cout << "gcd " << gcd << endl;
 69             if(res1.first==gcd)
 70                 ans+=res1.second;
 71             if(res2.first==gcd)
 72                 ans+=res2.second;
 73         }
 74         else
 75         {
 76             gcd = res1.first;
 77             ans += res1.second;
 78         }
 79     }
 80     else
 81     {
 82         gcd = res2.first;
 83         ans += res2.second;
 84     }
 85     //cout << "gcd " << gcd << " ans " << ans << endl;
 86     return pair<int,int>(gcd,ans);
 87 }
 88 int main()
 89 {
 90     //cout << GCD(1,3) << endl;
 91     cin >> n;
 92     for(int i = 1;i<=n;i++)
 93     {
 94         cin >> a[i];
 95     }
 96     build(1,1,n);
 97     cin >> t;
 98     for(int q = 0;q<t;q++)
 99     {
100         int L,R;
101         cin >> L >> R;
102         cout << R-L+1-query(1,L,R,1,n).second << endl;;
103     }
104     return 0;
105 }

 

转载于:https://www.cnblogs.com/zhanhonhao/p/11256670.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
电子图书资源服务系统是一款基于 Java Swing 的 C-S 应用,旨在提供电子图书资源一站式服务,可从系统提供的图书资源中直接检索资源并进行下载。.zip优质项目,资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松copy复刻,拿到资料包后可轻松复现出一样的项目。 本人系统开发经验充足,有任何使用问题欢迎随时与我联系,我会及时为你解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(若有),项目具体内容可查看下方的资源详情。 【附带帮助】: 若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步。 【本人专注计算机领域】: 有任何使用问题欢迎随时与我联系,我会及时解答,第一时间为你提供帮助,CSDN博客端可私信,为你解惑,欢迎交流。 【适合场景】: 相关项目设计中,皆可应用在项目开发、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面中 可借鉴此优质项目实现复刻,也可以基于此项目进行扩展来开发出更多功能 【无积分此资源可联系获取】 # 注意 1. 本资源仅用于开源学习和技术交流。不可商用等,一切后果由使用者承担。 2. 部分字体以及插图等来自网络,若是侵权请联系删除。积分/付费仅作为资源整理辛苦费用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值