Skip to content

Commit 65ab58f

Browse files
Merge pull request #246 from H4wk-3yE/patch-2
Create GCD_sum.cpp
2 parents 58789c1 + 17ec0f1 commit 65ab58f

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

fenwick_tree/GCD_sum.cpp

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
C++
3+
encoding: UTF-8
4+
Modified: <01/Oct/2019 03:02:07 PM>
5+
6+
✪ H4WK3yE乡
7+
Mohd. Farhan Tahir
8+
Indian Institute Of Information Technology (IIIT), Gwalior
9+
*/
10+
11+
#include <bits/stdc++.h>
12+
#ifdef LOCAL_PROJECT
13+
# include <prettypr.hpp>
14+
#endif
15+
16+
using namespace std;
17+
18+
// clang-format off
19+
20+
#define ll long long
21+
#define ve vector
22+
#define pb push_back
23+
#define endl "\n"
24+
#define ff first
25+
#define ss second
26+
#define pii pair<int, int>
27+
#define len(v) int(v.size())
28+
#define all(v) v.begin(), v.end()
29+
#define reset(a, b) memset(a, b, sizeof(a));
30+
#define fr(i, s, n) for (int i = s ; i < n ; ++i)
31+
#define dfr(i, s, n) for (int i = s ; i > n ; --i)
32+
33+
template < typename T > void pr (const T& t) {
34+
cout << t << "\n";
35+
}
36+
37+
template < typename T, typename U, typename... ARGS >
38+
void pr (const T& t, const U& u, const ARGS&... args) {
39+
cout << t << " ";
40+
pr (u, args...);
41+
}
42+
43+
// clang-format on
44+
#define int long long
45+
46+
const int N = 1e6 + 5;
47+
int arr[N];
48+
int bit[N];
49+
int n;
50+
const int mod = 1e9 + 7;
51+
52+
int phi[N], F[N];
53+
void precompute () {
54+
fr (i, 1, N) {
55+
phi[i] += i;
56+
phi[i] %= mod;
57+
for (int j = 2 * i; j < N; j += i) {
58+
phi[j] -= phi[i];
59+
phi[j] = (phi[j] + mod) % mod;
60+
}
61+
}
62+
fr (i, 1, N) {
63+
for (int j = i; j < N; j += i) {
64+
F[j] += j / i * phi[i];
65+
F[j] = (F[j] + mod) % mod;
66+
// F[j] %= mod;
67+
}
68+
}
69+
}
70+
71+
int query (int x) {
72+
int ans = 0;
73+
for (; x > 0; x -= (x & (-x))) {
74+
ans += bit[x];
75+
ans %= mod;
76+
}
77+
ans = (ans + mod) % mod;
78+
// ans %= mod;
79+
return ans;
80+
}
81+
82+
void update (int x, int val, int previous) {
83+
for (; x <= n; x += (x & (-x))) {
84+
bit[x] -= previous;
85+
bit[x] += val;
86+
bit[x] %= mod;
87+
}
88+
}
89+
90+
signed main () {
91+
ios_base::sync_with_stdio (false), cin.tie (nullptr);
92+
precompute ();
93+
cin >> n;
94+
fr (i, 1, n + 1) cin >> arr[i];
95+
96+
dfr (i, n, 0) {
97+
update (i, F[arr[i]], 0);
98+
}
99+
100+
int tc;
101+
cin >> tc;
102+
while (tc--) {
103+
char t;
104+
int x, y;
105+
cin >> t >> x >> y;
106+
if (t == 'C') {
107+
int b = query (y);
108+
int a = query (x - 1);
109+
pr ((b - a + mod) % mod);
110+
} else {
111+
int p = F[arr[x]];
112+
arr[x] = y;
113+
update (x, F[y], p);
114+
}
115+
}
116+
return 0;
117+
}

0 commit comments

Comments
 (0)