#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int N;
int A[MAXN], B[MAXN], C[MAXN];
vector<int> adj[MAXN];
void Read() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
cin >> N;
for (int i = 1; i <= N; i++) {
cin >> C[i];
}
for (int i = 0; i < N - 1; i++) {
cin >> A[i] >> B[i];
adj[A[i]].emplace_back(B[i]);
adj[B[i]].emplace_back(A[i]);
}
// Coordinate compression on C
vector<int> coord;
for (int i = 1; i <= N; i++) {
coord.emplace_back(C[i]);
}
sort(begin(coord), end(coord));
coord.resize(unique(begin(coord), end(coord)) - begin(coord));
for (int i = 1; i <= N; i++) {
C[i] = lower_bound(begin(coord), end(coord), C[i]) - begin(coord) + 1;
}
}
//// Heavy Light Decomposition
int root[MAXN]; // root of current chain
int par[MAXN]; // parent node
int sz[MAXN]; // size of subtree
int depth[MAXN];
void dfsSz(int n, int p) {
if (p) adj[n].erase(find(begin(adj[n]), end(adj[n]), p));
par[n] = p;
sz[n] = 1;
depth[n] = depth[p] + 1;
for (auto &i : adj[n]) {
dfsSz(i, n);
sz[n] += sz[i];
if (sz[i] > sz[adj[n][0]]) {
swap(i, adj[n][0]);
}
}
}
void dfsHld(int n) {
for (auto &i : adj[n]) {
root[i] = (i == adj[n][0] ? root[n] : i);
dfsHld(i);
}
}
void HLD() {
root[1] = 1;
dfsSz(1, 0);
dfsHld(1);
}
//// Binary Indexed Tree
int BIT[MAXN];
void Add(int pos, int x) {
for (int i = pos; i <= N; i += i & -i) BIT[i] += x;
}
int Sum(int pos) {
int res = 0;
for (int i = pos; i > 0; i -= i & -i) res += BIT[i];
return res;
}
//// Main Solution
long long CountInversion(const vector<pair<int, int>> &V) { // (frequency, value)
long long res = 0;
int n = V.size();
for (int i = n - 1; i >= 0; i--) {
Add(V[i].second, V[i].first);
res += 1ll * V[i].first * Sum(V[i].second - 1);
}
for (int i = 0; i < n; i++) {
Add(V[i].second, -V[i].first);
}
return res;
}
deque<pair<int, int>> chainValues[MAXN]; // values in the chain, (frequency, value). Each construction adds at most O(log N) values.
long long Construction(int a, int b) { // construct a new edge (a, b)
vector<pair<int, int>> path; // values in path from root to a
// get values in path and delete them
while (a != 0) {
deque<pair<int, int>> &chain = chainValues[root[a]];
int take = depth[a] - depth[root[a]] + 1;
int ptr = path.size();
while (take > 0) {
if (take >= chain.front().first) {
take -= chain.front().first;
path.emplace_back(chain.front());
chain.pop_front();
} else {
path.emplace_back(take, chain.front().second);
chain.front().first -= take;
take = 0;
}
}
reverse(begin(path) + ptr, end(path));
a = par[root[a]];
}
reverse(begin(path), end(path));
int new_C = C[b];
while (b != 0) {
deque<pair<int, int>> &chain = chainValues[root[b]];
int put = depth[b] - depth[root[b]] + 1;
chain.emplace_front(put, new_C);
b = par[root[b]];
}
return CountInversion(path);
}
void Solve() {
chainValues[1].emplace_back(1, C[1]);
for (int i = 0; i < N - 1; i++) {
cout << Construction(A[i], B[i]) << "\n";
}
}
int main() {
Read();
HLD();
Solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
70008 KB |
Output is correct |
2 |
Correct |
55 ms |
70016 KB |
Output is correct |
3 |
Correct |
55 ms |
70264 KB |
Output is correct |
4 |
Correct |
58 ms |
70136 KB |
Output is correct |
5 |
Correct |
56 ms |
70264 KB |
Output is correct |
6 |
Correct |
58 ms |
70136 KB |
Output is correct |
7 |
Correct |
57 ms |
70272 KB |
Output is correct |
8 |
Correct |
54 ms |
70136 KB |
Output is correct |
9 |
Correct |
54 ms |
70136 KB |
Output is correct |
10 |
Correct |
54 ms |
70136 KB |
Output is correct |
11 |
Correct |
58 ms |
70264 KB |
Output is correct |
12 |
Correct |
58 ms |
70264 KB |
Output is correct |
13 |
Correct |
56 ms |
70272 KB |
Output is correct |
14 |
Correct |
58 ms |
70264 KB |
Output is correct |
15 |
Correct |
62 ms |
70264 KB |
Output is correct |
16 |
Correct |
57 ms |
70136 KB |
Output is correct |
17 |
Correct |
56 ms |
70272 KB |
Output is correct |
18 |
Correct |
59 ms |
70272 KB |
Output is correct |
19 |
Correct |
56 ms |
70264 KB |
Output is correct |
20 |
Correct |
61 ms |
70264 KB |
Output is correct |
21 |
Correct |
58 ms |
70264 KB |
Output is correct |
22 |
Correct |
58 ms |
70264 KB |
Output is correct |
23 |
Correct |
56 ms |
70264 KB |
Output is correct |
24 |
Correct |
58 ms |
70264 KB |
Output is correct |
25 |
Correct |
58 ms |
70136 KB |
Output is correct |
26 |
Correct |
55 ms |
70136 KB |
Output is correct |
27 |
Correct |
58 ms |
70264 KB |
Output is correct |
28 |
Correct |
57 ms |
70264 KB |
Output is correct |
29 |
Correct |
57 ms |
70272 KB |
Output is correct |
30 |
Correct |
57 ms |
70136 KB |
Output is correct |
31 |
Correct |
57 ms |
70136 KB |
Output is correct |
32 |
Correct |
58 ms |
70520 KB |
Output is correct |
33 |
Correct |
64 ms |
70136 KB |
Output is correct |
34 |
Correct |
60 ms |
70260 KB |
Output is correct |
35 |
Correct |
57 ms |
70136 KB |
Output is correct |
36 |
Correct |
56 ms |
70144 KB |
Output is correct |
37 |
Correct |
58 ms |
70136 KB |
Output is correct |
38 |
Correct |
60 ms |
70284 KB |
Output is correct |
39 |
Correct |
58 ms |
70264 KB |
Output is correct |
40 |
Correct |
58 ms |
70136 KB |
Output is correct |
41 |
Correct |
59 ms |
70136 KB |
Output is correct |
42 |
Correct |
58 ms |
70136 KB |
Output is correct |
43 |
Correct |
59 ms |
70264 KB |
Output is correct |
44 |
Correct |
58 ms |
70136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
70008 KB |
Output is correct |
2 |
Correct |
55 ms |
70016 KB |
Output is correct |
3 |
Correct |
55 ms |
70264 KB |
Output is correct |
4 |
Correct |
58 ms |
70136 KB |
Output is correct |
5 |
Correct |
56 ms |
70264 KB |
Output is correct |
6 |
Correct |
58 ms |
70136 KB |
Output is correct |
7 |
Correct |
57 ms |
70272 KB |
Output is correct |
8 |
Correct |
54 ms |
70136 KB |
Output is correct |
9 |
Correct |
54 ms |
70136 KB |
Output is correct |
10 |
Correct |
54 ms |
70136 KB |
Output is correct |
11 |
Correct |
58 ms |
70264 KB |
Output is correct |
12 |
Correct |
58 ms |
70264 KB |
Output is correct |
13 |
Correct |
56 ms |
70272 KB |
Output is correct |
14 |
Correct |
58 ms |
70264 KB |
Output is correct |
15 |
Correct |
62 ms |
70264 KB |
Output is correct |
16 |
Correct |
57 ms |
70136 KB |
Output is correct |
17 |
Correct |
56 ms |
70272 KB |
Output is correct |
18 |
Correct |
59 ms |
70272 KB |
Output is correct |
19 |
Correct |
56 ms |
70264 KB |
Output is correct |
20 |
Correct |
61 ms |
70264 KB |
Output is correct |
21 |
Correct |
58 ms |
70264 KB |
Output is correct |
22 |
Correct |
58 ms |
70264 KB |
Output is correct |
23 |
Correct |
56 ms |
70264 KB |
Output is correct |
24 |
Correct |
58 ms |
70264 KB |
Output is correct |
25 |
Correct |
58 ms |
70136 KB |
Output is correct |
26 |
Correct |
55 ms |
70136 KB |
Output is correct |
27 |
Correct |
58 ms |
70264 KB |
Output is correct |
28 |
Correct |
57 ms |
70264 KB |
Output is correct |
29 |
Correct |
57 ms |
70272 KB |
Output is correct |
30 |
Correct |
57 ms |
70136 KB |
Output is correct |
31 |
Correct |
57 ms |
70136 KB |
Output is correct |
32 |
Correct |
58 ms |
70520 KB |
Output is correct |
33 |
Correct |
64 ms |
70136 KB |
Output is correct |
34 |
Correct |
60 ms |
70260 KB |
Output is correct |
35 |
Correct |
57 ms |
70136 KB |
Output is correct |
36 |
Correct |
56 ms |
70144 KB |
Output is correct |
37 |
Correct |
58 ms |
70136 KB |
Output is correct |
38 |
Correct |
60 ms |
70284 KB |
Output is correct |
39 |
Correct |
58 ms |
70264 KB |
Output is correct |
40 |
Correct |
58 ms |
70136 KB |
Output is correct |
41 |
Correct |
59 ms |
70136 KB |
Output is correct |
42 |
Correct |
58 ms |
70136 KB |
Output is correct |
43 |
Correct |
59 ms |
70264 KB |
Output is correct |
44 |
Correct |
58 ms |
70136 KB |
Output is correct |
45 |
Correct |
60 ms |
70392 KB |
Output is correct |
46 |
Correct |
65 ms |
71416 KB |
Output is correct |
47 |
Correct |
63 ms |
71416 KB |
Output is correct |
48 |
Correct |
64 ms |
71416 KB |
Output is correct |
49 |
Correct |
57 ms |
70648 KB |
Output is correct |
50 |
Correct |
56 ms |
70648 KB |
Output is correct |
51 |
Correct |
58 ms |
70648 KB |
Output is correct |
52 |
Correct |
61 ms |
71544 KB |
Output is correct |
53 |
Correct |
60 ms |
71544 KB |
Output is correct |
54 |
Correct |
61 ms |
71544 KB |
Output is correct |
55 |
Correct |
62 ms |
71544 KB |
Output is correct |
56 |
Correct |
60 ms |
71544 KB |
Output is correct |
57 |
Correct |
62 ms |
71416 KB |
Output is correct |
58 |
Correct |
65 ms |
71416 KB |
Output is correct |
59 |
Correct |
63 ms |
71416 KB |
Output is correct |
60 |
Correct |
63 ms |
71416 KB |
Output is correct |
61 |
Correct |
62 ms |
71672 KB |
Output is correct |
62 |
Correct |
61 ms |
71544 KB |
Output is correct |
63 |
Correct |
62 ms |
71544 KB |
Output is correct |
64 |
Correct |
63 ms |
71416 KB |
Output is correct |
65 |
Correct |
63 ms |
71416 KB |
Output is correct |
66 |
Correct |
63 ms |
71416 KB |
Output is correct |
67 |
Correct |
63 ms |
71416 KB |
Output is correct |
68 |
Correct |
60 ms |
70680 KB |
Output is correct |
69 |
Correct |
60 ms |
71544 KB |
Output is correct |
70 |
Correct |
61 ms |
71544 KB |
Output is correct |
71 |
Correct |
62 ms |
71544 KB |
Output is correct |
72 |
Correct |
65 ms |
71416 KB |
Output is correct |
73 |
Correct |
65 ms |
71288 KB |
Output is correct |
74 |
Correct |
61 ms |
71544 KB |
Output is correct |
75 |
Correct |
63 ms |
71416 KB |
Output is correct |
76 |
Correct |
63 ms |
71416 KB |
Output is correct |
77 |
Correct |
62 ms |
71416 KB |
Output is correct |
78 |
Correct |
65 ms |
71416 KB |
Output is correct |
79 |
Correct |
63 ms |
71416 KB |
Output is correct |
80 |
Correct |
62 ms |
71416 KB |
Output is correct |
81 |
Correct |
62 ms |
71416 KB |
Output is correct |
82 |
Correct |
61 ms |
71416 KB |
Output is correct |
83 |
Correct |
62 ms |
71416 KB |
Output is correct |
84 |
Correct |
62 ms |
71416 KB |
Output is correct |
85 |
Correct |
61 ms |
71424 KB |
Output is correct |
86 |
Correct |
61 ms |
71416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
70008 KB |
Output is correct |
2 |
Correct |
55 ms |
70016 KB |
Output is correct |
3 |
Correct |
55 ms |
70264 KB |
Output is correct |
4 |
Correct |
58 ms |
70136 KB |
Output is correct |
5 |
Correct |
56 ms |
70264 KB |
Output is correct |
6 |
Correct |
58 ms |
70136 KB |
Output is correct |
7 |
Correct |
57 ms |
70272 KB |
Output is correct |
8 |
Correct |
54 ms |
70136 KB |
Output is correct |
9 |
Correct |
54 ms |
70136 KB |
Output is correct |
10 |
Correct |
54 ms |
70136 KB |
Output is correct |
11 |
Correct |
58 ms |
70264 KB |
Output is correct |
12 |
Correct |
58 ms |
70264 KB |
Output is correct |
13 |
Correct |
56 ms |
70272 KB |
Output is correct |
14 |
Correct |
58 ms |
70264 KB |
Output is correct |
15 |
Correct |
62 ms |
70264 KB |
Output is correct |
16 |
Correct |
57 ms |
70136 KB |
Output is correct |
17 |
Correct |
56 ms |
70272 KB |
Output is correct |
18 |
Correct |
59 ms |
70272 KB |
Output is correct |
19 |
Correct |
56 ms |
70264 KB |
Output is correct |
20 |
Correct |
61 ms |
70264 KB |
Output is correct |
21 |
Correct |
58 ms |
70264 KB |
Output is correct |
22 |
Correct |
58 ms |
70264 KB |
Output is correct |
23 |
Correct |
56 ms |
70264 KB |
Output is correct |
24 |
Correct |
58 ms |
70264 KB |
Output is correct |
25 |
Correct |
58 ms |
70136 KB |
Output is correct |
26 |
Correct |
55 ms |
70136 KB |
Output is correct |
27 |
Correct |
58 ms |
70264 KB |
Output is correct |
28 |
Correct |
57 ms |
70264 KB |
Output is correct |
29 |
Correct |
57 ms |
70272 KB |
Output is correct |
30 |
Correct |
57 ms |
70136 KB |
Output is correct |
31 |
Correct |
57 ms |
70136 KB |
Output is correct |
32 |
Correct |
58 ms |
70520 KB |
Output is correct |
33 |
Correct |
64 ms |
70136 KB |
Output is correct |
34 |
Correct |
60 ms |
70260 KB |
Output is correct |
35 |
Correct |
57 ms |
70136 KB |
Output is correct |
36 |
Correct |
56 ms |
70144 KB |
Output is correct |
37 |
Correct |
58 ms |
70136 KB |
Output is correct |
38 |
Correct |
60 ms |
70284 KB |
Output is correct |
39 |
Correct |
58 ms |
70264 KB |
Output is correct |
40 |
Correct |
58 ms |
70136 KB |
Output is correct |
41 |
Correct |
59 ms |
70136 KB |
Output is correct |
42 |
Correct |
58 ms |
70136 KB |
Output is correct |
43 |
Correct |
59 ms |
70264 KB |
Output is correct |
44 |
Correct |
58 ms |
70136 KB |
Output is correct |
45 |
Correct |
60 ms |
70392 KB |
Output is correct |
46 |
Correct |
65 ms |
71416 KB |
Output is correct |
47 |
Correct |
63 ms |
71416 KB |
Output is correct |
48 |
Correct |
64 ms |
71416 KB |
Output is correct |
49 |
Correct |
57 ms |
70648 KB |
Output is correct |
50 |
Correct |
56 ms |
70648 KB |
Output is correct |
51 |
Correct |
58 ms |
70648 KB |
Output is correct |
52 |
Correct |
61 ms |
71544 KB |
Output is correct |
53 |
Correct |
60 ms |
71544 KB |
Output is correct |
54 |
Correct |
61 ms |
71544 KB |
Output is correct |
55 |
Correct |
62 ms |
71544 KB |
Output is correct |
56 |
Correct |
60 ms |
71544 KB |
Output is correct |
57 |
Correct |
62 ms |
71416 KB |
Output is correct |
58 |
Correct |
65 ms |
71416 KB |
Output is correct |
59 |
Correct |
63 ms |
71416 KB |
Output is correct |
60 |
Correct |
63 ms |
71416 KB |
Output is correct |
61 |
Correct |
62 ms |
71672 KB |
Output is correct |
62 |
Correct |
61 ms |
71544 KB |
Output is correct |
63 |
Correct |
62 ms |
71544 KB |
Output is correct |
64 |
Correct |
63 ms |
71416 KB |
Output is correct |
65 |
Correct |
63 ms |
71416 KB |
Output is correct |
66 |
Correct |
63 ms |
71416 KB |
Output is correct |
67 |
Correct |
63 ms |
71416 KB |
Output is correct |
68 |
Correct |
60 ms |
70680 KB |
Output is correct |
69 |
Correct |
60 ms |
71544 KB |
Output is correct |
70 |
Correct |
61 ms |
71544 KB |
Output is correct |
71 |
Correct |
62 ms |
71544 KB |
Output is correct |
72 |
Correct |
65 ms |
71416 KB |
Output is correct |
73 |
Correct |
65 ms |
71288 KB |
Output is correct |
74 |
Correct |
61 ms |
71544 KB |
Output is correct |
75 |
Correct |
63 ms |
71416 KB |
Output is correct |
76 |
Correct |
63 ms |
71416 KB |
Output is correct |
77 |
Correct |
62 ms |
71416 KB |
Output is correct |
78 |
Correct |
65 ms |
71416 KB |
Output is correct |
79 |
Correct |
63 ms |
71416 KB |
Output is correct |
80 |
Correct |
62 ms |
71416 KB |
Output is correct |
81 |
Correct |
62 ms |
71416 KB |
Output is correct |
82 |
Correct |
61 ms |
71416 KB |
Output is correct |
83 |
Correct |
62 ms |
71416 KB |
Output is correct |
84 |
Correct |
62 ms |
71416 KB |
Output is correct |
85 |
Correct |
61 ms |
71424 KB |
Output is correct |
86 |
Correct |
61 ms |
71416 KB |
Output is correct |
87 |
Correct |
79 ms |
73464 KB |
Output is correct |
88 |
Correct |
121 ms |
80508 KB |
Output is correct |
89 |
Correct |
356 ms |
104664 KB |
Output is correct |
90 |
Correct |
330 ms |
104684 KB |
Output is correct |
91 |
Correct |
347 ms |
104672 KB |
Output is correct |
92 |
Correct |
161 ms |
84968 KB |
Output is correct |
93 |
Correct |
163 ms |
84952 KB |
Output is correct |
94 |
Correct |
167 ms |
85084 KB |
Output is correct |
95 |
Correct |
210 ms |
108504 KB |
Output is correct |
96 |
Correct |
204 ms |
108892 KB |
Output is correct |
97 |
Correct |
216 ms |
108888 KB |
Output is correct |
98 |
Correct |
212 ms |
108892 KB |
Output is correct |
99 |
Correct |
225 ms |
107740 KB |
Output is correct |
100 |
Correct |
355 ms |
96220 KB |
Output is correct |
101 |
Correct |
396 ms |
96476 KB |
Output is correct |
102 |
Correct |
407 ms |
96476 KB |
Output is correct |
103 |
Correct |
389 ms |
96604 KB |
Output is correct |
104 |
Correct |
222 ms |
107736 KB |
Output is correct |
105 |
Correct |
222 ms |
107736 KB |
Output is correct |
106 |
Correct |
223 ms |
107868 KB |
Output is correct |
107 |
Correct |
292 ms |
103520 KB |
Output is correct |
108 |
Correct |
309 ms |
104032 KB |
Output is correct |
109 |
Correct |
315 ms |
104028 KB |
Output is correct |
110 |
Correct |
143 ms |
84072 KB |
Output is correct |
111 |
Correct |
203 ms |
108596 KB |
Output is correct |
112 |
Correct |
197 ms |
107868 KB |
Output is correct |
113 |
Correct |
204 ms |
106788 KB |
Output is correct |
114 |
Correct |
366 ms |
96344 KB |
Output is correct |
115 |
Correct |
366 ms |
95580 KB |
Output is correct |
116 |
Correct |
224 ms |
106712 KB |
Output is correct |
117 |
Correct |
230 ms |
105816 KB |
Output is correct |
118 |
Correct |
222 ms |
105308 KB |
Output is correct |
119 |
Correct |
235 ms |
104924 KB |
Output is correct |
120 |
Correct |
219 ms |
105180 KB |
Output is correct |
121 |
Correct |
205 ms |
104540 KB |
Output is correct |
122 |
Correct |
207 ms |
104412 KB |
Output is correct |
123 |
Correct |
227 ms |
105816 KB |
Output is correct |
124 |
Correct |
232 ms |
105308 KB |
Output is correct |
125 |
Correct |
233 ms |
105052 KB |
Output is correct |
126 |
Correct |
220 ms |
105180 KB |
Output is correct |
127 |
Correct |
226 ms |
104796 KB |
Output is correct |
128 |
Correct |
218 ms |
104412 KB |
Output is correct |