Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- using ll = long long;
- using ld = long double;
- using ull = unsigned long long;
- constexpr bool typetest = 0;
- constexpr int N = 3e5 + 5;
- struct FenwickTree
- {
- ll a[N];
- void Update(int p, ll v)
- {
- for (; p < N; p += p & -p)
- a[p] += v;
- }
- ll Get(int p)
- {
- ll ans(0);
- for (; p > 0; p -= p & -p)
- ans += a[p];
- return ans;
- }
- ll Get(int l, int r)
- {
- return Get(r) - Get(l - 1);
- }
- } f, g, h, k;
- int n;
- ll a[N];
- void Read()
- {
- cin >> n;
- for (int i = 1; i <= n; ++i)
- cin >> a[i];
- }
- void Buffalo()
- {
- ll ans(0);
- for (int i = 1; i <= n; ++i)
- {
- for (int j = 1; j < i; ++j)
- ans += a[i] % a[j] + a[j] % a[i];
- cout << ans << " ";
- }
- cout << "\n";
- }
- void Solve()
- {
- // /Buffalo();
- ll ans(0);
- for (int i = 1; i <= n; ++i)
- {
- ans += (h.Get(a[i] + 1, N - 1) * a[i]) + (f.Get(a[i]) * a[i] - g.Get(a[i]) + k.Get(1, a[i] - 1));
- for (int j = a[i]; j < N; j += a[i])
- {
- ans += k.Get(j + 1, min((ll)N - 1, j + a[i] - 1)) - h.Get(j + 1, min((ll)N - 1, j + a[i] - 1)) * j;
- //if (j <= 20)
- // cout << j << ": " << k.Get(j + 1, j + a[i] - 1) << " " << h.Get(j + 1, j + a[i] - 1) * j << "\n";
- g.Update(j + 1, j);
- g.Update(j + a[i], -j);
- f.Update(j + 1, 1);
- f.Update(j + a[i], -1);
- }
- h.Update(a[i], 1);
- k.Update(a[i], a[i]);
- cout << ans << " ";
- //cout << "\n";
- }
- }
- int32_t main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- int t(1);
- if (typetest)
- cin >> t;
- for (int _ = 1; _ <= t; ++_)
- {
- //cout << "Case #" << _ << "\n";
- Read();
- Solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment