-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathf.cpp
55 lines (51 loc) · 1.25 KB
/
f.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
#include <bits/stdc++.h>
#define endl '\n'
#define eat cin
#define moo cout
#define int long long
using namespace std;
struct BIT{
int ar[800001], N;
void init(int N){
this->N = N;
fill(ar, ar+N, 0);
}
void upd(int i, int dv){
for(; i <= N; i += i&-i) ar[i] += dv;
}
int qry(int i){
int ret = 0;
for(; i > 0; i -= i&-i) ret += ar[i];
return ret;
}
};
int N, a[200000], pfx[200000], a1[200000], a2[200000];
BIT *bit;
int32_t main(){
eat.tie(0) -> sync_with_stdio(0);
eat >> N;
for(int i = 0; i < N; i++){
eat >> a[i];
pfx[i] = (i == 0 ? 0 : pfx[i-1]) + a[i];
}
bit = new BIT();
bit->init(800001);
for(int i = 0; i < N; i++){
a1[i] = (i == 0 ? 0 : a1[i-1]) + a[i] * i + bit->qry(a[i]);
for(int j = a[i]; j <= 300000; j += a[i]){
bit->upd(j, -a[i]);
}
}
bit->init(800001);
for(int i = 0; i < N; i++){
a2[i] = (i == 0 ? 0 : a2[i-1] + pfx[i-1]);
for(int j = a[i]; j <= 300000; j += a[i]){
a2[i] += (bit->qry(j + a[i] - 1) - bit->qry(j-1)) * j;
}
bit->upd(a[i], -1);
}
for(int i = 0; i < N; i++){
moo << a1[i] + a2[i] << ' ';
}
moo << endl;
}