-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryIndexedTree.cpp
More file actions
79 lines (68 loc) · 1.5 KB
/
binaryIndexedTree.cpp
File metadata and controls
79 lines (68 loc) · 1.5 KB
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
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
const int MAXN = 1e6 + 5;
const int MAXV = 5e5;
const ll MOD = 1e9 + 7;
ll BIT[MAXN], a[MAXN], n,q;
ll phi[MAXV + 5], p[MAXV + 5];
ll inline mod(ll x) {
return (x%MOD + MOD)%MOD;
}
void compute_phi() {
for(int i = 1; i <= MAXV; i++) phi[i] = i;
for(int i = 2; i <= MAXV; i++)
if (phi[i] == i) {
for(int j = i; j <= MAXV; j += i) {
phi[j] -= phi[j] / i;
phi[j] = mod(phi[j]);
}
}
}
void compute_pillai() {
for(int i = 1; i <= MAXV; i++)
for(int j = i; j <= MAXV; j += i) {
p[j] += i * phi[j / i];
p[j] = mod(p[j]);
}
}
void update(int x, ll val){
for(; x <= n; x += x&-x){
BIT[x] += val;
BIT[x] = mod(BIT[x]);
}
}
ll query(int x){
ll sum = 0;
for(; x > 0; x -= x&-x){
sum += BIT[x];
sum=mod(sum);
}
return sum;
}
int main(){
compute_phi();
compute_pillai();
memset(BIT, 0, sizeof(BIT));
scanf("%lld",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i]);
update(i,p[a[i]]);
}
scanf("%lld",&q);
while(q--){
getchar();
char c;
int u,v;
scanf("%c %d %d",&c,&u,&v);
if(c=='C'){
ll ans = mod(query(v)-query(u)+p[a[u]]);
printf("%lld\n",ans);
}
else if(c=='U'){
update(u,mod(p[v]-p[a[u]]));
a[u]=v;
}
}
return 0;
}