// oooo
/*
har chi delet mikhad bebar ~
gitar o ba khodet nabar! ~
;Amoo_Hasan;
*/
#include<bits/stdc++.h>
//#pragma GCC optimize("O3,no-stack-protector,unroll-loops")
//#pragma GCC target("avx2,fma")
using namespace std;
typedef long long ll;
typedef long double ld;
#define Sz(x) int((x).size())
#define All(x) (x).begin(), (x).end()
#define wtf(x) cout<<#x <<" : " <<x <<endl
#define mak make_pair
//constexpr int PRI = 1000696969;
constexpr ll INF = 1e18, N = 1e5 + 10, LOG = 30;
int n, u[N], v[N], a[N];
int h[N], st[N], fn[N], tim;
int par[N][LOG];
vector<int> adj[N];
void compress() {
vector<int> vc;
for(int i = 1; i <= n; i++) vc.push_back(a[i]);
sort(All(vc));
vc.erase(unique(All(vc)), vc.end());
for(int i = 1; i <= n; i++) a[i] = lower_bound(All(vc), a[i]) - vc.begin();
for(int i = 1; i <= n; i++) a[i]++;
}
void dfs(int x, int p = 0) {
st[x] = tim++;
par[x][0] = p;
for(int i = 1; i < LOG; i++) par[x][i] = par[par[x][i - 1]][i - 1];
h[x] = h[p] + 1;
for(auto j : adj[x]) {
if(j == p) continue;
dfs(j, x);
}
fn[x] = tim;
}
struct node {
int mx, cnt;
node() {
mx = cnt = 0;
}
};
struct Seg {
node x[N << 2];
node merge(node a, node b) {
node res;
res = a;
if(b.mx > a.mx) res = b;
return res;
}
void change(int l, int r, int ind, int val, int timer, int v) {
if(r - l < 2) {
x[v].mx = timer, x[v].cnt = a[val];
return;
}
int mid = (l + r) >> 1;
if(ind < mid) change(l, mid, ind, val, timer, 2 * v);
else change(mid, r, ind, val, timer, 2 * v + 1);
x[v] = merge(x[2 * v], x[2 * v + 1]);
}
node get(int l, int r, int s, int e, int v) {
node res;
if(r <= s || l >= e) return res;
if(l >= s && r <= e) return x[v];
int mid = (l + r) >> 1;
return merge(get(l, mid, s, e, 2 * v), get(mid, r, s, e, 2 * v + 1));
}
} S;
int find(int v) {
node b = S.get(0, n, st[v], fn[v], 1);
for(int i = LOG - 1; i >= 0; i--) {
int p = par[v][i];
if(p == 0) continue;
node a = S.get(0, n, st[p], fn[p], 1);
if(a.mx == b.mx) v = p;
}
return v;
}
struct Fenwick {
int fen[N];
vector<int> his;
Fenwick() {
his.clear();
memset(fen, 0, sizeof(fen));
}
void upd(int ind, int val) {
for(++ind; ind < N; ind += ind & -ind) {
fen[ind] += val;
his.push_back(ind);
}
}
int get(int ind) {
int sum = 0;
for(++ind; ind; ind -= ind & -ind)
sum += fen[ind];
return sum;
}
void clear() {
for(auto j : his) fen[j] = 0;
his.clear();
}
} F;
int main() {
ios :: sync_with_stdio(0), cin.tie(0); cout.tie(0);
cin >>n;
for(int i = 1; i <= n; i++) cin >>a[i];
compress();
for(int i = 0; i < n - 1; i++) {
cin >>u[i] >>v[i];
adj[u[i]].push_back(v[i]), adj[v[i]].push_back(u[i]);
}
dfs(1);
S.change(0, n, st[1], 1, 1, 1);
for(int i = 0; i < n - 1; i++) {
int x = u[i], y = v[i];
if(par[x][0] == y) swap(x, y);
ll ans = 0;
while(x) {
int X = find(x);
int cur = h[x] - h[X] + 1;
int val = S.get(0, n, st[x], fn[x], 1).cnt;
ans += 1ll * F.get(val - 1) * cur;
F.upd(val, cur);
x = par[X][0];
}
S.change(0, n, st[y], y, i + 2, 1);
F.clear();
cout<<ans <<endl;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
4 ms |
6132 KB |
Output is correct |
3 |
Correct |
3 ms |
6228 KB |
Output is correct |
4 |
Correct |
4 ms |
6276 KB |
Output is correct |
5 |
Correct |
5 ms |
6228 KB |
Output is correct |
6 |
Correct |
7 ms |
6228 KB |
Output is correct |
7 |
Correct |
4 ms |
6228 KB |
Output is correct |
8 |
Correct |
4 ms |
6264 KB |
Output is correct |
9 |
Correct |
4 ms |
6228 KB |
Output is correct |
10 |
Correct |
4 ms |
6228 KB |
Output is correct |
11 |
Correct |
5 ms |
6228 KB |
Output is correct |
12 |
Correct |
7 ms |
6268 KB |
Output is correct |
13 |
Correct |
6 ms |
6324 KB |
Output is correct |
14 |
Correct |
6 ms |
6228 KB |
Output is correct |
15 |
Correct |
6 ms |
6228 KB |
Output is correct |
16 |
Correct |
8 ms |
6228 KB |
Output is correct |
17 |
Correct |
5 ms |
6228 KB |
Output is correct |
18 |
Correct |
6 ms |
6228 KB |
Output is correct |
19 |
Correct |
4 ms |
6228 KB |
Output is correct |
20 |
Correct |
5 ms |
6228 KB |
Output is correct |
21 |
Correct |
5 ms |
6324 KB |
Output is correct |
22 |
Correct |
6 ms |
6228 KB |
Output is correct |
23 |
Correct |
7 ms |
6228 KB |
Output is correct |
24 |
Correct |
6 ms |
6228 KB |
Output is correct |
25 |
Correct |
5 ms |
6228 KB |
Output is correct |
26 |
Correct |
4 ms |
6228 KB |
Output is correct |
27 |
Correct |
5 ms |
6320 KB |
Output is correct |
28 |
Correct |
5 ms |
6228 KB |
Output is correct |
29 |
Correct |
5 ms |
6228 KB |
Output is correct |
30 |
Correct |
5 ms |
6228 KB |
Output is correct |
31 |
Correct |
7 ms |
6228 KB |
Output is correct |
32 |
Correct |
4 ms |
6228 KB |
Output is correct |
33 |
Correct |
4 ms |
6228 KB |
Output is correct |
34 |
Correct |
5 ms |
6228 KB |
Output is correct |
35 |
Correct |
4 ms |
6228 KB |
Output is correct |
36 |
Correct |
7 ms |
6228 KB |
Output is correct |
37 |
Correct |
5 ms |
6228 KB |
Output is correct |
38 |
Correct |
6 ms |
6228 KB |
Output is correct |
39 |
Correct |
5 ms |
6228 KB |
Output is correct |
40 |
Correct |
5 ms |
6228 KB |
Output is correct |
41 |
Correct |
5 ms |
6228 KB |
Output is correct |
42 |
Correct |
4 ms |
6216 KB |
Output is correct |
43 |
Correct |
5 ms |
6228 KB |
Output is correct |
44 |
Correct |
5 ms |
6228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
4 ms |
6132 KB |
Output is correct |
3 |
Correct |
3 ms |
6228 KB |
Output is correct |
4 |
Correct |
4 ms |
6276 KB |
Output is correct |
5 |
Correct |
5 ms |
6228 KB |
Output is correct |
6 |
Correct |
7 ms |
6228 KB |
Output is correct |
7 |
Correct |
4 ms |
6228 KB |
Output is correct |
8 |
Correct |
4 ms |
6264 KB |
Output is correct |
9 |
Correct |
4 ms |
6228 KB |
Output is correct |
10 |
Correct |
4 ms |
6228 KB |
Output is correct |
11 |
Correct |
5 ms |
6228 KB |
Output is correct |
12 |
Correct |
7 ms |
6268 KB |
Output is correct |
13 |
Correct |
6 ms |
6324 KB |
Output is correct |
14 |
Correct |
6 ms |
6228 KB |
Output is correct |
15 |
Correct |
6 ms |
6228 KB |
Output is correct |
16 |
Correct |
8 ms |
6228 KB |
Output is correct |
17 |
Correct |
5 ms |
6228 KB |
Output is correct |
18 |
Correct |
6 ms |
6228 KB |
Output is correct |
19 |
Correct |
4 ms |
6228 KB |
Output is correct |
20 |
Correct |
5 ms |
6228 KB |
Output is correct |
21 |
Correct |
5 ms |
6324 KB |
Output is correct |
22 |
Correct |
6 ms |
6228 KB |
Output is correct |
23 |
Correct |
7 ms |
6228 KB |
Output is correct |
24 |
Correct |
6 ms |
6228 KB |
Output is correct |
25 |
Correct |
5 ms |
6228 KB |
Output is correct |
26 |
Correct |
4 ms |
6228 KB |
Output is correct |
27 |
Correct |
5 ms |
6320 KB |
Output is correct |
28 |
Correct |
5 ms |
6228 KB |
Output is correct |
29 |
Correct |
5 ms |
6228 KB |
Output is correct |
30 |
Correct |
5 ms |
6228 KB |
Output is correct |
31 |
Correct |
7 ms |
6228 KB |
Output is correct |
32 |
Correct |
4 ms |
6228 KB |
Output is correct |
33 |
Correct |
4 ms |
6228 KB |
Output is correct |
34 |
Correct |
5 ms |
6228 KB |
Output is correct |
35 |
Correct |
4 ms |
6228 KB |
Output is correct |
36 |
Correct |
7 ms |
6228 KB |
Output is correct |
37 |
Correct |
5 ms |
6228 KB |
Output is correct |
38 |
Correct |
6 ms |
6228 KB |
Output is correct |
39 |
Correct |
5 ms |
6228 KB |
Output is correct |
40 |
Correct |
5 ms |
6228 KB |
Output is correct |
41 |
Correct |
5 ms |
6228 KB |
Output is correct |
42 |
Correct |
4 ms |
6216 KB |
Output is correct |
43 |
Correct |
5 ms |
6228 KB |
Output is correct |
44 |
Correct |
5 ms |
6228 KB |
Output is correct |
45 |
Correct |
7 ms |
6356 KB |
Output is correct |
46 |
Correct |
22 ms |
6924 KB |
Output is correct |
47 |
Correct |
22 ms |
6920 KB |
Output is correct |
48 |
Correct |
21 ms |
6924 KB |
Output is correct |
49 |
Correct |
14 ms |
7108 KB |
Output is correct |
50 |
Correct |
14 ms |
7104 KB |
Output is correct |
51 |
Correct |
13 ms |
7064 KB |
Output is correct |
52 |
Correct |
20 ms |
7236 KB |
Output is correct |
53 |
Correct |
19 ms |
7252 KB |
Output is correct |
54 |
Correct |
20 ms |
7228 KB |
Output is correct |
55 |
Correct |
20 ms |
7252 KB |
Output is correct |
56 |
Correct |
26 ms |
7100 KB |
Output is correct |
57 |
Correct |
44 ms |
6868 KB |
Output is correct |
58 |
Correct |
36 ms |
6992 KB |
Output is correct |
59 |
Correct |
36 ms |
7000 KB |
Output is correct |
60 |
Correct |
37 ms |
7004 KB |
Output is correct |
61 |
Correct |
22 ms |
6996 KB |
Output is correct |
62 |
Correct |
20 ms |
6996 KB |
Output is correct |
63 |
Correct |
20 ms |
6996 KB |
Output is correct |
64 |
Correct |
23 ms |
6996 KB |
Output is correct |
65 |
Correct |
23 ms |
6972 KB |
Output is correct |
66 |
Correct |
23 ms |
6900 KB |
Output is correct |
67 |
Correct |
21 ms |
6868 KB |
Output is correct |
68 |
Correct |
15 ms |
7148 KB |
Output is correct |
69 |
Correct |
21 ms |
7252 KB |
Output is correct |
70 |
Correct |
26 ms |
7172 KB |
Output is correct |
71 |
Correct |
26 ms |
6996 KB |
Output is correct |
72 |
Correct |
35 ms |
6868 KB |
Output is correct |
73 |
Correct |
36 ms |
6868 KB |
Output is correct |
74 |
Correct |
20 ms |
7056 KB |
Output is correct |
75 |
Correct |
19 ms |
6984 KB |
Output is correct |
76 |
Correct |
17 ms |
7016 KB |
Output is correct |
77 |
Correct |
18 ms |
6980 KB |
Output is correct |
78 |
Correct |
17 ms |
6996 KB |
Output is correct |
79 |
Correct |
19 ms |
6996 KB |
Output is correct |
80 |
Correct |
17 ms |
6868 KB |
Output is correct |
81 |
Correct |
20 ms |
7028 KB |
Output is correct |
82 |
Correct |
19 ms |
7016 KB |
Output is correct |
83 |
Correct |
25 ms |
6996 KB |
Output is correct |
84 |
Correct |
22 ms |
6992 KB |
Output is correct |
85 |
Correct |
21 ms |
7000 KB |
Output is correct |
86 |
Correct |
20 ms |
6868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6100 KB |
Output is correct |
2 |
Correct |
4 ms |
6132 KB |
Output is correct |
3 |
Correct |
3 ms |
6228 KB |
Output is correct |
4 |
Correct |
4 ms |
6276 KB |
Output is correct |
5 |
Correct |
5 ms |
6228 KB |
Output is correct |
6 |
Correct |
7 ms |
6228 KB |
Output is correct |
7 |
Correct |
4 ms |
6228 KB |
Output is correct |
8 |
Correct |
4 ms |
6264 KB |
Output is correct |
9 |
Correct |
4 ms |
6228 KB |
Output is correct |
10 |
Correct |
4 ms |
6228 KB |
Output is correct |
11 |
Correct |
5 ms |
6228 KB |
Output is correct |
12 |
Correct |
7 ms |
6268 KB |
Output is correct |
13 |
Correct |
6 ms |
6324 KB |
Output is correct |
14 |
Correct |
6 ms |
6228 KB |
Output is correct |
15 |
Correct |
6 ms |
6228 KB |
Output is correct |
16 |
Correct |
8 ms |
6228 KB |
Output is correct |
17 |
Correct |
5 ms |
6228 KB |
Output is correct |
18 |
Correct |
6 ms |
6228 KB |
Output is correct |
19 |
Correct |
4 ms |
6228 KB |
Output is correct |
20 |
Correct |
5 ms |
6228 KB |
Output is correct |
21 |
Correct |
5 ms |
6324 KB |
Output is correct |
22 |
Correct |
6 ms |
6228 KB |
Output is correct |
23 |
Correct |
7 ms |
6228 KB |
Output is correct |
24 |
Correct |
6 ms |
6228 KB |
Output is correct |
25 |
Correct |
5 ms |
6228 KB |
Output is correct |
26 |
Correct |
4 ms |
6228 KB |
Output is correct |
27 |
Correct |
5 ms |
6320 KB |
Output is correct |
28 |
Correct |
5 ms |
6228 KB |
Output is correct |
29 |
Correct |
5 ms |
6228 KB |
Output is correct |
30 |
Correct |
5 ms |
6228 KB |
Output is correct |
31 |
Correct |
7 ms |
6228 KB |
Output is correct |
32 |
Correct |
4 ms |
6228 KB |
Output is correct |
33 |
Correct |
4 ms |
6228 KB |
Output is correct |
34 |
Correct |
5 ms |
6228 KB |
Output is correct |
35 |
Correct |
4 ms |
6228 KB |
Output is correct |
36 |
Correct |
7 ms |
6228 KB |
Output is correct |
37 |
Correct |
5 ms |
6228 KB |
Output is correct |
38 |
Correct |
6 ms |
6228 KB |
Output is correct |
39 |
Correct |
5 ms |
6228 KB |
Output is correct |
40 |
Correct |
5 ms |
6228 KB |
Output is correct |
41 |
Correct |
5 ms |
6228 KB |
Output is correct |
42 |
Correct |
4 ms |
6216 KB |
Output is correct |
43 |
Correct |
5 ms |
6228 KB |
Output is correct |
44 |
Correct |
5 ms |
6228 KB |
Output is correct |
45 |
Correct |
7 ms |
6356 KB |
Output is correct |
46 |
Correct |
22 ms |
6924 KB |
Output is correct |
47 |
Correct |
22 ms |
6920 KB |
Output is correct |
48 |
Correct |
21 ms |
6924 KB |
Output is correct |
49 |
Correct |
14 ms |
7108 KB |
Output is correct |
50 |
Correct |
14 ms |
7104 KB |
Output is correct |
51 |
Correct |
13 ms |
7064 KB |
Output is correct |
52 |
Correct |
20 ms |
7236 KB |
Output is correct |
53 |
Correct |
19 ms |
7252 KB |
Output is correct |
54 |
Correct |
20 ms |
7228 KB |
Output is correct |
55 |
Correct |
20 ms |
7252 KB |
Output is correct |
56 |
Correct |
26 ms |
7100 KB |
Output is correct |
57 |
Correct |
44 ms |
6868 KB |
Output is correct |
58 |
Correct |
36 ms |
6992 KB |
Output is correct |
59 |
Correct |
36 ms |
7000 KB |
Output is correct |
60 |
Correct |
37 ms |
7004 KB |
Output is correct |
61 |
Correct |
22 ms |
6996 KB |
Output is correct |
62 |
Correct |
20 ms |
6996 KB |
Output is correct |
63 |
Correct |
20 ms |
6996 KB |
Output is correct |
64 |
Correct |
23 ms |
6996 KB |
Output is correct |
65 |
Correct |
23 ms |
6972 KB |
Output is correct |
66 |
Correct |
23 ms |
6900 KB |
Output is correct |
67 |
Correct |
21 ms |
6868 KB |
Output is correct |
68 |
Correct |
15 ms |
7148 KB |
Output is correct |
69 |
Correct |
21 ms |
7252 KB |
Output is correct |
70 |
Correct |
26 ms |
7172 KB |
Output is correct |
71 |
Correct |
26 ms |
6996 KB |
Output is correct |
72 |
Correct |
35 ms |
6868 KB |
Output is correct |
73 |
Correct |
36 ms |
6868 KB |
Output is correct |
74 |
Correct |
20 ms |
7056 KB |
Output is correct |
75 |
Correct |
19 ms |
6984 KB |
Output is correct |
76 |
Correct |
17 ms |
7016 KB |
Output is correct |
77 |
Correct |
18 ms |
6980 KB |
Output is correct |
78 |
Correct |
17 ms |
6996 KB |
Output is correct |
79 |
Correct |
19 ms |
6996 KB |
Output is correct |
80 |
Correct |
17 ms |
6868 KB |
Output is correct |
81 |
Correct |
20 ms |
7028 KB |
Output is correct |
82 |
Correct |
19 ms |
7016 KB |
Output is correct |
83 |
Correct |
25 ms |
6996 KB |
Output is correct |
84 |
Correct |
22 ms |
6992 KB |
Output is correct |
85 |
Correct |
21 ms |
7000 KB |
Output is correct |
86 |
Correct |
20 ms |
6868 KB |
Output is correct |
87 |
Correct |
59 ms |
8156 KB |
Output is correct |
88 |
Correct |
212 ms |
12144 KB |
Output is correct |
89 |
Correct |
867 ms |
25952 KB |
Output is correct |
90 |
Correct |
901 ms |
25996 KB |
Output is correct |
91 |
Correct |
884 ms |
25816 KB |
Output is correct |
92 |
Correct |
403 ms |
30400 KB |
Output is correct |
93 |
Correct |
395 ms |
30340 KB |
Output is correct |
94 |
Correct |
415 ms |
30352 KB |
Output is correct |
95 |
Correct |
680 ms |
30188 KB |
Output is correct |
96 |
Correct |
648 ms |
30192 KB |
Output is correct |
97 |
Correct |
649 ms |
30172 KB |
Output is correct |
98 |
Correct |
690 ms |
30180 KB |
Output is correct |
99 |
Correct |
660 ms |
28204 KB |
Output is correct |
100 |
Correct |
1583 ms |
25472 KB |
Output is correct |
101 |
Correct |
1615 ms |
25768 KB |
Output is correct |
102 |
Correct |
1680 ms |
25832 KB |
Output is correct |
103 |
Correct |
1664 ms |
25920 KB |
Output is correct |
104 |
Correct |
883 ms |
28188 KB |
Output is correct |
105 |
Correct |
794 ms |
28168 KB |
Output is correct |
106 |
Correct |
816 ms |
28092 KB |
Output is correct |
107 |
Correct |
819 ms |
25124 KB |
Output is correct |
108 |
Correct |
907 ms |
25264 KB |
Output is correct |
109 |
Correct |
922 ms |
25520 KB |
Output is correct |
110 |
Correct |
449 ms |
29744 KB |
Output is correct |
111 |
Correct |
638 ms |
29784 KB |
Output is correct |
112 |
Correct |
662 ms |
31628 KB |
Output is correct |
113 |
Correct |
641 ms |
27648 KB |
Output is correct |
114 |
Correct |
1657 ms |
25652 KB |
Output is correct |
115 |
Correct |
1634 ms |
25244 KB |
Output is correct |
116 |
Correct |
762 ms |
27460 KB |
Output is correct |
117 |
Correct |
608 ms |
26592 KB |
Output is correct |
118 |
Correct |
602 ms |
26292 KB |
Output is correct |
119 |
Correct |
571 ms |
26084 KB |
Output is correct |
120 |
Correct |
647 ms |
26196 KB |
Output is correct |
121 |
Correct |
625 ms |
26088 KB |
Output is correct |
122 |
Correct |
563 ms |
25728 KB |
Output is correct |
123 |
Correct |
751 ms |
26812 KB |
Output is correct |
124 |
Correct |
712 ms |
26292 KB |
Output is correct |
125 |
Correct |
692 ms |
26252 KB |
Output is correct |
126 |
Correct |
769 ms |
26532 KB |
Output is correct |
127 |
Correct |
711 ms |
26020 KB |
Output is correct |
128 |
Correct |
744 ms |
25936 KB |
Output is correct |