-
Notifications
You must be signed in to change notification settings - Fork 62
/
PRMQ.cpp
77 lines (65 loc) · 1.35 KB
/
PRMQ.cpp
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
// Code written by Monal
#include "bits/stdc++.h"
using namespace std;
#define MAXN 1000001
long int spf[MAXN];
void sieve() {
spf[1] = 1;
for (long int i = 2; i < MAXN; ++i)
spf[i] = i;
for (long int i=4; i<MAXN; i+=2)
spf[i] = 2;
for (long int i = 3; i * i < MAXN; ++i) {
if (spf[i] == i) {
for (long int j = i * i; j < MAXN; j += i)
if (spf[j]==j)
spf[j] = i;
}
}
}
vector<long int> get_factorization(long int num) {
vector<long int > ret;
while (num != 1) {
ret.push_back(spf[num]);
num = num / spf[num];
}
return ret;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
sieve();
long int n;
cin>>n;
long int arr[n];
for (int i = 0; i < n; ++i)
cin>>arr[i];
//cout<<"done"<<endl;
/*for (int i = 0; i < n; ++i) {
cout<<arr[i]<<" ";
}
cout<<endl;*/
long int q;
cin>>q;
while (q--) {
long int l,r,x,y;
cin>>l>>r>>x>>y;
//cout<<l<<" "<<r<<" "<<x<<" "<<y<<" "<<endl;
unsigned long long answer = 0;
unsigned long long int tmp;
//cout<<"yaha"<<endl;
for (long int j = r-1; j >= l-1; --j) {
//cout<<"yaha"<<endl;
vector < long int > tmp = get_factorization(arr[j]);
//for (int i = 0; i < tmp.size(); ++i)
// cout<<tmp[i]<<" ";
//cout<<endl;
for (long int i = x; i <= y; ++i) {
answer += count(tmp.begin(), tmp.end(), i);
}
tmp.clear();
}
cout<<answer<<"\n";
}
return 0;
}