#include <bits/stdc++.h>
using i64 = long long;
constexpr int N = 1E5 + 5;
int a[N + 1];
void add(int x, int y) {
for (int i = x + 1; i <= N; i += i & -i) {
a[i] += y;
}
}
int sum(int x) {
int res = 0;
for (int i = x; i > 0; i -= i & -i) {
res += a[i];
}
return res;
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
}
auto f = a;
std::sort(f.begin(), f.end());
f.erase(std::unique(f.begin(), f.end()), f.end());
int m = f.size();
for (int i = 0; i < n; i++) {
a[i] = std::lower_bound(f.begin(), f.end(), a[i]) - f.begin();
}
std::vector<int> p(n, -1);
std::vector<std::vector<int>> adj(n);
std::vector<std::pair<int, int>> E;
for (int _ = 0; _ < n - 1; _++) {
int A, B;
std::cin >> A >> B;
A--;
B--;
adj[A].push_back(B);
p[B] = A;
E.emplace_back(A, B);
}
std::vector<int> siz(n, 1), pos(n), head(n);
std::function<void(int)> dfs = [&](int u) {
for (int &v : adj[u]) {
dfs(v);
if (siz[v] > siz[adj[u][0]]) {
std::swap(v, adj[u][0]);
}
siz[u] += siz[v];
}
};
dfs(0);
int timeStamp = 0;
std::function<void(int, int)> dfs2 = [&](int u, int h) {
pos[u] = timeStamp++;
head[u] = h;
for (int v : adj[u]) {
if (v == adj[u][0]) {
dfs2(v, h);
} else {
dfs2(v, v);
}
}
};
dfs2(0, 0);
std::vector<std::set<std::pair<int, int>>> s(n);
for (int i = 0; i < n; i++) {
s[head[i]].insert({pos[i], a[i]});
}
for (auto [A, B] : E) {
int u = A;
std::vector<std::pair<int, int>> v;
while (u != -1) {
int h = head[u];
std::vector<std::pair<int, int>> cur;
int last = pos[h] - 1;
while (true) {
assert(!s[h].empty());
auto [r, c] = *s[h].begin();
if (r <= pos[u]) {
cur.push_back({c, r - last});
s[h].erase(s[h].begin());
if (r == pos[u]) {
break;
}
} else {
cur.push_back({c, pos[u] - last});
break;
}
last = r;
}
s[h].insert({pos[u], a[B]});
std::reverse(cur.begin(), cur.end());
v.insert(v.end(), cur.begin(), cur.end());
u = p[h];
}
std::reverse(v.begin(), v.end());
i64 ans = 0;
for (auto [x, y] : v) {
ans += y * 1LL * (sum(m) - sum(x + 1));
add(x, y);
}
for (auto [x, y] : v) {
add(x, -y);
}
std::cout << ans << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
456 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
328 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
440 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
456 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
328 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
440 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
6 ms |
1108 KB |
Output is correct |
47 |
Correct |
6 ms |
1104 KB |
Output is correct |
48 |
Correct |
6 ms |
1060 KB |
Output is correct |
49 |
Correct |
5 ms |
1492 KB |
Output is correct |
50 |
Correct |
5 ms |
1492 KB |
Output is correct |
51 |
Correct |
5 ms |
1492 KB |
Output is correct |
52 |
Correct |
4 ms |
1236 KB |
Output is correct |
53 |
Correct |
5 ms |
1236 KB |
Output is correct |
54 |
Correct |
5 ms |
1308 KB |
Output is correct |
55 |
Correct |
4 ms |
1232 KB |
Output is correct |
56 |
Correct |
4 ms |
1236 KB |
Output is correct |
57 |
Correct |
8 ms |
980 KB |
Output is correct |
58 |
Correct |
8 ms |
1108 KB |
Output is correct |
59 |
Correct |
9 ms |
1056 KB |
Output is correct |
60 |
Correct |
11 ms |
1000 KB |
Output is correct |
61 |
Correct |
4 ms |
1236 KB |
Output is correct |
62 |
Correct |
5 ms |
1228 KB |
Output is correct |
63 |
Correct |
5 ms |
1232 KB |
Output is correct |
64 |
Correct |
6 ms |
980 KB |
Output is correct |
65 |
Correct |
6 ms |
1032 KB |
Output is correct |
66 |
Correct |
7 ms |
1032 KB |
Output is correct |
67 |
Correct |
7 ms |
1096 KB |
Output is correct |
68 |
Correct |
5 ms |
1416 KB |
Output is correct |
69 |
Correct |
5 ms |
1236 KB |
Output is correct |
70 |
Correct |
4 ms |
1236 KB |
Output is correct |
71 |
Correct |
4 ms |
1160 KB |
Output is correct |
72 |
Correct |
9 ms |
980 KB |
Output is correct |
73 |
Correct |
8 ms |
980 KB |
Output is correct |
74 |
Correct |
4 ms |
1232 KB |
Output is correct |
75 |
Correct |
5 ms |
1108 KB |
Output is correct |
76 |
Correct |
4 ms |
1108 KB |
Output is correct |
77 |
Correct |
5 ms |
1108 KB |
Output is correct |
78 |
Correct |
4 ms |
1108 KB |
Output is correct |
79 |
Correct |
4 ms |
1108 KB |
Output is correct |
80 |
Correct |
4 ms |
1108 KB |
Output is correct |
81 |
Correct |
5 ms |
1236 KB |
Output is correct |
82 |
Correct |
5 ms |
1048 KB |
Output is correct |
83 |
Correct |
5 ms |
1108 KB |
Output is correct |
84 |
Correct |
4 ms |
1156 KB |
Output is correct |
85 |
Correct |
4 ms |
1108 KB |
Output is correct |
86 |
Correct |
6 ms |
1108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
2 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
464 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
456 KB |
Output is correct |
27 |
Correct |
1 ms |
456 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
328 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
440 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
6 ms |
1108 KB |
Output is correct |
47 |
Correct |
6 ms |
1104 KB |
Output is correct |
48 |
Correct |
6 ms |
1060 KB |
Output is correct |
49 |
Correct |
5 ms |
1492 KB |
Output is correct |
50 |
Correct |
5 ms |
1492 KB |
Output is correct |
51 |
Correct |
5 ms |
1492 KB |
Output is correct |
52 |
Correct |
4 ms |
1236 KB |
Output is correct |
53 |
Correct |
5 ms |
1236 KB |
Output is correct |
54 |
Correct |
5 ms |
1308 KB |
Output is correct |
55 |
Correct |
4 ms |
1232 KB |
Output is correct |
56 |
Correct |
4 ms |
1236 KB |
Output is correct |
57 |
Correct |
8 ms |
980 KB |
Output is correct |
58 |
Correct |
8 ms |
1108 KB |
Output is correct |
59 |
Correct |
9 ms |
1056 KB |
Output is correct |
60 |
Correct |
11 ms |
1000 KB |
Output is correct |
61 |
Correct |
4 ms |
1236 KB |
Output is correct |
62 |
Correct |
5 ms |
1228 KB |
Output is correct |
63 |
Correct |
5 ms |
1232 KB |
Output is correct |
64 |
Correct |
6 ms |
980 KB |
Output is correct |
65 |
Correct |
6 ms |
1032 KB |
Output is correct |
66 |
Correct |
7 ms |
1032 KB |
Output is correct |
67 |
Correct |
7 ms |
1096 KB |
Output is correct |
68 |
Correct |
5 ms |
1416 KB |
Output is correct |
69 |
Correct |
5 ms |
1236 KB |
Output is correct |
70 |
Correct |
4 ms |
1236 KB |
Output is correct |
71 |
Correct |
4 ms |
1160 KB |
Output is correct |
72 |
Correct |
9 ms |
980 KB |
Output is correct |
73 |
Correct |
8 ms |
980 KB |
Output is correct |
74 |
Correct |
4 ms |
1232 KB |
Output is correct |
75 |
Correct |
5 ms |
1108 KB |
Output is correct |
76 |
Correct |
4 ms |
1108 KB |
Output is correct |
77 |
Correct |
5 ms |
1108 KB |
Output is correct |
78 |
Correct |
4 ms |
1108 KB |
Output is correct |
79 |
Correct |
4 ms |
1108 KB |
Output is correct |
80 |
Correct |
4 ms |
1108 KB |
Output is correct |
81 |
Correct |
5 ms |
1236 KB |
Output is correct |
82 |
Correct |
5 ms |
1048 KB |
Output is correct |
83 |
Correct |
5 ms |
1108 KB |
Output is correct |
84 |
Correct |
4 ms |
1156 KB |
Output is correct |
85 |
Correct |
4 ms |
1108 KB |
Output is correct |
86 |
Correct |
6 ms |
1108 KB |
Output is correct |
87 |
Correct |
19 ms |
2260 KB |
Output is correct |
88 |
Correct |
71 ms |
6036 KB |
Output is correct |
89 |
Correct |
228 ms |
19524 KB |
Output is correct |
90 |
Correct |
237 ms |
19520 KB |
Output is correct |
91 |
Correct |
260 ms |
19604 KB |
Output is correct |
92 |
Correct |
135 ms |
30384 KB |
Output is correct |
93 |
Correct |
134 ms |
30316 KB |
Output is correct |
94 |
Correct |
153 ms |
30412 KB |
Output is correct |
95 |
Correct |
111 ms |
24636 KB |
Output is correct |
96 |
Correct |
126 ms |
25044 KB |
Output is correct |
97 |
Correct |
124 ms |
25088 KB |
Output is correct |
98 |
Correct |
119 ms |
25048 KB |
Output is correct |
99 |
Correct |
121 ms |
24260 KB |
Output is correct |
100 |
Correct |
313 ms |
19640 KB |
Output is correct |
101 |
Correct |
428 ms |
20032 KB |
Output is correct |
102 |
Correct |
397 ms |
20036 KB |
Output is correct |
103 |
Correct |
385 ms |
20020 KB |
Output is correct |
104 |
Correct |
125 ms |
24248 KB |
Output is correct |
105 |
Correct |
125 ms |
24372 KB |
Output is correct |
106 |
Correct |
155 ms |
24236 KB |
Output is correct |
107 |
Correct |
197 ms |
18368 KB |
Output is correct |
108 |
Correct |
209 ms |
18444 KB |
Output is correct |
109 |
Correct |
261 ms |
18868 KB |
Output is correct |
110 |
Correct |
115 ms |
29380 KB |
Output is correct |
111 |
Correct |
123 ms |
24664 KB |
Output is correct |
112 |
Correct |
129 ms |
24056 KB |
Output is correct |
113 |
Correct |
124 ms |
23236 KB |
Output is correct |
114 |
Correct |
327 ms |
19716 KB |
Output is correct |
115 |
Correct |
336 ms |
19004 KB |
Output is correct |
116 |
Correct |
112 ms |
23308 KB |
Output is correct |
117 |
Correct |
117 ms |
21580 KB |
Output is correct |
118 |
Correct |
130 ms |
20836 KB |
Output is correct |
119 |
Correct |
118 ms |
20428 KB |
Output is correct |
120 |
Correct |
106 ms |
20820 KB |
Output is correct |
121 |
Correct |
108 ms |
20160 KB |
Output is correct |
122 |
Correct |
132 ms |
19648 KB |
Output is correct |
123 |
Correct |
133 ms |
21600 KB |
Output is correct |
124 |
Correct |
142 ms |
20888 KB |
Output is correct |
125 |
Correct |
167 ms |
20392 KB |
Output is correct |
126 |
Correct |
124 ms |
20924 KB |
Output is correct |
127 |
Correct |
117 ms |
20220 KB |
Output is correct |
128 |
Correct |
121 ms |
19768 KB |
Output is correct |