#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int c[100001], u[100001], v[100001], par[100001];
vector<int> graph[100001];
int heavy[100001], head[100001], depth[100001];
deque<pair<int, int>> hld_vals[100001];
int dfs(int node = 1) {
int sz = 1, mx = 0;
for (int i : graph[node]) {
int csz = dfs(i);
sz += csz;
if (csz > mx) mx = csz, heavy[node] = i;
}
return sz;
}
void decompose(int node = 1, int h = 1, int d = 1) {
head[node] = h; depth[node] = d;
if (heavy[node]) decompose(heavy[node], h, d + 1);
for (int i : graph[node]) if (i != heavy[node]) decompose(i, i, 1);
}
ll count_inversions(deque<pair<int, int>> &path) {
map<int, int> comp;
for (pair<int, int> i : path) comp[i.first];
int n = 0;
for (auto &i : comp) i.second = ++n;
ll ans = 0;
vector<ll> bit(n + 1);
for (pair<int, int> i : path) {
for (int j = comp[i.first] - 1; j; j -= j & -j) ans += bit[j] * i.second;
for (int j = comp[i.first]; j <= n; j += j & -j) bit[j] += i.second;
}
return ans;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> c[i];
for (int i = 1; i < n; i++) {
cin >> u[i] >> v[i];
graph[u[i]].push_back(v[i]);
par[v[i]] = u[i];
}
dfs(); decompose();
hld_vals[1].push_back({c[1], 1});
for (int i = 1; i < n; i++) {
deque<pair<int, int>> path;
int curr = v[i];
while (curr) {
deque<pair<int, int>> ins;
int rem = depth[curr] - (curr == v[i]), h = head[curr];
while (rem) {
if (hld_vals[h][0].second <= rem) {
rem -= hld_vals[h][0].second;
ins.push_front(hld_vals[h][0]);
hld_vals[h].pop_front();
} else {
ins.push_front({hld_vals[h][0].first, rem});
pair<int, int> upd = {hld_vals[h][0].first, hld_vals[h][0].second - rem};
hld_vals[h].pop_front();
hld_vals[h].push_front(upd);
rem = 0;
}
}
hld_vals[h].push_front({c[v[i]], depth[curr]});
path.insert(path.end(), ins.begin(), ins.end());
curr = par[h];
}
cout << count_inversions(path) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
70124 KB |
Output is correct |
2 |
Correct |
55 ms |
69996 KB |
Output is correct |
3 |
Correct |
55 ms |
70124 KB |
Output is correct |
4 |
Correct |
54 ms |
70124 KB |
Output is correct |
5 |
Correct |
60 ms |
70252 KB |
Output is correct |
6 |
Correct |
54 ms |
70252 KB |
Output is correct |
7 |
Correct |
59 ms |
70252 KB |
Output is correct |
8 |
Correct |
54 ms |
70124 KB |
Output is correct |
9 |
Correct |
52 ms |
70124 KB |
Output is correct |
10 |
Correct |
62 ms |
70124 KB |
Output is correct |
11 |
Correct |
53 ms |
70252 KB |
Output is correct |
12 |
Correct |
54 ms |
70252 KB |
Output is correct |
13 |
Correct |
53 ms |
70252 KB |
Output is correct |
14 |
Correct |
57 ms |
70252 KB |
Output is correct |
15 |
Correct |
56 ms |
70124 KB |
Output is correct |
16 |
Correct |
54 ms |
70124 KB |
Output is correct |
17 |
Correct |
55 ms |
70124 KB |
Output is correct |
18 |
Correct |
57 ms |
70124 KB |
Output is correct |
19 |
Correct |
67 ms |
70252 KB |
Output is correct |
20 |
Correct |
58 ms |
70252 KB |
Output is correct |
21 |
Correct |
57 ms |
70252 KB |
Output is correct |
22 |
Correct |
61 ms |
70252 KB |
Output is correct |
23 |
Correct |
62 ms |
70252 KB |
Output is correct |
24 |
Correct |
54 ms |
70124 KB |
Output is correct |
25 |
Correct |
54 ms |
70124 KB |
Output is correct |
26 |
Correct |
54 ms |
70124 KB |
Output is correct |
27 |
Correct |
54 ms |
70252 KB |
Output is correct |
28 |
Correct |
54 ms |
70252 KB |
Output is correct |
29 |
Correct |
58 ms |
70252 KB |
Output is correct |
30 |
Correct |
55 ms |
70124 KB |
Output is correct |
31 |
Correct |
54 ms |
70124 KB |
Output is correct |
32 |
Correct |
58 ms |
70252 KB |
Output is correct |
33 |
Correct |
54 ms |
70140 KB |
Output is correct |
34 |
Correct |
61 ms |
70124 KB |
Output is correct |
35 |
Correct |
60 ms |
70252 KB |
Output is correct |
36 |
Correct |
53 ms |
70252 KB |
Output is correct |
37 |
Correct |
60 ms |
70124 KB |
Output is correct |
38 |
Correct |
60 ms |
70400 KB |
Output is correct |
39 |
Correct |
55 ms |
70252 KB |
Output is correct |
40 |
Correct |
65 ms |
70184 KB |
Output is correct |
41 |
Correct |
55 ms |
70124 KB |
Output is correct |
42 |
Correct |
58 ms |
70252 KB |
Output is correct |
43 |
Correct |
54 ms |
70264 KB |
Output is correct |
44 |
Correct |
61 ms |
70332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
70124 KB |
Output is correct |
2 |
Correct |
55 ms |
69996 KB |
Output is correct |
3 |
Correct |
55 ms |
70124 KB |
Output is correct |
4 |
Correct |
54 ms |
70124 KB |
Output is correct |
5 |
Correct |
60 ms |
70252 KB |
Output is correct |
6 |
Correct |
54 ms |
70252 KB |
Output is correct |
7 |
Correct |
59 ms |
70252 KB |
Output is correct |
8 |
Correct |
54 ms |
70124 KB |
Output is correct |
9 |
Correct |
52 ms |
70124 KB |
Output is correct |
10 |
Correct |
62 ms |
70124 KB |
Output is correct |
11 |
Correct |
53 ms |
70252 KB |
Output is correct |
12 |
Correct |
54 ms |
70252 KB |
Output is correct |
13 |
Correct |
53 ms |
70252 KB |
Output is correct |
14 |
Correct |
57 ms |
70252 KB |
Output is correct |
15 |
Correct |
56 ms |
70124 KB |
Output is correct |
16 |
Correct |
54 ms |
70124 KB |
Output is correct |
17 |
Correct |
55 ms |
70124 KB |
Output is correct |
18 |
Correct |
57 ms |
70124 KB |
Output is correct |
19 |
Correct |
67 ms |
70252 KB |
Output is correct |
20 |
Correct |
58 ms |
70252 KB |
Output is correct |
21 |
Correct |
57 ms |
70252 KB |
Output is correct |
22 |
Correct |
61 ms |
70252 KB |
Output is correct |
23 |
Correct |
62 ms |
70252 KB |
Output is correct |
24 |
Correct |
54 ms |
70124 KB |
Output is correct |
25 |
Correct |
54 ms |
70124 KB |
Output is correct |
26 |
Correct |
54 ms |
70124 KB |
Output is correct |
27 |
Correct |
54 ms |
70252 KB |
Output is correct |
28 |
Correct |
54 ms |
70252 KB |
Output is correct |
29 |
Correct |
58 ms |
70252 KB |
Output is correct |
30 |
Correct |
55 ms |
70124 KB |
Output is correct |
31 |
Correct |
54 ms |
70124 KB |
Output is correct |
32 |
Correct |
58 ms |
70252 KB |
Output is correct |
33 |
Correct |
54 ms |
70140 KB |
Output is correct |
34 |
Correct |
61 ms |
70124 KB |
Output is correct |
35 |
Correct |
60 ms |
70252 KB |
Output is correct |
36 |
Correct |
53 ms |
70252 KB |
Output is correct |
37 |
Correct |
60 ms |
70124 KB |
Output is correct |
38 |
Correct |
60 ms |
70400 KB |
Output is correct |
39 |
Correct |
55 ms |
70252 KB |
Output is correct |
40 |
Correct |
65 ms |
70184 KB |
Output is correct |
41 |
Correct |
55 ms |
70124 KB |
Output is correct |
42 |
Correct |
58 ms |
70252 KB |
Output is correct |
43 |
Correct |
54 ms |
70264 KB |
Output is correct |
44 |
Correct |
61 ms |
70332 KB |
Output is correct |
45 |
Correct |
58 ms |
70380 KB |
Output is correct |
46 |
Correct |
65 ms |
71276 KB |
Output is correct |
47 |
Correct |
75 ms |
71276 KB |
Output is correct |
48 |
Correct |
65 ms |
71276 KB |
Output is correct |
49 |
Correct |
56 ms |
70508 KB |
Output is correct |
50 |
Correct |
56 ms |
70508 KB |
Output is correct |
51 |
Correct |
56 ms |
70508 KB |
Output is correct |
52 |
Correct |
58 ms |
71532 KB |
Output is correct |
53 |
Correct |
60 ms |
71532 KB |
Output is correct |
54 |
Correct |
59 ms |
71532 KB |
Output is correct |
55 |
Correct |
59 ms |
71532 KB |
Output is correct |
56 |
Correct |
62 ms |
71404 KB |
Output is correct |
57 |
Correct |
67 ms |
71276 KB |
Output is correct |
58 |
Correct |
71 ms |
71276 KB |
Output is correct |
59 |
Correct |
69 ms |
71276 KB |
Output is correct |
60 |
Correct |
73 ms |
71276 KB |
Output is correct |
61 |
Correct |
62 ms |
71404 KB |
Output is correct |
62 |
Correct |
62 ms |
71404 KB |
Output is correct |
63 |
Correct |
64 ms |
71404 KB |
Output is correct |
64 |
Correct |
65 ms |
71404 KB |
Output is correct |
65 |
Correct |
67 ms |
71276 KB |
Output is correct |
66 |
Correct |
65 ms |
71276 KB |
Output is correct |
67 |
Correct |
64 ms |
71276 KB |
Output is correct |
68 |
Correct |
57 ms |
70508 KB |
Output is correct |
69 |
Correct |
59 ms |
71532 KB |
Output is correct |
70 |
Correct |
63 ms |
71660 KB |
Output is correct |
71 |
Correct |
62 ms |
71404 KB |
Output is correct |
72 |
Correct |
67 ms |
71276 KB |
Output is correct |
73 |
Correct |
70 ms |
71276 KB |
Output is correct |
74 |
Correct |
63 ms |
71404 KB |
Output is correct |
75 |
Correct |
63 ms |
71276 KB |
Output is correct |
76 |
Correct |
63 ms |
71276 KB |
Output is correct |
77 |
Correct |
63 ms |
71276 KB |
Output is correct |
78 |
Correct |
63 ms |
71276 KB |
Output is correct |
79 |
Correct |
66 ms |
71428 KB |
Output is correct |
80 |
Correct |
62 ms |
71276 KB |
Output is correct |
81 |
Correct |
63 ms |
71276 KB |
Output is correct |
82 |
Correct |
63 ms |
71276 KB |
Output is correct |
83 |
Correct |
64 ms |
71276 KB |
Output is correct |
84 |
Correct |
62 ms |
71404 KB |
Output is correct |
85 |
Correct |
62 ms |
71276 KB |
Output is correct |
86 |
Correct |
63 ms |
71276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
53 ms |
70124 KB |
Output is correct |
2 |
Correct |
55 ms |
69996 KB |
Output is correct |
3 |
Correct |
55 ms |
70124 KB |
Output is correct |
4 |
Correct |
54 ms |
70124 KB |
Output is correct |
5 |
Correct |
60 ms |
70252 KB |
Output is correct |
6 |
Correct |
54 ms |
70252 KB |
Output is correct |
7 |
Correct |
59 ms |
70252 KB |
Output is correct |
8 |
Correct |
54 ms |
70124 KB |
Output is correct |
9 |
Correct |
52 ms |
70124 KB |
Output is correct |
10 |
Correct |
62 ms |
70124 KB |
Output is correct |
11 |
Correct |
53 ms |
70252 KB |
Output is correct |
12 |
Correct |
54 ms |
70252 KB |
Output is correct |
13 |
Correct |
53 ms |
70252 KB |
Output is correct |
14 |
Correct |
57 ms |
70252 KB |
Output is correct |
15 |
Correct |
56 ms |
70124 KB |
Output is correct |
16 |
Correct |
54 ms |
70124 KB |
Output is correct |
17 |
Correct |
55 ms |
70124 KB |
Output is correct |
18 |
Correct |
57 ms |
70124 KB |
Output is correct |
19 |
Correct |
67 ms |
70252 KB |
Output is correct |
20 |
Correct |
58 ms |
70252 KB |
Output is correct |
21 |
Correct |
57 ms |
70252 KB |
Output is correct |
22 |
Correct |
61 ms |
70252 KB |
Output is correct |
23 |
Correct |
62 ms |
70252 KB |
Output is correct |
24 |
Correct |
54 ms |
70124 KB |
Output is correct |
25 |
Correct |
54 ms |
70124 KB |
Output is correct |
26 |
Correct |
54 ms |
70124 KB |
Output is correct |
27 |
Correct |
54 ms |
70252 KB |
Output is correct |
28 |
Correct |
54 ms |
70252 KB |
Output is correct |
29 |
Correct |
58 ms |
70252 KB |
Output is correct |
30 |
Correct |
55 ms |
70124 KB |
Output is correct |
31 |
Correct |
54 ms |
70124 KB |
Output is correct |
32 |
Correct |
58 ms |
70252 KB |
Output is correct |
33 |
Correct |
54 ms |
70140 KB |
Output is correct |
34 |
Correct |
61 ms |
70124 KB |
Output is correct |
35 |
Correct |
60 ms |
70252 KB |
Output is correct |
36 |
Correct |
53 ms |
70252 KB |
Output is correct |
37 |
Correct |
60 ms |
70124 KB |
Output is correct |
38 |
Correct |
60 ms |
70400 KB |
Output is correct |
39 |
Correct |
55 ms |
70252 KB |
Output is correct |
40 |
Correct |
65 ms |
70184 KB |
Output is correct |
41 |
Correct |
55 ms |
70124 KB |
Output is correct |
42 |
Correct |
58 ms |
70252 KB |
Output is correct |
43 |
Correct |
54 ms |
70264 KB |
Output is correct |
44 |
Correct |
61 ms |
70332 KB |
Output is correct |
45 |
Correct |
58 ms |
70380 KB |
Output is correct |
46 |
Correct |
65 ms |
71276 KB |
Output is correct |
47 |
Correct |
75 ms |
71276 KB |
Output is correct |
48 |
Correct |
65 ms |
71276 KB |
Output is correct |
49 |
Correct |
56 ms |
70508 KB |
Output is correct |
50 |
Correct |
56 ms |
70508 KB |
Output is correct |
51 |
Correct |
56 ms |
70508 KB |
Output is correct |
52 |
Correct |
58 ms |
71532 KB |
Output is correct |
53 |
Correct |
60 ms |
71532 KB |
Output is correct |
54 |
Correct |
59 ms |
71532 KB |
Output is correct |
55 |
Correct |
59 ms |
71532 KB |
Output is correct |
56 |
Correct |
62 ms |
71404 KB |
Output is correct |
57 |
Correct |
67 ms |
71276 KB |
Output is correct |
58 |
Correct |
71 ms |
71276 KB |
Output is correct |
59 |
Correct |
69 ms |
71276 KB |
Output is correct |
60 |
Correct |
73 ms |
71276 KB |
Output is correct |
61 |
Correct |
62 ms |
71404 KB |
Output is correct |
62 |
Correct |
62 ms |
71404 KB |
Output is correct |
63 |
Correct |
64 ms |
71404 KB |
Output is correct |
64 |
Correct |
65 ms |
71404 KB |
Output is correct |
65 |
Correct |
67 ms |
71276 KB |
Output is correct |
66 |
Correct |
65 ms |
71276 KB |
Output is correct |
67 |
Correct |
64 ms |
71276 KB |
Output is correct |
68 |
Correct |
57 ms |
70508 KB |
Output is correct |
69 |
Correct |
59 ms |
71532 KB |
Output is correct |
70 |
Correct |
63 ms |
71660 KB |
Output is correct |
71 |
Correct |
62 ms |
71404 KB |
Output is correct |
72 |
Correct |
67 ms |
71276 KB |
Output is correct |
73 |
Correct |
70 ms |
71276 KB |
Output is correct |
74 |
Correct |
63 ms |
71404 KB |
Output is correct |
75 |
Correct |
63 ms |
71276 KB |
Output is correct |
76 |
Correct |
63 ms |
71276 KB |
Output is correct |
77 |
Correct |
63 ms |
71276 KB |
Output is correct |
78 |
Correct |
63 ms |
71276 KB |
Output is correct |
79 |
Correct |
66 ms |
71428 KB |
Output is correct |
80 |
Correct |
62 ms |
71276 KB |
Output is correct |
81 |
Correct |
63 ms |
71276 KB |
Output is correct |
82 |
Correct |
63 ms |
71276 KB |
Output is correct |
83 |
Correct |
64 ms |
71276 KB |
Output is correct |
84 |
Correct |
62 ms |
71404 KB |
Output is correct |
85 |
Correct |
62 ms |
71276 KB |
Output is correct |
86 |
Correct |
63 ms |
71276 KB |
Output is correct |
87 |
Correct |
79 ms |
73196 KB |
Output is correct |
88 |
Correct |
133 ms |
79212 KB |
Output is correct |
89 |
Correct |
387 ms |
100588 KB |
Output is correct |
90 |
Correct |
377 ms |
100588 KB |
Output is correct |
91 |
Correct |
387 ms |
100716 KB |
Output is correct |
92 |
Correct |
147 ms |
82432 KB |
Output is correct |
93 |
Correct |
155 ms |
82284 KB |
Output is correct |
94 |
Correct |
181 ms |
82412 KB |
Output is correct |
95 |
Correct |
206 ms |
106604 KB |
Output is correct |
96 |
Correct |
250 ms |
106604 KB |
Output is correct |
97 |
Correct |
247 ms |
106604 KB |
Output is correct |
98 |
Correct |
247 ms |
106604 KB |
Output is correct |
99 |
Correct |
200 ms |
103660 KB |
Output is correct |
100 |
Correct |
478 ms |
93188 KB |
Output is correct |
101 |
Correct |
640 ms |
92972 KB |
Output is correct |
102 |
Correct |
629 ms |
92908 KB |
Output is correct |
103 |
Correct |
646 ms |
93292 KB |
Output is correct |
104 |
Correct |
207 ms |
103660 KB |
Output is correct |
105 |
Correct |
203 ms |
103660 KB |
Output is correct |
106 |
Correct |
228 ms |
103660 KB |
Output is correct |
107 |
Correct |
318 ms |
100588 KB |
Output is correct |
108 |
Correct |
370 ms |
100716 KB |
Output is correct |
109 |
Correct |
408 ms |
100588 KB |
Output is correct |
110 |
Correct |
146 ms |
82412 KB |
Output is correct |
111 |
Correct |
215 ms |
106604 KB |
Output is correct |
112 |
Correct |
208 ms |
104300 KB |
Output is correct |
113 |
Correct |
204 ms |
103788 KB |
Output is correct |
114 |
Correct |
495 ms |
93036 KB |
Output is correct |
115 |
Correct |
635 ms |
93152 KB |
Output is correct |
116 |
Correct |
218 ms |
103748 KB |
Output is correct |
117 |
Correct |
200 ms |
101996 KB |
Output is correct |
118 |
Correct |
205 ms |
101484 KB |
Output is correct |
119 |
Correct |
217 ms |
101228 KB |
Output is correct |
120 |
Correct |
206 ms |
101868 KB |
Output is correct |
121 |
Correct |
208 ms |
101484 KB |
Output is correct |
122 |
Correct |
217 ms |
101356 KB |
Output is correct |
123 |
Correct |
256 ms |
102124 KB |
Output is correct |
124 |
Correct |
226 ms |
101524 KB |
Output is correct |
125 |
Correct |
220 ms |
101228 KB |
Output is correct |
126 |
Correct |
226 ms |
102124 KB |
Output is correct |
127 |
Correct |
270 ms |
101740 KB |
Output is correct |
128 |
Correct |
219 ms |
101228 KB |
Output is correct |