#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 7;
int n;
int c[N];
int a[N], b[N];
vector<int> g[N];
int par[N], sz[N], top[N];
int tin[N], tout[N];
vector<int> order;
int t = 0;
void dfs(int u) {
tin[u] = t++;
order.push_back(u);
for (auto &v : g[u]) {
if (sz[v] > sz[g[u][0]]) {
swap(v, g[u][0]);
}
}
for (auto v : g[u]) {
top[v] = (v == g[u][0] ? top[u] : v);
dfs(v);
}
tout[u] = t;
}
struct Node {
int mn;
int mx;
Node(int val = 0) {
mn = val;
mx = val;
}
};
Node operator + (Node a, Node b) {
Node c;
c.mn = min(a.mn, b.mn);
c.mx = max(a.mx, b.mx);
return c;
}
Node tree[4 * N];
int color[4 * N];
void build(int v, int tl, int tr) {
color[v] = -1;
if (tr - tl == 1) {
tree[v] = Node(c[order[tl]]);
return;
}
int tm = (tl + tr) / 2;
build(2 * v, tl, tm);
build(2 * v + 1, tm, tr);
tree[v] = tree[2 * v] + tree[2 * v + 1];
}
void push(int v) {
if (color[v] == -1) {
return;
}
tree[2 * v] = Node(color[v]);
tree[2 * v + 1] = Node(color[v]);
color[2 * v] = color[v];
color[2 * v + 1] = color[v];
color[v] = -1;
}
void update(int v, int tl, int tr, int l, int r, int c) {
if (tr <= l || tl >= r) {
return;
}
if (l <= tl && tr <= r) {
tree[v] = Node(c);
color[v] = c;
return;
}
push(v);
int tm = (tl + tr) / 2;
update(2 * v, tl, tm, l, r, c);
update(2 * v + 1, tm, tr, l, r, c);
tree[v] = tree[2 * v] + tree[2 * v + 1];
}
int find_prev(int v, int tl, int tr, int x, int y, int val) {
if (tr <= x || tl >= y || (tree[v].mn == val && tree[v].mx == val)) {
return -1;
}
if (tr - tl == 1) {
return tl;
}
push(v);
int tm = (tl + tr) / 2;
int sol = find_prev(2 * v + 1, tm, tr, x, y, val);
if (sol != -1) {
return sol;
}
return find_prev(2 * v, tl, tm, x, y, val);
}
int get_color(int v, int tl, int tr, int pos) {
if (color[v] != -1) {
return color[v];
}
if (tr - tl == 1) {
return c[order[tl]];
}
int tm = (tl + tr) / 2;
if (pos < tm) {
return get_color(2 * v, tl, tm, pos);
}
return get_color(2 * v + 1, tm, tr, pos);
}
int fen[N];
void add(int i, int val) {
for (++i; i < N; i += i & -i) {
fen[i] += val;
}
}
int get(int i) {
int sol = 0;
for (; i >= 1; i -= i & -i) {
sol += fen[i];
}
return sol;
}
ll get_cost(int u) {
vector<pair<int, int>> segs;
while (u != -1) {
int c = get_color(1, 0, n, tin[u]);
int pos = find_prev(1, 0, n, 0, tin[u] + 1, c);
if (pos < tin[top[u]]) {
segs.push_back({c, tin[u] - tin[top[u]] + 1});
u = par[top[u]];
} else {
segs.push_back({c, tin[u] - pos});
u = order[pos];
}
}
ll ans = 0;
for (auto it : segs) {
ans += get(it.first) * 1ll * it.second;
add(it.first, it.second);
}
for (auto it : segs) {
add(it.first, -it.second);
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for (int i = 0; i < n; ++i) {
cin >> c[i];
}
vector<int> sorted;
for (int i = 0; i < n; ++i) {
sorted.push_back(c[i]);
}
sort(sorted.begin(), sorted.end());
for (int i = 0; i < n; ++i) {
c[i] = lower_bound(sorted.begin(), sorted.end(), c[i]) - sorted.begin();
}
for (int i = 0; i < n - 1; ++i) {
cin >> a[i] >> b[i];
--a[i];
--b[i];
}
par[0] = -1;
for (int i = 0; i < n - 1; ++i) {
par[b[i]] = a[i];
g[a[i]].push_back(b[i]);
}
for (int i = 0; i < n; ++i) {
sz[i] = 1;
}
for (int i = n - 1; i >= 0; --i) {
sz[a[i]] += sz[b[i]];
}
dfs(0);
build(1, 0, n);
for (int i = 0; i < n - 1; ++i) {
cout << get_cost(a[i]) << "\n";
for (int u = b[i]; u != -1; u = par[top[u]]) {
update(1, 0, n, tin[top[u]], tin[u] + 1, c[b[i]]);
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10680 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10736 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10676 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10780 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10844 KB |
Output is correct |
15 |
Correct |
3 ms |
10588 KB |
Output is correct |
16 |
Correct |
3 ms |
10588 KB |
Output is correct |
17 |
Correct |
3 ms |
10736 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10988 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10724 KB |
Output is correct |
25 |
Correct |
2 ms |
10584 KB |
Output is correct |
26 |
Correct |
2 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
3 ms |
10588 KB |
Output is correct |
31 |
Correct |
3 ms |
10588 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10588 KB |
Output is correct |
35 |
Correct |
2 ms |
10588 KB |
Output is correct |
36 |
Correct |
2 ms |
10724 KB |
Output is correct |
37 |
Correct |
2 ms |
10588 KB |
Output is correct |
38 |
Correct |
2 ms |
10588 KB |
Output is correct |
39 |
Correct |
2 ms |
10732 KB |
Output is correct |
40 |
Correct |
3 ms |
10676 KB |
Output is correct |
41 |
Correct |
2 ms |
10588 KB |
Output is correct |
42 |
Correct |
3 ms |
10588 KB |
Output is correct |
43 |
Correct |
2 ms |
10588 KB |
Output is correct |
44 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10680 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10736 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10676 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10780 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10844 KB |
Output is correct |
15 |
Correct |
3 ms |
10588 KB |
Output is correct |
16 |
Correct |
3 ms |
10588 KB |
Output is correct |
17 |
Correct |
3 ms |
10736 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10988 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10724 KB |
Output is correct |
25 |
Correct |
2 ms |
10584 KB |
Output is correct |
26 |
Correct |
2 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
3 ms |
10588 KB |
Output is correct |
31 |
Correct |
3 ms |
10588 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10588 KB |
Output is correct |
35 |
Correct |
2 ms |
10588 KB |
Output is correct |
36 |
Correct |
2 ms |
10724 KB |
Output is correct |
37 |
Correct |
2 ms |
10588 KB |
Output is correct |
38 |
Correct |
2 ms |
10588 KB |
Output is correct |
39 |
Correct |
2 ms |
10732 KB |
Output is correct |
40 |
Correct |
3 ms |
10676 KB |
Output is correct |
41 |
Correct |
2 ms |
10588 KB |
Output is correct |
42 |
Correct |
3 ms |
10588 KB |
Output is correct |
43 |
Correct |
2 ms |
10588 KB |
Output is correct |
44 |
Correct |
2 ms |
10588 KB |
Output is correct |
45 |
Correct |
4 ms |
10584 KB |
Output is correct |
46 |
Correct |
11 ms |
10948 KB |
Output is correct |
47 |
Correct |
11 ms |
10840 KB |
Output is correct |
48 |
Correct |
10 ms |
10844 KB |
Output is correct |
49 |
Correct |
5 ms |
11096 KB |
Output is correct |
50 |
Correct |
6 ms |
11100 KB |
Output is correct |
51 |
Correct |
5 ms |
11100 KB |
Output is correct |
52 |
Correct |
6 ms |
11048 KB |
Output is correct |
53 |
Correct |
6 ms |
10988 KB |
Output is correct |
54 |
Correct |
6 ms |
10844 KB |
Output is correct |
55 |
Correct |
6 ms |
10996 KB |
Output is correct |
56 |
Correct |
6 ms |
10844 KB |
Output is correct |
57 |
Correct |
16 ms |
10920 KB |
Output is correct |
58 |
Correct |
16 ms |
10940 KB |
Output is correct |
59 |
Correct |
17 ms |
10840 KB |
Output is correct |
60 |
Correct |
16 ms |
11192 KB |
Output is correct |
61 |
Correct |
7 ms |
10840 KB |
Output is correct |
62 |
Correct |
6 ms |
10840 KB |
Output is correct |
63 |
Correct |
6 ms |
10988 KB |
Output is correct |
64 |
Correct |
11 ms |
11000 KB |
Output is correct |
65 |
Correct |
10 ms |
10844 KB |
Output is correct |
66 |
Correct |
10 ms |
10916 KB |
Output is correct |
67 |
Correct |
10 ms |
10840 KB |
Output is correct |
68 |
Correct |
5 ms |
11100 KB |
Output is correct |
69 |
Correct |
5 ms |
11036 KB |
Output is correct |
70 |
Correct |
5 ms |
10844 KB |
Output is correct |
71 |
Correct |
5 ms |
10840 KB |
Output is correct |
72 |
Correct |
18 ms |
10840 KB |
Output is correct |
73 |
Correct |
16 ms |
10904 KB |
Output is correct |
74 |
Correct |
6 ms |
10840 KB |
Output is correct |
75 |
Correct |
9 ms |
10840 KB |
Output is correct |
76 |
Correct |
7 ms |
10824 KB |
Output is correct |
77 |
Correct |
7 ms |
10840 KB |
Output is correct |
78 |
Correct |
6 ms |
10844 KB |
Output is correct |
79 |
Correct |
6 ms |
10956 KB |
Output is correct |
80 |
Correct |
6 ms |
10844 KB |
Output is correct |
81 |
Correct |
7 ms |
10844 KB |
Output is correct |
82 |
Correct |
7 ms |
10844 KB |
Output is correct |
83 |
Correct |
7 ms |
10844 KB |
Output is correct |
84 |
Correct |
6 ms |
10732 KB |
Output is correct |
85 |
Correct |
7 ms |
10844 KB |
Output is correct |
86 |
Correct |
7 ms |
10840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10680 KB |
Output is correct |
4 |
Correct |
2 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10736 KB |
Output is correct |
6 |
Correct |
3 ms |
10588 KB |
Output is correct |
7 |
Correct |
3 ms |
10676 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10780 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
3 ms |
10844 KB |
Output is correct |
15 |
Correct |
3 ms |
10588 KB |
Output is correct |
16 |
Correct |
3 ms |
10588 KB |
Output is correct |
17 |
Correct |
3 ms |
10736 KB |
Output is correct |
18 |
Correct |
3 ms |
10588 KB |
Output is correct |
19 |
Correct |
2 ms |
10988 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
10588 KB |
Output is correct |
23 |
Correct |
3 ms |
10588 KB |
Output is correct |
24 |
Correct |
3 ms |
10724 KB |
Output is correct |
25 |
Correct |
2 ms |
10584 KB |
Output is correct |
26 |
Correct |
2 ms |
10588 KB |
Output is correct |
27 |
Correct |
3 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
10588 KB |
Output is correct |
29 |
Correct |
2 ms |
10588 KB |
Output is correct |
30 |
Correct |
3 ms |
10588 KB |
Output is correct |
31 |
Correct |
3 ms |
10588 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
33 |
Correct |
2 ms |
10588 KB |
Output is correct |
34 |
Correct |
2 ms |
10588 KB |
Output is correct |
35 |
Correct |
2 ms |
10588 KB |
Output is correct |
36 |
Correct |
2 ms |
10724 KB |
Output is correct |
37 |
Correct |
2 ms |
10588 KB |
Output is correct |
38 |
Correct |
2 ms |
10588 KB |
Output is correct |
39 |
Correct |
2 ms |
10732 KB |
Output is correct |
40 |
Correct |
3 ms |
10676 KB |
Output is correct |
41 |
Correct |
2 ms |
10588 KB |
Output is correct |
42 |
Correct |
3 ms |
10588 KB |
Output is correct |
43 |
Correct |
2 ms |
10588 KB |
Output is correct |
44 |
Correct |
2 ms |
10588 KB |
Output is correct |
45 |
Correct |
4 ms |
10584 KB |
Output is correct |
46 |
Correct |
11 ms |
10948 KB |
Output is correct |
47 |
Correct |
11 ms |
10840 KB |
Output is correct |
48 |
Correct |
10 ms |
10844 KB |
Output is correct |
49 |
Correct |
5 ms |
11096 KB |
Output is correct |
50 |
Correct |
6 ms |
11100 KB |
Output is correct |
51 |
Correct |
5 ms |
11100 KB |
Output is correct |
52 |
Correct |
6 ms |
11048 KB |
Output is correct |
53 |
Correct |
6 ms |
10988 KB |
Output is correct |
54 |
Correct |
6 ms |
10844 KB |
Output is correct |
55 |
Correct |
6 ms |
10996 KB |
Output is correct |
56 |
Correct |
6 ms |
10844 KB |
Output is correct |
57 |
Correct |
16 ms |
10920 KB |
Output is correct |
58 |
Correct |
16 ms |
10940 KB |
Output is correct |
59 |
Correct |
17 ms |
10840 KB |
Output is correct |
60 |
Correct |
16 ms |
11192 KB |
Output is correct |
61 |
Correct |
7 ms |
10840 KB |
Output is correct |
62 |
Correct |
6 ms |
10840 KB |
Output is correct |
63 |
Correct |
6 ms |
10988 KB |
Output is correct |
64 |
Correct |
11 ms |
11000 KB |
Output is correct |
65 |
Correct |
10 ms |
10844 KB |
Output is correct |
66 |
Correct |
10 ms |
10916 KB |
Output is correct |
67 |
Correct |
10 ms |
10840 KB |
Output is correct |
68 |
Correct |
5 ms |
11100 KB |
Output is correct |
69 |
Correct |
5 ms |
11036 KB |
Output is correct |
70 |
Correct |
5 ms |
10844 KB |
Output is correct |
71 |
Correct |
5 ms |
10840 KB |
Output is correct |
72 |
Correct |
18 ms |
10840 KB |
Output is correct |
73 |
Correct |
16 ms |
10904 KB |
Output is correct |
74 |
Correct |
6 ms |
10840 KB |
Output is correct |
75 |
Correct |
9 ms |
10840 KB |
Output is correct |
76 |
Correct |
7 ms |
10824 KB |
Output is correct |
77 |
Correct |
7 ms |
10840 KB |
Output is correct |
78 |
Correct |
6 ms |
10844 KB |
Output is correct |
79 |
Correct |
6 ms |
10956 KB |
Output is correct |
80 |
Correct |
6 ms |
10844 KB |
Output is correct |
81 |
Correct |
7 ms |
10844 KB |
Output is correct |
82 |
Correct |
7 ms |
10844 KB |
Output is correct |
83 |
Correct |
7 ms |
10844 KB |
Output is correct |
84 |
Correct |
6 ms |
10732 KB |
Output is correct |
85 |
Correct |
7 ms |
10844 KB |
Output is correct |
86 |
Correct |
7 ms |
10840 KB |
Output is correct |
87 |
Correct |
31 ms |
11292 KB |
Output is correct |
88 |
Correct |
106 ms |
12352 KB |
Output is correct |
89 |
Correct |
385 ms |
15752 KB |
Output is correct |
90 |
Correct |
399 ms |
15712 KB |
Output is correct |
91 |
Correct |
427 ms |
15896 KB |
Output is correct |
92 |
Correct |
103 ms |
23456 KB |
Output is correct |
93 |
Correct |
100 ms |
23396 KB |
Output is correct |
94 |
Correct |
101 ms |
23468 KB |
Output is correct |
95 |
Correct |
112 ms |
19132 KB |
Output is correct |
96 |
Correct |
117 ms |
19540 KB |
Output is correct |
97 |
Correct |
147 ms |
19540 KB |
Output is correct |
98 |
Correct |
116 ms |
19536 KB |
Output is correct |
99 |
Correct |
130 ms |
19136 KB |
Output is correct |
100 |
Correct |
598 ms |
16060 KB |
Output is correct |
101 |
Correct |
655 ms |
16400 KB |
Output is correct |
102 |
Correct |
685 ms |
16432 KB |
Output is correct |
103 |
Correct |
645 ms |
16224 KB |
Output is correct |
104 |
Correct |
135 ms |
18884 KB |
Output is correct |
105 |
Correct |
135 ms |
19020 KB |
Output is correct |
106 |
Correct |
136 ms |
19028 KB |
Output is correct |
107 |
Correct |
376 ms |
14984 KB |
Output is correct |
108 |
Correct |
386 ms |
15336 KB |
Output is correct |
109 |
Correct |
378 ms |
15528 KB |
Output is correct |
110 |
Correct |
109 ms |
22872 KB |
Output is correct |
111 |
Correct |
110 ms |
19028 KB |
Output is correct |
112 |
Correct |
113 ms |
18772 KB |
Output is correct |
113 |
Correct |
113 ms |
18256 KB |
Output is correct |
114 |
Correct |
606 ms |
15840 KB |
Output is correct |
115 |
Correct |
650 ms |
15856 KB |
Output is correct |
116 |
Correct |
146 ms |
18340 KB |
Output is correct |
117 |
Correct |
143 ms |
17128 KB |
Output is correct |
118 |
Correct |
157 ms |
16428 KB |
Output is correct |
119 |
Correct |
165 ms |
16212 KB |
Output is correct |
120 |
Correct |
136 ms |
16700 KB |
Output is correct |
121 |
Correct |
154 ms |
16280 KB |
Output is correct |
122 |
Correct |
149 ms |
16068 KB |
Output is correct |
123 |
Correct |
161 ms |
16980 KB |
Output is correct |
124 |
Correct |
206 ms |
16568 KB |
Output is correct |
125 |
Correct |
196 ms |
16340 KB |
Output is correct |
126 |
Correct |
160 ms |
16680 KB |
Output is correct |
127 |
Correct |
165 ms |
16332 KB |
Output is correct |
128 |
Correct |
174 ms |
15948 KB |
Output is correct |