Codeforces G. Ant colony

題目描述:node

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

思路:ide

題目的意思是說,給一個數列,看裏面有多少個數,這樣的數能夠被數列中的其餘全部數整除。顯然這個數就是數列的gcd啦!爲何呢?首先gcd能夠知足條件,而後若是不是gcd,那就是gcd的因數,但是數列中的數若是有一個是gcd的因數那它小於等於gcd,而它又不可能比gcd小,只能相等。(爲何,它要是比gcd小,那它纔會是gcd)。我怎麼會有這麼奇怪的想法懷疑它不是gcd (キ`゚Д゚´)!!函數

又由於是區間查詢問題,整一個線段樹來維護區間gcd,和等於gcd的數目。注意的是build函數裏這麼pushup,還有查詢函數怎麼統計結果。ui

pushup就是對一個節點求左右兩個節點的gcd,若是左邊的節點的gcd與這個gcd相等,統計數目加左邊的相等數目,若是右邊的等就再加右邊的數目,不等就是零。spa

query函數求答案的時候要看一下當前區間答案來自哪裏,是左區間,仍是右區間,仍是兩邊都有?分別處理一下就好。rest

這道題居然連懶標記都沒用,就是靜態查詢√code

代碼:blog

  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 }
相關文章
相關標籤/搜索