#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1e5 + 7;
int n;
int c[N];
int a[N], b[N];
vector<int> g[N];
int sz[N];
vector<int> order;
int tin[N], tout[N];
int top[N];
int par[N];
void dfs(int u) {
tin[u] = order.size();
order.push_back(u);
for (int &v : g[u]) {
if (sz[v] > sz[g[u][0]])
swap(v, g[u][0]);
}
for (int v : g[u]) {
top[v] = v == g[u][0] ? top[u] : v;
dfs(v);
}
tout[u] = order.size();
}
int mn[4 * N], mx[4 * N], color[4 * N];
void apply(int v, int c) {
mn[v] = c;
mx[v] = c;
color[v] = c;
}
void push(int v) {
if (color[v] == -1)
return;
apply(2 * v, color[v]);
apply(2 * v + 1, color[v]);
color[v] = -1;
}
void update(int v, int left, int right, int l, int r, int c) {
if (left >= r || right <= l)
return;
if (left >= l && right <= r) {
apply(v, c);
return;
}
push(v);
int mid = (left + right) / 2;
update(2 * v, left, mid, l, r, c);
update(2 * v + 1, mid, right, l, r, c);
mn[v] = min(mn[2 * v], mn[2 * v + 1]);
mx[v] = max(mx[2 * v], mx[2 * v + 1]);
}
int get_color(int v, int left, int right, int pos) {
if (color[v] != -1)
return color[v];
if (right - left == 1)
return c[order[left]];
int mid = (left + right) / 2;
if (pos < mid)
return get_color(2 * v, left, mid, pos);
return get_color(2 * v + 1, mid, right, pos);
}
int find_prev(int v, int left, int right, int l, int r, int c) {
if (left >= r || right <= l || (mn[v] == c && mx[v] == c))
return -1;
if (right - left == 1)
return left;
push(v);
int mid = (left + right) / 2;
int res = find_prev(2 * v + 1, mid, right, l, r, c);
if (res == -1)
res = find_prev(2 * v, left, mid, l, r, c);
return res;
}
void build(int v, int left, int right) {
color[v] = -1;
if (right - left == 1) {
mn[v] = mx[v] = c[order[left]];
return;
}
int mid = (left + right) / 2;
build(2 * v, left, mid);
build(2 * v + 1, mid, right);
mn[v] = min(mn[2 * v], mn[2 * v + 1]);
mx[v] = max(mx[2 * v], mx[2 * v + 1]);
}
int fen[N];
void add_fen(int i, int val) {
for (++i; i < N; i += i & -i) {
fen[i] += val;
}
}
int get_fen(int i) {
int sum = 0;
for (; i > 0; i -= i & -i) {
sum += fen[i];
}
return sum;
}
long long get_cost(int u) {
vector<pair<int, int>> segments;
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]]) {
segments.push_back({c, tin[u] - tin[top[u]] + 1});
u = par[top[u]];
} else {
segments.push_back({c, tin[u] - pos});
u = order[pos];
}
}
long long res = 0;
for (auto segment : segments) {
// cout << segment.first << " " << segment.second << endl;
res += get_fen(segment.first) * 1LL * segment.second;
add_fen(segment.first, segment.second);
}
for (auto segment : segments) {
add_fen(segment.first, -segment.second);
}
return res;
}
int main() {
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 = 0; u < n; ++u) {
// cout << get_color(1, 0, n, tin[u]) << " ";
// }
// cout << endl;
for (int u = b[i]; u != -1; u = par[top[u]]) {
update(1, 0, n, tin[top[u]], tin[u] + 1, c[b[i]]);
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2664 KB |
Output is correct |
4 |
Correct |
2 ms |
2672 KB |
Output is correct |
5 |
Correct |
3 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2808 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2804 KB |
Output is correct |
12 |
Correct |
3 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2804 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2800 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2800 KB |
Output is correct |
29 |
Correct |
2 ms |
2808 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
2772 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
2772 KB |
Output is correct |
34 |
Correct |
2 ms |
2772 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
2 ms |
2772 KB |
Output is correct |
37 |
Correct |
2 ms |
2772 KB |
Output is correct |
38 |
Correct |
3 ms |
2800 KB |
Output is correct |
39 |
Correct |
2 ms |
2800 KB |
Output is correct |
40 |
Correct |
3 ms |
2804 KB |
Output is correct |
41 |
Correct |
3 ms |
2772 KB |
Output is correct |
42 |
Correct |
3 ms |
2800 KB |
Output is correct |
43 |
Correct |
2 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2664 KB |
Output is correct |
4 |
Correct |
2 ms |
2672 KB |
Output is correct |
5 |
Correct |
3 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2808 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2804 KB |
Output is correct |
12 |
Correct |
3 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2804 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2800 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2800 KB |
Output is correct |
29 |
Correct |
2 ms |
2808 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
2772 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
2772 KB |
Output is correct |
34 |
Correct |
2 ms |
2772 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
2 ms |
2772 KB |
Output is correct |
37 |
Correct |
2 ms |
2772 KB |
Output is correct |
38 |
Correct |
3 ms |
2800 KB |
Output is correct |
39 |
Correct |
2 ms |
2800 KB |
Output is correct |
40 |
Correct |
3 ms |
2804 KB |
Output is correct |
41 |
Correct |
3 ms |
2772 KB |
Output is correct |
42 |
Correct |
3 ms |
2800 KB |
Output is correct |
43 |
Correct |
2 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2800 KB |
Output is correct |
45 |
Correct |
4 ms |
2772 KB |
Output is correct |
46 |
Correct |
14 ms |
3160 KB |
Output is correct |
47 |
Correct |
15 ms |
3156 KB |
Output is correct |
48 |
Correct |
14 ms |
3128 KB |
Output is correct |
49 |
Correct |
8 ms |
3540 KB |
Output is correct |
50 |
Correct |
8 ms |
3412 KB |
Output is correct |
51 |
Correct |
7 ms |
3412 KB |
Output is correct |
52 |
Correct |
8 ms |
3284 KB |
Output is correct |
53 |
Correct |
8 ms |
3284 KB |
Output is correct |
54 |
Correct |
9 ms |
3308 KB |
Output is correct |
55 |
Correct |
8 ms |
3192 KB |
Output is correct |
56 |
Correct |
8 ms |
3284 KB |
Output is correct |
57 |
Correct |
19 ms |
3128 KB |
Output is correct |
58 |
Correct |
20 ms |
3160 KB |
Output is correct |
59 |
Correct |
20 ms |
3156 KB |
Output is correct |
60 |
Correct |
20 ms |
3132 KB |
Output is correct |
61 |
Correct |
9 ms |
3284 KB |
Output is correct |
62 |
Correct |
9 ms |
3284 KB |
Output is correct |
63 |
Correct |
9 ms |
3284 KB |
Output is correct |
64 |
Correct |
12 ms |
3064 KB |
Output is correct |
65 |
Correct |
12 ms |
3132 KB |
Output is correct |
66 |
Correct |
15 ms |
3144 KB |
Output is correct |
67 |
Correct |
13 ms |
3028 KB |
Output is correct |
68 |
Correct |
7 ms |
3412 KB |
Output is correct |
69 |
Correct |
9 ms |
3284 KB |
Output is correct |
70 |
Correct |
7 ms |
3192 KB |
Output is correct |
71 |
Correct |
8 ms |
3156 KB |
Output is correct |
72 |
Correct |
19 ms |
3184 KB |
Output is correct |
73 |
Correct |
21 ms |
3064 KB |
Output is correct |
74 |
Correct |
7 ms |
3180 KB |
Output is correct |
75 |
Correct |
9 ms |
3120 KB |
Output is correct |
76 |
Correct |
9 ms |
3160 KB |
Output is correct |
77 |
Correct |
12 ms |
3172 KB |
Output is correct |
78 |
Correct |
8 ms |
3184 KB |
Output is correct |
79 |
Correct |
8 ms |
3180 KB |
Output is correct |
80 |
Correct |
9 ms |
3156 KB |
Output is correct |
81 |
Correct |
10 ms |
3204 KB |
Output is correct |
82 |
Correct |
10 ms |
3156 KB |
Output is correct |
83 |
Correct |
10 ms |
3176 KB |
Output is correct |
84 |
Correct |
8 ms |
3192 KB |
Output is correct |
85 |
Correct |
9 ms |
3156 KB |
Output is correct |
86 |
Correct |
9 ms |
3160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
1 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2664 KB |
Output is correct |
4 |
Correct |
2 ms |
2672 KB |
Output is correct |
5 |
Correct |
3 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
3 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2808 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2772 KB |
Output is correct |
11 |
Correct |
2 ms |
2804 KB |
Output is correct |
12 |
Correct |
3 ms |
2772 KB |
Output is correct |
13 |
Correct |
2 ms |
2804 KB |
Output is correct |
14 |
Correct |
2 ms |
2772 KB |
Output is correct |
15 |
Correct |
3 ms |
2772 KB |
Output is correct |
16 |
Correct |
3 ms |
2772 KB |
Output is correct |
17 |
Correct |
3 ms |
2772 KB |
Output is correct |
18 |
Correct |
3 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2772 KB |
Output is correct |
21 |
Correct |
2 ms |
2772 KB |
Output is correct |
22 |
Correct |
2 ms |
2772 KB |
Output is correct |
23 |
Correct |
2 ms |
2772 KB |
Output is correct |
24 |
Correct |
2 ms |
2772 KB |
Output is correct |
25 |
Correct |
2 ms |
2800 KB |
Output is correct |
26 |
Correct |
2 ms |
2772 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2800 KB |
Output is correct |
29 |
Correct |
2 ms |
2808 KB |
Output is correct |
30 |
Correct |
3 ms |
2772 KB |
Output is correct |
31 |
Correct |
3 ms |
2772 KB |
Output is correct |
32 |
Correct |
2 ms |
2772 KB |
Output is correct |
33 |
Correct |
2 ms |
2772 KB |
Output is correct |
34 |
Correct |
2 ms |
2772 KB |
Output is correct |
35 |
Correct |
2 ms |
2772 KB |
Output is correct |
36 |
Correct |
2 ms |
2772 KB |
Output is correct |
37 |
Correct |
2 ms |
2772 KB |
Output is correct |
38 |
Correct |
3 ms |
2800 KB |
Output is correct |
39 |
Correct |
2 ms |
2800 KB |
Output is correct |
40 |
Correct |
3 ms |
2804 KB |
Output is correct |
41 |
Correct |
3 ms |
2772 KB |
Output is correct |
42 |
Correct |
3 ms |
2800 KB |
Output is correct |
43 |
Correct |
2 ms |
2772 KB |
Output is correct |
44 |
Correct |
2 ms |
2800 KB |
Output is correct |
45 |
Correct |
4 ms |
2772 KB |
Output is correct |
46 |
Correct |
14 ms |
3160 KB |
Output is correct |
47 |
Correct |
15 ms |
3156 KB |
Output is correct |
48 |
Correct |
14 ms |
3128 KB |
Output is correct |
49 |
Correct |
8 ms |
3540 KB |
Output is correct |
50 |
Correct |
8 ms |
3412 KB |
Output is correct |
51 |
Correct |
7 ms |
3412 KB |
Output is correct |
52 |
Correct |
8 ms |
3284 KB |
Output is correct |
53 |
Correct |
8 ms |
3284 KB |
Output is correct |
54 |
Correct |
9 ms |
3308 KB |
Output is correct |
55 |
Correct |
8 ms |
3192 KB |
Output is correct |
56 |
Correct |
8 ms |
3284 KB |
Output is correct |
57 |
Correct |
19 ms |
3128 KB |
Output is correct |
58 |
Correct |
20 ms |
3160 KB |
Output is correct |
59 |
Correct |
20 ms |
3156 KB |
Output is correct |
60 |
Correct |
20 ms |
3132 KB |
Output is correct |
61 |
Correct |
9 ms |
3284 KB |
Output is correct |
62 |
Correct |
9 ms |
3284 KB |
Output is correct |
63 |
Correct |
9 ms |
3284 KB |
Output is correct |
64 |
Correct |
12 ms |
3064 KB |
Output is correct |
65 |
Correct |
12 ms |
3132 KB |
Output is correct |
66 |
Correct |
15 ms |
3144 KB |
Output is correct |
67 |
Correct |
13 ms |
3028 KB |
Output is correct |
68 |
Correct |
7 ms |
3412 KB |
Output is correct |
69 |
Correct |
9 ms |
3284 KB |
Output is correct |
70 |
Correct |
7 ms |
3192 KB |
Output is correct |
71 |
Correct |
8 ms |
3156 KB |
Output is correct |
72 |
Correct |
19 ms |
3184 KB |
Output is correct |
73 |
Correct |
21 ms |
3064 KB |
Output is correct |
74 |
Correct |
7 ms |
3180 KB |
Output is correct |
75 |
Correct |
9 ms |
3120 KB |
Output is correct |
76 |
Correct |
9 ms |
3160 KB |
Output is correct |
77 |
Correct |
12 ms |
3172 KB |
Output is correct |
78 |
Correct |
8 ms |
3184 KB |
Output is correct |
79 |
Correct |
8 ms |
3180 KB |
Output is correct |
80 |
Correct |
9 ms |
3156 KB |
Output is correct |
81 |
Correct |
10 ms |
3204 KB |
Output is correct |
82 |
Correct |
10 ms |
3156 KB |
Output is correct |
83 |
Correct |
10 ms |
3176 KB |
Output is correct |
84 |
Correct |
8 ms |
3192 KB |
Output is correct |
85 |
Correct |
9 ms |
3156 KB |
Output is correct |
86 |
Correct |
9 ms |
3160 KB |
Output is correct |
87 |
Correct |
49 ms |
3952 KB |
Output is correct |
88 |
Correct |
126 ms |
6104 KB |
Output is correct |
89 |
Correct |
501 ms |
14236 KB |
Output is correct |
90 |
Correct |
498 ms |
14184 KB |
Output is correct |
91 |
Correct |
512 ms |
14196 KB |
Output is correct |
92 |
Correct |
180 ms |
21868 KB |
Output is correct |
93 |
Correct |
178 ms |
21880 KB |
Output is correct |
94 |
Correct |
179 ms |
21776 KB |
Output is correct |
95 |
Correct |
183 ms |
17440 KB |
Output is correct |
96 |
Correct |
203 ms |
17728 KB |
Output is correct |
97 |
Correct |
197 ms |
17708 KB |
Output is correct |
98 |
Correct |
199 ms |
17764 KB |
Output is correct |
99 |
Correct |
200 ms |
17316 KB |
Output is correct |
100 |
Correct |
729 ms |
14388 KB |
Output is correct |
101 |
Correct |
779 ms |
14684 KB |
Output is correct |
102 |
Correct |
813 ms |
14672 KB |
Output is correct |
103 |
Correct |
799 ms |
14700 KB |
Output is correct |
104 |
Correct |
227 ms |
17264 KB |
Output is correct |
105 |
Correct |
221 ms |
17224 KB |
Output is correct |
106 |
Correct |
224 ms |
17256 KB |
Output is correct |
107 |
Correct |
427 ms |
13144 KB |
Output is correct |
108 |
Correct |
486 ms |
13500 KB |
Output is correct |
109 |
Correct |
511 ms |
13724 KB |
Output is correct |
110 |
Correct |
159 ms |
21224 KB |
Output is correct |
111 |
Correct |
182 ms |
17472 KB |
Output is correct |
112 |
Correct |
185 ms |
17224 KB |
Output is correct |
113 |
Correct |
178 ms |
16668 KB |
Output is correct |
114 |
Correct |
736 ms |
14260 KB |
Output is correct |
115 |
Correct |
777 ms |
14024 KB |
Output is correct |
116 |
Correct |
206 ms |
16624 KB |
Output is correct |
117 |
Correct |
214 ms |
15452 KB |
Output is correct |
118 |
Correct |
240 ms |
14880 KB |
Output is correct |
119 |
Correct |
239 ms |
14584 KB |
Output is correct |
120 |
Correct |
206 ms |
14928 KB |
Output is correct |
121 |
Correct |
219 ms |
14576 KB |
Output is correct |
122 |
Correct |
217 ms |
14248 KB |
Output is correct |
123 |
Correct |
241 ms |
15428 KB |
Output is correct |
124 |
Correct |
255 ms |
14940 KB |
Output is correct |
125 |
Correct |
265 ms |
14668 KB |
Output is correct |
126 |
Correct |
236 ms |
15080 KB |
Output is correct |
127 |
Correct |
244 ms |
14564 KB |
Output is correct |
128 |
Correct |
247 ms |
14400 KB |
Output is correct |