// In the name of the God
#include <bits/stdc++.h>
#define ll long long
#define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 5e5+5;
int n, c[maxn], par[maxn], he[maxn], node[maxn], heavy[maxn], head[maxn], fen[maxn];
vector <int> uni, adj[maxn];
vector <pii> ch[maxn], vec1, vec2;
int dfs1(int v = 0) {
int sz = 1, mx = 0;
heavy[v] = -1;
for (int u : adj[v]) {
int csz = dfs1(u);
sz += csz;
if (csz > mx) mx = csz, heavy[v] = u;
}
return sz;
}
void dfs2(int v = 0) {
for (int u : adj[v]) {
if (u == heavy[v]) head[u] = head[v];
else head[u] = u;
dfs2(u);
}
}
int get(int i) {
int ans = 0;
for (i++; i; i -= i&-i) ans += fen[i-1];
return ans;
}
void upd(int i, int x) {
for (i++; i <= n; i += i&-i) fen[i-1] += x;
}
int processs(int v) {
int ans = 0;
while (v != -1) {
int last = he[head[v]]-1;
for (int i = ch[head[v]].size()-1; i >= 0; i--) {
vec1.pb(mp(min(ch[head[v]][i].F, he[v])-last, ch[head[v]][i].S));
last = ch[head[v]][i].F;
if (last >= he[v]) break;
}
for (int i = vec1.size()-1; i >= 0; i--) {
vec2.pb(vec1[i]);
ans += get(vec1[i].S-1)*vec1[i].F;
upd(vec1[i].S, vec1[i].F);
}
vec1.clear();
v = par[head[v]];
}
for (int i = 0; i < vec2.size(); i++) upd(vec2[i].S, -vec2[i].F);
vec2.clear();
return ans;
}
void amir(int v) {
int x = c[v];
while (v != -1) {
for (int i = ch[head[v]].size()-1; i >= 0; i--) {
if (ch[head[v]][i].F > he[v]) break;
ch[head[v]].pop_back();
}
ch[head[v]].pb(mp(he[v], x));
v = par[head[v]];
}
}
int32_t main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 0; i < n; i++) {
cin >> c[i];
uni.pb(c[i]);
}
sort(all(uni)); uni.resize(unique(all(uni))-uni.begin());
for (int i = 0; i < n; i++) c[i] = lower_bound(all(uni), c[i])-uni.begin();
par[0] = -1;
for (int i = 0; i < n-1; i++) {
int v, u; cin >> v >> u;
adj[--v].pb(--u);
par[u] = v;
he[u] = he[v]+1;
node[i] = u;
}
dfs1(), dfs2();
ch[0].pb(mp(0, c[0]));
for (int i = 0; i < n-1; i++) {
cout << processs(par[node[i]]) << '\n';
amir(node[i]);
}
return 0;
}
Compilation message
construction.cpp: In function 'long long int processs(long long int)':
construction.cpp:69:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int i = 0; i < vec2.size(); i++) upd(vec2[i].S, -vec2[i].F);
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
33372 KB |
Output is correct |
2 |
Correct |
9 ms |
35536 KB |
Output is correct |
3 |
Correct |
10 ms |
35420 KB |
Output is correct |
4 |
Correct |
9 ms |
35456 KB |
Output is correct |
5 |
Correct |
10 ms |
33372 KB |
Output is correct |
6 |
Correct |
10 ms |
35572 KB |
Output is correct |
7 |
Correct |
10 ms |
33372 KB |
Output is correct |
8 |
Correct |
10 ms |
33372 KB |
Output is correct |
9 |
Correct |
11 ms |
33476 KB |
Output is correct |
10 |
Correct |
10 ms |
33372 KB |
Output is correct |
11 |
Correct |
11 ms |
33880 KB |
Output is correct |
12 |
Correct |
9 ms |
33372 KB |
Output is correct |
13 |
Correct |
9 ms |
33552 KB |
Output is correct |
14 |
Correct |
9 ms |
33372 KB |
Output is correct |
15 |
Correct |
9 ms |
33372 KB |
Output is correct |
16 |
Correct |
10 ms |
33372 KB |
Output is correct |
17 |
Correct |
9 ms |
33372 KB |
Output is correct |
18 |
Correct |
10 ms |
33372 KB |
Output is correct |
19 |
Correct |
11 ms |
33372 KB |
Output is correct |
20 |
Correct |
9 ms |
33368 KB |
Output is correct |
21 |
Correct |
10 ms |
33372 KB |
Output is correct |
22 |
Correct |
9 ms |
33372 KB |
Output is correct |
23 |
Correct |
10 ms |
33372 KB |
Output is correct |
24 |
Correct |
9 ms |
33372 KB |
Output is correct |
25 |
Correct |
10 ms |
33468 KB |
Output is correct |
26 |
Correct |
9 ms |
33544 KB |
Output is correct |
27 |
Correct |
10 ms |
33368 KB |
Output is correct |
28 |
Correct |
9 ms |
33372 KB |
Output is correct |
29 |
Correct |
9 ms |
33372 KB |
Output is correct |
30 |
Correct |
10 ms |
33372 KB |
Output is correct |
31 |
Correct |
11 ms |
33368 KB |
Output is correct |
32 |
Correct |
10 ms |
33372 KB |
Output is correct |
33 |
Correct |
10 ms |
33372 KB |
Output is correct |
34 |
Correct |
9 ms |
33372 KB |
Output is correct |
35 |
Correct |
9 ms |
33368 KB |
Output is correct |
36 |
Correct |
9 ms |
33372 KB |
Output is correct |
37 |
Correct |
11 ms |
33412 KB |
Output is correct |
38 |
Correct |
9 ms |
33372 KB |
Output is correct |
39 |
Correct |
10 ms |
33420 KB |
Output is correct |
40 |
Correct |
10 ms |
33368 KB |
Output is correct |
41 |
Correct |
10 ms |
33372 KB |
Output is correct |
42 |
Correct |
10 ms |
33452 KB |
Output is correct |
43 |
Correct |
9 ms |
33372 KB |
Output is correct |
44 |
Correct |
10 ms |
33368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
33372 KB |
Output is correct |
2 |
Correct |
9 ms |
35536 KB |
Output is correct |
3 |
Correct |
10 ms |
35420 KB |
Output is correct |
4 |
Correct |
9 ms |
35456 KB |
Output is correct |
5 |
Correct |
10 ms |
33372 KB |
Output is correct |
6 |
Correct |
10 ms |
35572 KB |
Output is correct |
7 |
Correct |
10 ms |
33372 KB |
Output is correct |
8 |
Correct |
10 ms |
33372 KB |
Output is correct |
9 |
Correct |
11 ms |
33476 KB |
Output is correct |
10 |
Correct |
10 ms |
33372 KB |
Output is correct |
11 |
Correct |
11 ms |
33880 KB |
Output is correct |
12 |
Correct |
9 ms |
33372 KB |
Output is correct |
13 |
Correct |
9 ms |
33552 KB |
Output is correct |
14 |
Correct |
9 ms |
33372 KB |
Output is correct |
15 |
Correct |
9 ms |
33372 KB |
Output is correct |
16 |
Correct |
10 ms |
33372 KB |
Output is correct |
17 |
Correct |
9 ms |
33372 KB |
Output is correct |
18 |
Correct |
10 ms |
33372 KB |
Output is correct |
19 |
Correct |
11 ms |
33372 KB |
Output is correct |
20 |
Correct |
9 ms |
33368 KB |
Output is correct |
21 |
Correct |
10 ms |
33372 KB |
Output is correct |
22 |
Correct |
9 ms |
33372 KB |
Output is correct |
23 |
Correct |
10 ms |
33372 KB |
Output is correct |
24 |
Correct |
9 ms |
33372 KB |
Output is correct |
25 |
Correct |
10 ms |
33468 KB |
Output is correct |
26 |
Correct |
9 ms |
33544 KB |
Output is correct |
27 |
Correct |
10 ms |
33368 KB |
Output is correct |
28 |
Correct |
9 ms |
33372 KB |
Output is correct |
29 |
Correct |
9 ms |
33372 KB |
Output is correct |
30 |
Correct |
10 ms |
33372 KB |
Output is correct |
31 |
Correct |
11 ms |
33368 KB |
Output is correct |
32 |
Correct |
10 ms |
33372 KB |
Output is correct |
33 |
Correct |
10 ms |
33372 KB |
Output is correct |
34 |
Correct |
9 ms |
33372 KB |
Output is correct |
35 |
Correct |
9 ms |
33368 KB |
Output is correct |
36 |
Correct |
9 ms |
33372 KB |
Output is correct |
37 |
Correct |
11 ms |
33412 KB |
Output is correct |
38 |
Correct |
9 ms |
33372 KB |
Output is correct |
39 |
Correct |
10 ms |
33420 KB |
Output is correct |
40 |
Correct |
10 ms |
33368 KB |
Output is correct |
41 |
Correct |
10 ms |
33372 KB |
Output is correct |
42 |
Correct |
10 ms |
33452 KB |
Output is correct |
43 |
Correct |
9 ms |
33372 KB |
Output is correct |
44 |
Correct |
10 ms |
33368 KB |
Output is correct |
45 |
Correct |
10 ms |
33368 KB |
Output is correct |
46 |
Correct |
14 ms |
35664 KB |
Output is correct |
47 |
Correct |
14 ms |
35676 KB |
Output is correct |
48 |
Correct |
16 ms |
37724 KB |
Output is correct |
49 |
Correct |
12 ms |
35932 KB |
Output is correct |
50 |
Correct |
13 ms |
35872 KB |
Output is correct |
51 |
Correct |
12 ms |
35932 KB |
Output is correct |
52 |
Correct |
12 ms |
35932 KB |
Output is correct |
53 |
Correct |
12 ms |
36040 KB |
Output is correct |
54 |
Correct |
12 ms |
35928 KB |
Output is correct |
55 |
Correct |
12 ms |
35932 KB |
Output is correct |
56 |
Correct |
12 ms |
35928 KB |
Output is correct |
57 |
Correct |
15 ms |
35676 KB |
Output is correct |
58 |
Correct |
14 ms |
35676 KB |
Output is correct |
59 |
Correct |
15 ms |
35676 KB |
Output is correct |
60 |
Correct |
15 ms |
35676 KB |
Output is correct |
61 |
Correct |
12 ms |
35928 KB |
Output is correct |
62 |
Correct |
13 ms |
35676 KB |
Output is correct |
63 |
Correct |
12 ms |
35932 KB |
Output is correct |
64 |
Correct |
13 ms |
35676 KB |
Output is correct |
65 |
Correct |
13 ms |
35676 KB |
Output is correct |
66 |
Correct |
15 ms |
35764 KB |
Output is correct |
67 |
Correct |
13 ms |
35784 KB |
Output is correct |
68 |
Correct |
12 ms |
35932 KB |
Output is correct |
69 |
Correct |
13 ms |
36028 KB |
Output is correct |
70 |
Correct |
11 ms |
35968 KB |
Output is correct |
71 |
Correct |
12 ms |
35676 KB |
Output is correct |
72 |
Correct |
14 ms |
35848 KB |
Output is correct |
73 |
Correct |
16 ms |
35928 KB |
Output is correct |
74 |
Correct |
13 ms |
35676 KB |
Output is correct |
75 |
Correct |
14 ms |
35672 KB |
Output is correct |
76 |
Correct |
12 ms |
35676 KB |
Output is correct |
77 |
Correct |
12 ms |
35716 KB |
Output is correct |
78 |
Correct |
12 ms |
35676 KB |
Output is correct |
79 |
Correct |
12 ms |
35672 KB |
Output is correct |
80 |
Correct |
12 ms |
35676 KB |
Output is correct |
81 |
Correct |
13 ms |
35636 KB |
Output is correct |
82 |
Correct |
13 ms |
35676 KB |
Output is correct |
83 |
Correct |
12 ms |
35692 KB |
Output is correct |
84 |
Correct |
12 ms |
35676 KB |
Output is correct |
85 |
Correct |
12 ms |
35836 KB |
Output is correct |
86 |
Correct |
13 ms |
35676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
33372 KB |
Output is correct |
2 |
Correct |
9 ms |
35536 KB |
Output is correct |
3 |
Correct |
10 ms |
35420 KB |
Output is correct |
4 |
Correct |
9 ms |
35456 KB |
Output is correct |
5 |
Correct |
10 ms |
33372 KB |
Output is correct |
6 |
Correct |
10 ms |
35572 KB |
Output is correct |
7 |
Correct |
10 ms |
33372 KB |
Output is correct |
8 |
Correct |
10 ms |
33372 KB |
Output is correct |
9 |
Correct |
11 ms |
33476 KB |
Output is correct |
10 |
Correct |
10 ms |
33372 KB |
Output is correct |
11 |
Correct |
11 ms |
33880 KB |
Output is correct |
12 |
Correct |
9 ms |
33372 KB |
Output is correct |
13 |
Correct |
9 ms |
33552 KB |
Output is correct |
14 |
Correct |
9 ms |
33372 KB |
Output is correct |
15 |
Correct |
9 ms |
33372 KB |
Output is correct |
16 |
Correct |
10 ms |
33372 KB |
Output is correct |
17 |
Correct |
9 ms |
33372 KB |
Output is correct |
18 |
Correct |
10 ms |
33372 KB |
Output is correct |
19 |
Correct |
11 ms |
33372 KB |
Output is correct |
20 |
Correct |
9 ms |
33368 KB |
Output is correct |
21 |
Correct |
10 ms |
33372 KB |
Output is correct |
22 |
Correct |
9 ms |
33372 KB |
Output is correct |
23 |
Correct |
10 ms |
33372 KB |
Output is correct |
24 |
Correct |
9 ms |
33372 KB |
Output is correct |
25 |
Correct |
10 ms |
33468 KB |
Output is correct |
26 |
Correct |
9 ms |
33544 KB |
Output is correct |
27 |
Correct |
10 ms |
33368 KB |
Output is correct |
28 |
Correct |
9 ms |
33372 KB |
Output is correct |
29 |
Correct |
9 ms |
33372 KB |
Output is correct |
30 |
Correct |
10 ms |
33372 KB |
Output is correct |
31 |
Correct |
11 ms |
33368 KB |
Output is correct |
32 |
Correct |
10 ms |
33372 KB |
Output is correct |
33 |
Correct |
10 ms |
33372 KB |
Output is correct |
34 |
Correct |
9 ms |
33372 KB |
Output is correct |
35 |
Correct |
9 ms |
33368 KB |
Output is correct |
36 |
Correct |
9 ms |
33372 KB |
Output is correct |
37 |
Correct |
11 ms |
33412 KB |
Output is correct |
38 |
Correct |
9 ms |
33372 KB |
Output is correct |
39 |
Correct |
10 ms |
33420 KB |
Output is correct |
40 |
Correct |
10 ms |
33368 KB |
Output is correct |
41 |
Correct |
10 ms |
33372 KB |
Output is correct |
42 |
Correct |
10 ms |
33452 KB |
Output is correct |
43 |
Correct |
9 ms |
33372 KB |
Output is correct |
44 |
Correct |
10 ms |
33368 KB |
Output is correct |
45 |
Correct |
10 ms |
33368 KB |
Output is correct |
46 |
Correct |
14 ms |
35664 KB |
Output is correct |
47 |
Correct |
14 ms |
35676 KB |
Output is correct |
48 |
Correct |
16 ms |
37724 KB |
Output is correct |
49 |
Correct |
12 ms |
35932 KB |
Output is correct |
50 |
Correct |
13 ms |
35872 KB |
Output is correct |
51 |
Correct |
12 ms |
35932 KB |
Output is correct |
52 |
Correct |
12 ms |
35932 KB |
Output is correct |
53 |
Correct |
12 ms |
36040 KB |
Output is correct |
54 |
Correct |
12 ms |
35928 KB |
Output is correct |
55 |
Correct |
12 ms |
35932 KB |
Output is correct |
56 |
Correct |
12 ms |
35928 KB |
Output is correct |
57 |
Correct |
15 ms |
35676 KB |
Output is correct |
58 |
Correct |
14 ms |
35676 KB |
Output is correct |
59 |
Correct |
15 ms |
35676 KB |
Output is correct |
60 |
Correct |
15 ms |
35676 KB |
Output is correct |
61 |
Correct |
12 ms |
35928 KB |
Output is correct |
62 |
Correct |
13 ms |
35676 KB |
Output is correct |
63 |
Correct |
12 ms |
35932 KB |
Output is correct |
64 |
Correct |
13 ms |
35676 KB |
Output is correct |
65 |
Correct |
13 ms |
35676 KB |
Output is correct |
66 |
Correct |
15 ms |
35764 KB |
Output is correct |
67 |
Correct |
13 ms |
35784 KB |
Output is correct |
68 |
Correct |
12 ms |
35932 KB |
Output is correct |
69 |
Correct |
13 ms |
36028 KB |
Output is correct |
70 |
Correct |
11 ms |
35968 KB |
Output is correct |
71 |
Correct |
12 ms |
35676 KB |
Output is correct |
72 |
Correct |
14 ms |
35848 KB |
Output is correct |
73 |
Correct |
16 ms |
35928 KB |
Output is correct |
74 |
Correct |
13 ms |
35676 KB |
Output is correct |
75 |
Correct |
14 ms |
35672 KB |
Output is correct |
76 |
Correct |
12 ms |
35676 KB |
Output is correct |
77 |
Correct |
12 ms |
35716 KB |
Output is correct |
78 |
Correct |
12 ms |
35676 KB |
Output is correct |
79 |
Correct |
12 ms |
35672 KB |
Output is correct |
80 |
Correct |
12 ms |
35676 KB |
Output is correct |
81 |
Correct |
13 ms |
35636 KB |
Output is correct |
82 |
Correct |
13 ms |
35676 KB |
Output is correct |
83 |
Correct |
12 ms |
35692 KB |
Output is correct |
84 |
Correct |
12 ms |
35676 KB |
Output is correct |
85 |
Correct |
12 ms |
35836 KB |
Output is correct |
86 |
Correct |
13 ms |
35676 KB |
Output is correct |
87 |
Correct |
22 ms |
38232 KB |
Output is correct |
88 |
Correct |
43 ms |
39580 KB |
Output is correct |
89 |
Correct |
162 ms |
47820 KB |
Output is correct |
90 |
Correct |
142 ms |
47824 KB |
Output is correct |
91 |
Correct |
148 ms |
46540 KB |
Output is correct |
92 |
Correct |
87 ms |
50468 KB |
Output is correct |
93 |
Correct |
80 ms |
50508 KB |
Output is correct |
94 |
Correct |
79 ms |
50376 KB |
Output is correct |
95 |
Correct |
87 ms |
50968 KB |
Output is correct |
96 |
Correct |
88 ms |
51284 KB |
Output is correct |
97 |
Correct |
88 ms |
51100 KB |
Output is correct |
98 |
Correct |
91 ms |
51276 KB |
Output is correct |
99 |
Correct |
86 ms |
48236 KB |
Output is correct |
100 |
Correct |
160 ms |
46288 KB |
Output is correct |
101 |
Correct |
212 ms |
46504 KB |
Output is correct |
102 |
Correct |
226 ms |
46688 KB |
Output is correct |
103 |
Correct |
200 ms |
46520 KB |
Output is correct |
104 |
Correct |
92 ms |
48292 KB |
Output is correct |
105 |
Correct |
90 ms |
48104 KB |
Output is correct |
106 |
Correct |
90 ms |
48220 KB |
Output is correct |
107 |
Correct |
126 ms |
46028 KB |
Output is correct |
108 |
Correct |
126 ms |
47568 KB |
Output is correct |
109 |
Correct |
137 ms |
46300 KB |
Output is correct |
110 |
Correct |
67 ms |
50180 KB |
Output is correct |
111 |
Correct |
85 ms |
50812 KB |
Output is correct |
112 |
Correct |
85 ms |
51156 KB |
Output is correct |
113 |
Correct |
73 ms |
47836 KB |
Output is correct |
114 |
Correct |
170 ms |
46212 KB |
Output is correct |
115 |
Correct |
174 ms |
46228 KB |
Output is correct |
116 |
Correct |
84 ms |
47820 KB |
Output is correct |
117 |
Correct |
99 ms |
47448 KB |
Output is correct |
118 |
Correct |
84 ms |
46536 KB |
Output is correct |
119 |
Correct |
86 ms |
46408 KB |
Output is correct |
120 |
Correct |
74 ms |
46572 KB |
Output is correct |
121 |
Correct |
83 ms |
46388 KB |
Output is correct |
122 |
Correct |
74 ms |
46060 KB |
Output is correct |
123 |
Correct |
92 ms |
47056 KB |
Output is correct |
124 |
Correct |
93 ms |
46544 KB |
Output is correct |
125 |
Correct |
96 ms |
46588 KB |
Output is correct |
126 |
Correct |
81 ms |
46696 KB |
Output is correct |
127 |
Correct |
79 ms |
46276 KB |
Output is correct |
128 |
Correct |
86 ms |
46192 KB |
Output is correct |