#include <bits/stdc++.h>
#define long long long
#define pii pair<long, long>
#define x first
#define y second
using namespace std;
const int N = 1e5+5;
int n, C[N];
int par[N], dep[N], spi[N], pos[N], rot[N];
deque<pii> Q[N];
vector<int> g[N];
vector<pii> E;
int dfs(int u, int p) {
int sz = 1; pii ret(0, -1);
par[u] = p, dep[u] = dep[p] + 1;
for(int v : g[u]) if(v != p) {
int z = dfs(v, u);
ret = max(ret, pii(z, v));
sz += z;
}
spi[u] = ret.y;
return sz;
}
void hld() {
dfs(1, 0);
for(int i = 1, idx = 0; i <= n; i++) if(spi[par[i]] != i)
for(int j = i; j != -1; j = spi[j])
rot[j] = i, pos[j] = ++idx;
}
long query(int u) {
vector<pii> ret; vector<int> coord;
while(u) {
int sz = pos[u] - pos[rot[u]] + 1;
vector<pii> now;
for(pii x : Q[rot[u]]) {
if(x.y < sz) now.emplace_back(x), sz -= x.y;
else { now.emplace_back(x.x, sz); break; }
}
reverse(now.begin(), now.end());
for(pii x : now) ret.emplace_back(x), coord.emplace_back(x.x);
u = par[rot[u]];
}
sort(coord.begin(), coord.end());
coord.resize(unique(coord.begin(), coord.end()) - coord.begin());
long sum = 0;
vector<long> t((int)coord.size() + 1, 0);
auto get = [&](int x) { return lower_bound(coord.begin(), coord.end(), x) - coord.begin() + 1; };
auto upd = [&](int x, long k) { for(int i = x; i <= coord.size(); i += i & -i) t[i] += k; };
auto que = [&](int x, long k = 0) { for(int i = x; i; i -= i & -i) k += t[i]; return k; };
for(pii x : ret) {
sum += que(get(x.x) - 1) * x.y;
upd(get(x.x), x.y);
}
return sum;
}
void update(int u, int k) {
while(u) {
int sz = pos[u] - pos[rot[u]] + 1;
deque<pii> &now = Q[rot[u]];
while(!now.empty()) {
if(now.front().y <= sz) sz -= now.front().y, now.pop_front();
else { now.front().y -= sz; break; }
}
Q[rot[u]].emplace_front(k, pos[u] - pos[rot[u]] + 1);
u = par[rot[u]];
}
}
int main() {
scanf("%d", &n);
for(int i = 1; i <= n; i++) scanf("%d", C + i);
for(int i = 1, a, b; i < n; i++) {
scanf("%d %d", &a, &b);
E.emplace_back(a, b);
g[a].emplace_back(b), g[b].emplace_back(a);
}
hld();
Q[1].emplace_back(C[1], 1);
for(pii p : E) {
int u, v; tie(u, v) = p;
printf("%lld\n", query(u));
update(v, C[v]);
}
return 0;
}
Compilation message
construction.cpp: In lambda function:
construction.cpp:56:54: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
auto upd = [&](int x, long k) { for(int i = x; i <= coord.size(); i += i & -i) t[i] += k; };
~~^~~~~~~~~~~~~~~
construction.cpp: In function 'int main()':
construction.cpp:80:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
construction.cpp:81:38: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i = 1; i <= n; i++) scanf("%d", C + i);
~~~~~^~~~~~~~~~~~~
construction.cpp:83:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
70008 KB |
Output is correct |
2 |
Correct |
78 ms |
69980 KB |
Output is correct |
3 |
Correct |
78 ms |
70008 KB |
Output is correct |
4 |
Correct |
79 ms |
70064 KB |
Output is correct |
5 |
Correct |
79 ms |
70136 KB |
Output is correct |
6 |
Correct |
80 ms |
70172 KB |
Output is correct |
7 |
Correct |
80 ms |
70368 KB |
Output is correct |
8 |
Correct |
72 ms |
70108 KB |
Output is correct |
9 |
Correct |
72 ms |
70160 KB |
Output is correct |
10 |
Correct |
73 ms |
70264 KB |
Output is correct |
11 |
Correct |
80 ms |
70264 KB |
Output is correct |
12 |
Correct |
79 ms |
70264 KB |
Output is correct |
13 |
Correct |
78 ms |
70264 KB |
Output is correct |
14 |
Correct |
80 ms |
70264 KB |
Output is correct |
15 |
Correct |
81 ms |
70108 KB |
Output is correct |
16 |
Correct |
98 ms |
70136 KB |
Output is correct |
17 |
Correct |
78 ms |
70140 KB |
Output is correct |
18 |
Correct |
99 ms |
70316 KB |
Output is correct |
19 |
Correct |
104 ms |
70184 KB |
Output is correct |
20 |
Correct |
79 ms |
70264 KB |
Output is correct |
21 |
Correct |
80 ms |
70264 KB |
Output is correct |
22 |
Correct |
99 ms |
70264 KB |
Output is correct |
23 |
Correct |
101 ms |
70264 KB |
Output is correct |
24 |
Correct |
101 ms |
70264 KB |
Output is correct |
25 |
Correct |
79 ms |
70264 KB |
Output is correct |
26 |
Correct |
74 ms |
70268 KB |
Output is correct |
27 |
Correct |
79 ms |
70264 KB |
Output is correct |
28 |
Correct |
80 ms |
70392 KB |
Output is correct |
29 |
Correct |
76 ms |
70308 KB |
Output is correct |
30 |
Correct |
81 ms |
70136 KB |
Output is correct |
31 |
Correct |
80 ms |
70264 KB |
Output is correct |
32 |
Correct |
80 ms |
70208 KB |
Output is correct |
33 |
Correct |
83 ms |
70252 KB |
Output is correct |
34 |
Correct |
79 ms |
70264 KB |
Output is correct |
35 |
Correct |
99 ms |
70252 KB |
Output is correct |
36 |
Correct |
78 ms |
70136 KB |
Output is correct |
37 |
Correct |
79 ms |
70264 KB |
Output is correct |
38 |
Correct |
79 ms |
70264 KB |
Output is correct |
39 |
Correct |
78 ms |
70364 KB |
Output is correct |
40 |
Correct |
80 ms |
70264 KB |
Output is correct |
41 |
Correct |
79 ms |
70260 KB |
Output is correct |
42 |
Correct |
82 ms |
70264 KB |
Output is correct |
43 |
Correct |
82 ms |
70264 KB |
Output is correct |
44 |
Correct |
88 ms |
70296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
70008 KB |
Output is correct |
2 |
Correct |
78 ms |
69980 KB |
Output is correct |
3 |
Correct |
78 ms |
70008 KB |
Output is correct |
4 |
Correct |
79 ms |
70064 KB |
Output is correct |
5 |
Correct |
79 ms |
70136 KB |
Output is correct |
6 |
Correct |
80 ms |
70172 KB |
Output is correct |
7 |
Correct |
80 ms |
70368 KB |
Output is correct |
8 |
Correct |
72 ms |
70108 KB |
Output is correct |
9 |
Correct |
72 ms |
70160 KB |
Output is correct |
10 |
Correct |
73 ms |
70264 KB |
Output is correct |
11 |
Correct |
80 ms |
70264 KB |
Output is correct |
12 |
Correct |
79 ms |
70264 KB |
Output is correct |
13 |
Correct |
78 ms |
70264 KB |
Output is correct |
14 |
Correct |
80 ms |
70264 KB |
Output is correct |
15 |
Correct |
81 ms |
70108 KB |
Output is correct |
16 |
Correct |
98 ms |
70136 KB |
Output is correct |
17 |
Correct |
78 ms |
70140 KB |
Output is correct |
18 |
Correct |
99 ms |
70316 KB |
Output is correct |
19 |
Correct |
104 ms |
70184 KB |
Output is correct |
20 |
Correct |
79 ms |
70264 KB |
Output is correct |
21 |
Correct |
80 ms |
70264 KB |
Output is correct |
22 |
Correct |
99 ms |
70264 KB |
Output is correct |
23 |
Correct |
101 ms |
70264 KB |
Output is correct |
24 |
Correct |
101 ms |
70264 KB |
Output is correct |
25 |
Correct |
79 ms |
70264 KB |
Output is correct |
26 |
Correct |
74 ms |
70268 KB |
Output is correct |
27 |
Correct |
79 ms |
70264 KB |
Output is correct |
28 |
Correct |
80 ms |
70392 KB |
Output is correct |
29 |
Correct |
76 ms |
70308 KB |
Output is correct |
30 |
Correct |
81 ms |
70136 KB |
Output is correct |
31 |
Correct |
80 ms |
70264 KB |
Output is correct |
32 |
Correct |
80 ms |
70208 KB |
Output is correct |
33 |
Correct |
83 ms |
70252 KB |
Output is correct |
34 |
Correct |
79 ms |
70264 KB |
Output is correct |
35 |
Correct |
99 ms |
70252 KB |
Output is correct |
36 |
Correct |
78 ms |
70136 KB |
Output is correct |
37 |
Correct |
79 ms |
70264 KB |
Output is correct |
38 |
Correct |
79 ms |
70264 KB |
Output is correct |
39 |
Correct |
78 ms |
70364 KB |
Output is correct |
40 |
Correct |
80 ms |
70264 KB |
Output is correct |
41 |
Correct |
79 ms |
70260 KB |
Output is correct |
42 |
Correct |
82 ms |
70264 KB |
Output is correct |
43 |
Correct |
82 ms |
70264 KB |
Output is correct |
44 |
Correct |
88 ms |
70296 KB |
Output is correct |
45 |
Correct |
82 ms |
70352 KB |
Output is correct |
46 |
Correct |
88 ms |
71416 KB |
Output is correct |
47 |
Correct |
89 ms |
71420 KB |
Output is correct |
48 |
Correct |
86 ms |
71416 KB |
Output is correct |
49 |
Correct |
95 ms |
70776 KB |
Output is correct |
50 |
Correct |
102 ms |
70904 KB |
Output is correct |
51 |
Correct |
79 ms |
70776 KB |
Output is correct |
52 |
Correct |
84 ms |
71800 KB |
Output is correct |
53 |
Correct |
87 ms |
71800 KB |
Output is correct |
54 |
Correct |
87 ms |
71672 KB |
Output is correct |
55 |
Correct |
85 ms |
71772 KB |
Output is correct |
56 |
Correct |
87 ms |
71672 KB |
Output is correct |
57 |
Correct |
88 ms |
71416 KB |
Output is correct |
58 |
Correct |
91 ms |
71416 KB |
Output is correct |
59 |
Correct |
92 ms |
71416 KB |
Output is correct |
60 |
Correct |
92 ms |
71420 KB |
Output is correct |
61 |
Correct |
86 ms |
71672 KB |
Output is correct |
62 |
Correct |
83 ms |
71672 KB |
Output is correct |
63 |
Correct |
95 ms |
71544 KB |
Output is correct |
64 |
Correct |
97 ms |
71416 KB |
Output is correct |
65 |
Correct |
88 ms |
71416 KB |
Output is correct |
66 |
Correct |
90 ms |
71416 KB |
Output is correct |
67 |
Correct |
88 ms |
71544 KB |
Output is correct |
68 |
Correct |
77 ms |
70776 KB |
Output is correct |
69 |
Correct |
85 ms |
71904 KB |
Output is correct |
70 |
Correct |
85 ms |
71776 KB |
Output is correct |
71 |
Correct |
84 ms |
71676 KB |
Output is correct |
72 |
Correct |
89 ms |
71416 KB |
Output is correct |
73 |
Correct |
92 ms |
71416 KB |
Output is correct |
74 |
Correct |
84 ms |
71672 KB |
Output is correct |
75 |
Correct |
86 ms |
71672 KB |
Output is correct |
76 |
Correct |
85 ms |
71544 KB |
Output is correct |
77 |
Correct |
85 ms |
71416 KB |
Output is correct |
78 |
Correct |
87 ms |
71524 KB |
Output is correct |
79 |
Correct |
86 ms |
71416 KB |
Output is correct |
80 |
Correct |
86 ms |
71416 KB |
Output is correct |
81 |
Correct |
85 ms |
71672 KB |
Output is correct |
82 |
Correct |
87 ms |
71584 KB |
Output is correct |
83 |
Correct |
86 ms |
71568 KB |
Output is correct |
84 |
Correct |
86 ms |
71416 KB |
Output is correct |
85 |
Correct |
85 ms |
71416 KB |
Output is correct |
86 |
Correct |
84 ms |
71420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
70008 KB |
Output is correct |
2 |
Correct |
78 ms |
69980 KB |
Output is correct |
3 |
Correct |
78 ms |
70008 KB |
Output is correct |
4 |
Correct |
79 ms |
70064 KB |
Output is correct |
5 |
Correct |
79 ms |
70136 KB |
Output is correct |
6 |
Correct |
80 ms |
70172 KB |
Output is correct |
7 |
Correct |
80 ms |
70368 KB |
Output is correct |
8 |
Correct |
72 ms |
70108 KB |
Output is correct |
9 |
Correct |
72 ms |
70160 KB |
Output is correct |
10 |
Correct |
73 ms |
70264 KB |
Output is correct |
11 |
Correct |
80 ms |
70264 KB |
Output is correct |
12 |
Correct |
79 ms |
70264 KB |
Output is correct |
13 |
Correct |
78 ms |
70264 KB |
Output is correct |
14 |
Correct |
80 ms |
70264 KB |
Output is correct |
15 |
Correct |
81 ms |
70108 KB |
Output is correct |
16 |
Correct |
98 ms |
70136 KB |
Output is correct |
17 |
Correct |
78 ms |
70140 KB |
Output is correct |
18 |
Correct |
99 ms |
70316 KB |
Output is correct |
19 |
Correct |
104 ms |
70184 KB |
Output is correct |
20 |
Correct |
79 ms |
70264 KB |
Output is correct |
21 |
Correct |
80 ms |
70264 KB |
Output is correct |
22 |
Correct |
99 ms |
70264 KB |
Output is correct |
23 |
Correct |
101 ms |
70264 KB |
Output is correct |
24 |
Correct |
101 ms |
70264 KB |
Output is correct |
25 |
Correct |
79 ms |
70264 KB |
Output is correct |
26 |
Correct |
74 ms |
70268 KB |
Output is correct |
27 |
Correct |
79 ms |
70264 KB |
Output is correct |
28 |
Correct |
80 ms |
70392 KB |
Output is correct |
29 |
Correct |
76 ms |
70308 KB |
Output is correct |
30 |
Correct |
81 ms |
70136 KB |
Output is correct |
31 |
Correct |
80 ms |
70264 KB |
Output is correct |
32 |
Correct |
80 ms |
70208 KB |
Output is correct |
33 |
Correct |
83 ms |
70252 KB |
Output is correct |
34 |
Correct |
79 ms |
70264 KB |
Output is correct |
35 |
Correct |
99 ms |
70252 KB |
Output is correct |
36 |
Correct |
78 ms |
70136 KB |
Output is correct |
37 |
Correct |
79 ms |
70264 KB |
Output is correct |
38 |
Correct |
79 ms |
70264 KB |
Output is correct |
39 |
Correct |
78 ms |
70364 KB |
Output is correct |
40 |
Correct |
80 ms |
70264 KB |
Output is correct |
41 |
Correct |
79 ms |
70260 KB |
Output is correct |
42 |
Correct |
82 ms |
70264 KB |
Output is correct |
43 |
Correct |
82 ms |
70264 KB |
Output is correct |
44 |
Correct |
88 ms |
70296 KB |
Output is correct |
45 |
Correct |
82 ms |
70352 KB |
Output is correct |
46 |
Correct |
88 ms |
71416 KB |
Output is correct |
47 |
Correct |
89 ms |
71420 KB |
Output is correct |
48 |
Correct |
86 ms |
71416 KB |
Output is correct |
49 |
Correct |
95 ms |
70776 KB |
Output is correct |
50 |
Correct |
102 ms |
70904 KB |
Output is correct |
51 |
Correct |
79 ms |
70776 KB |
Output is correct |
52 |
Correct |
84 ms |
71800 KB |
Output is correct |
53 |
Correct |
87 ms |
71800 KB |
Output is correct |
54 |
Correct |
87 ms |
71672 KB |
Output is correct |
55 |
Correct |
85 ms |
71772 KB |
Output is correct |
56 |
Correct |
87 ms |
71672 KB |
Output is correct |
57 |
Correct |
88 ms |
71416 KB |
Output is correct |
58 |
Correct |
91 ms |
71416 KB |
Output is correct |
59 |
Correct |
92 ms |
71416 KB |
Output is correct |
60 |
Correct |
92 ms |
71420 KB |
Output is correct |
61 |
Correct |
86 ms |
71672 KB |
Output is correct |
62 |
Correct |
83 ms |
71672 KB |
Output is correct |
63 |
Correct |
95 ms |
71544 KB |
Output is correct |
64 |
Correct |
97 ms |
71416 KB |
Output is correct |
65 |
Correct |
88 ms |
71416 KB |
Output is correct |
66 |
Correct |
90 ms |
71416 KB |
Output is correct |
67 |
Correct |
88 ms |
71544 KB |
Output is correct |
68 |
Correct |
77 ms |
70776 KB |
Output is correct |
69 |
Correct |
85 ms |
71904 KB |
Output is correct |
70 |
Correct |
85 ms |
71776 KB |
Output is correct |
71 |
Correct |
84 ms |
71676 KB |
Output is correct |
72 |
Correct |
89 ms |
71416 KB |
Output is correct |
73 |
Correct |
92 ms |
71416 KB |
Output is correct |
74 |
Correct |
84 ms |
71672 KB |
Output is correct |
75 |
Correct |
86 ms |
71672 KB |
Output is correct |
76 |
Correct |
85 ms |
71544 KB |
Output is correct |
77 |
Correct |
85 ms |
71416 KB |
Output is correct |
78 |
Correct |
87 ms |
71524 KB |
Output is correct |
79 |
Correct |
86 ms |
71416 KB |
Output is correct |
80 |
Correct |
86 ms |
71416 KB |
Output is correct |
81 |
Correct |
85 ms |
71672 KB |
Output is correct |
82 |
Correct |
87 ms |
71584 KB |
Output is correct |
83 |
Correct |
86 ms |
71568 KB |
Output is correct |
84 |
Correct |
86 ms |
71416 KB |
Output is correct |
85 |
Correct |
85 ms |
71416 KB |
Output is correct |
86 |
Correct |
84 ms |
71420 KB |
Output is correct |
87 |
Correct |
104 ms |
73596 KB |
Output is correct |
88 |
Correct |
166 ms |
80700 KB |
Output is correct |
89 |
Correct |
435 ms |
105444 KB |
Output is correct |
90 |
Correct |
479 ms |
105408 KB |
Output is correct |
91 |
Correct |
435 ms |
105316 KB |
Output is correct |
92 |
Correct |
213 ms |
90340 KB |
Output is correct |
93 |
Correct |
214 ms |
90316 KB |
Output is correct |
94 |
Correct |
209 ms |
90296 KB |
Output is correct |
95 |
Correct |
270 ms |
113756 KB |
Output is correct |
96 |
Correct |
292 ms |
114104 KB |
Output is correct |
97 |
Correct |
288 ms |
114152 KB |
Output is correct |
98 |
Correct |
288 ms |
114188 KB |
Output is correct |
99 |
Correct |
308 ms |
110820 KB |
Output is correct |
100 |
Correct |
527 ms |
97128 KB |
Output is correct |
101 |
Correct |
594 ms |
97364 KB |
Output is correct |
102 |
Correct |
574 ms |
97360 KB |
Output is correct |
103 |
Correct |
576 ms |
97412 KB |
Output is correct |
104 |
Correct |
288 ms |
110948 KB |
Output is correct |
105 |
Correct |
278 ms |
110748 KB |
Output is correct |
106 |
Correct |
285 ms |
110948 KB |
Output is correct |
107 |
Correct |
402 ms |
104680 KB |
Output is correct |
108 |
Correct |
421 ms |
104808 KB |
Output is correct |
109 |
Correct |
442 ms |
104932 KB |
Output is correct |
110 |
Correct |
200 ms |
89744 KB |
Output is correct |
111 |
Correct |
272 ms |
113636 KB |
Output is correct |
112 |
Correct |
273 ms |
113592 KB |
Output is correct |
113 |
Correct |
296 ms |
110292 KB |
Output is correct |
114 |
Correct |
511 ms |
96968 KB |
Output is correct |
115 |
Correct |
611 ms |
96868 KB |
Output is correct |
116 |
Correct |
355 ms |
110308 KB |
Output is correct |
117 |
Correct |
287 ms |
107748 KB |
Output is correct |
118 |
Correct |
291 ms |
106952 KB |
Output is correct |
119 |
Correct |
293 ms |
106340 KB |
Output is correct |
120 |
Correct |
288 ms |
107364 KB |
Output is correct |
121 |
Correct |
281 ms |
106468 KB |
Output is correct |
122 |
Correct |
285 ms |
106084 KB |
Output is correct |
123 |
Correct |
296 ms |
107824 KB |
Output is correct |
124 |
Correct |
336 ms |
106908 KB |
Output is correct |
125 |
Correct |
350 ms |
106472 KB |
Output is correct |
126 |
Correct |
309 ms |
107496 KB |
Output is correct |
127 |
Correct |
297 ms |
106596 KB |
Output is correct |
128 |
Correct |
304 ms |
106212 KB |
Output is correct |