ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces G. Ant colony

2019-07-27 21:01:07  阅读:474  来源: 互联网

标签:ant gcd int colony Codeforces ants Ant battle Mole


题目描述:

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 }

 

标签:ant,gcd,int,colony,Codeforces,ants,Ant,battle,Mole
来源: https://www.cnblogs.com/zhanhonhao/p/11256670.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有