-
Notifications
You must be signed in to change notification settings - Fork 0
/
AntColony.cc
86 lines (82 loc) · 2.21 KB
/
AntColony.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef vector<ll> vi;
typedef pair<ll,ll> ii;
#define pb push_back
#define fst first
#define snd second
#define ALL(cont) cont.begin(), cont.end()
#define mset(a,b) memset(a,b,sizeof(a));
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define fore(i, a, b) for (int i = a, almo5t = b; i < almo5t; ++i)
#define SZ(x) ((int)x.size())
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define FIN ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
auto rnd=bind(uniform_int_distribution<long long>(0,1), mt19937(time(0)));
ll GCD(ll a, ll b){
if(!a) return b;
if(!b) return a;
return __gcd(a,b);
}
#define oper GCD
#define NEUT 0LL
struct STree { // segment tree for min over llegers
vector<ll> st;ll n;
STree(ll n): st(4*n+5,NEUT), n(n) {}
void init(ll k, ll s, ll e, ll *a){
if(s+1==e){st[k]=a[s];return;}
ll m=(s+e)/2;
init(2*k,s,m,a);init(2*k+1,m,e,a);
st[k]=oper(st[2*k],st[2*k+1]);
}
void upd(ll k, ll s, ll e, ll p, ll v){
if(s+1==e){st[k]=v;return;}
ll m=(s+e)/2;
if(p<m)upd(2*k,s,m,p,v);
else upd(2*k+1,m,e,p,v);
st[k]=oper(st[2*k],st[2*k+1]);
}
ll query(ll k, ll s, ll e, ll a, ll b){
if(s>=b||e<=a)return NEUT;
if(s>=a&&e<=b)return st[k];
ll m=(s+e)/2;
return oper(query(2*k,s,m,a,b),query(2*k+1,m,e,a,b));
}
void init(ll *a){init(1,0,n,a);}
void upd(ll p, ll v){upd(1,0,n,p,v);}
ll query(ll a, ll b){return query(1,0,n,a,b);}
}; // usage: STree rmq(n);rmq.init(x);rmq.upd(i,v);rmq.query(s,e);
int main()
{
FIN;
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#else
#define endl '\n'
#endif
int N;cin>>N;
ll a[N];
ll gcd = 1;
vector<ii> pares;
fore(i,0,N){
cin>>a[i];
pares.pb({a[i],i});
}
sort(ALL(pares));
int t;cin>>t;
STree rmq(N);rmq.init(a);
while(t--){
ll l, r;cin>>l>>r;l--;
gcd = rmq.query(l,r);
r--;
ll i1 = lower_bound(ALL(pares),make_pair(gcd,l))-pares.begin();
ll i2 = lower_bound(ALL(pares),make_pair(gcd,r+1))-pares.begin();
int salvadas = i2-i1;
cout<<(r-l+1-salvadas)<<endl;
}
return 0;
}