#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = (int)1e5 + 10;
int A[N], B[N], C[N];
vector<int> T[N];
int subt[N];
int dep[N];
int link[N];
int par[N];
void dfs(int u){
subt[u]=1;
for(auto x : T[u]){
dep[x]=dep[u]+1;
par[x]=u;
dfs(x);
subt[u]+=subt[x];
}
}
void hld(int u){
for(auto x : T[u]){
if(subt[x] > (subt[u] + 1) / 2){
link[x]=link[u];
}
else{
link[x]=x;
}
hld(x);
}
}
deque<pii> vals[N];
vector<int> ups;
int lim;
int tree[N * 2];
void upd(int id, int v){
id += lim;
while(id > 0){
tree[id] += v;
ups.push_back(id);
id /= 2;
}
}
int get(int l, int r){
int cnt = 0;
l += lim;
r += lim;
while(l <= r){
if(l % 2 == 1) cnt += tree[l ++];
if(r % 2 == 0) cnt += tree[r -- ];
l /= 2;
r /= 2;
}
return cnt;
}
ll setv(int node, int bb){
ll ret = 0;
int en;
int len;
int cur;
int low;
vector<pii> lines;
vector<pii> curs;
while(1){
en = link[node];
len = dep[node]-dep[en]+1;
cur = len;
curs.clear();
while(cur > 0){
low = min(cur, vals[en].back().fi);
curs.push_back(mp(low, vals[en].back().se));
vals[en].back().fi -= low;
cur -= low;
if(vals[en].back().fi == 0)
vals[en].pop_back();
}
reverse(curs.begin(), curs.end());
for(auto x : curs)
lines.push_back(x);
vals[en].push_back(mp(len, bb));
if(en == 1) break;
node = par[en];
}
reverse(lines.begin(), lines.end());
ups.clear();
for(auto pq : lines){
ret += get(pq.se + 1, lim - 1) * 1ll * pq.fi;
upd(pq.se, pq.fi);
}
for(auto x : ups){
tree[x]=0;
}
return ret;
}
int main(){
fastIO;
int n;
cin >> n;
vector<int> x;
for(int i = 1; i <= n; i ++ ){
cin >> C[i];
x.push_back(C[i]);
}
sort(x.begin(), x.end());
x.resize(unique(x.begin(), x.end()) - x.begin());
lim = x.size();
for(int i = 1; i <= n; i ++ ){
C[i] = lower_bound(x.begin(), x.end(), C[i]) - x.begin();
}
int u, v;
for(int i = 1; i < n; i ++ ){
cin >> A[i] >> B[i];
T[A[i]].push_back(B[i]);
}
link[1]=1;
dfs(1);
hld(1);
par[1]=1;
vals[1].push_back(mp(1, C[1]));
for(int i = 1; i < n; i ++ ){
cout << setv(A[i], C[B[i]]) << "\n";
if(vals[link[B[i]]].empty() || vals[link[B[i]]].front().se != C[B[i]]){
vals[link[B[i]]].push_front(mp(1, C[B[i]]));
}
else{
vals[link[B[i]]].front().fi ++ ;
}
}
return 0;
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:128:9: warning: unused variable 'u' [-Wunused-variable]
128 | int u, v;
| ^
construction.cpp:128:12: warning: unused variable 'v' [-Wunused-variable]
128 | int u, v;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
70008 KB |
Output is correct |
2 |
Correct |
53 ms |
70008 KB |
Output is correct |
3 |
Correct |
60 ms |
70136 KB |
Output is correct |
4 |
Correct |
72 ms |
70188 KB |
Output is correct |
5 |
Correct |
60 ms |
70292 KB |
Output is correct |
6 |
Correct |
59 ms |
70264 KB |
Output is correct |
7 |
Correct |
59 ms |
70320 KB |
Output is correct |
8 |
Correct |
59 ms |
70152 KB |
Output is correct |
9 |
Correct |
61 ms |
70136 KB |
Output is correct |
10 |
Correct |
59 ms |
70136 KB |
Output is correct |
11 |
Correct |
65 ms |
70300 KB |
Output is correct |
12 |
Correct |
59 ms |
70264 KB |
Output is correct |
13 |
Correct |
59 ms |
70264 KB |
Output is correct |
14 |
Correct |
59 ms |
70264 KB |
Output is correct |
15 |
Correct |
62 ms |
70264 KB |
Output is correct |
16 |
Correct |
60 ms |
70308 KB |
Output is correct |
17 |
Correct |
59 ms |
70332 KB |
Output is correct |
18 |
Correct |
62 ms |
70392 KB |
Output is correct |
19 |
Correct |
59 ms |
70264 KB |
Output is correct |
20 |
Correct |
63 ms |
70268 KB |
Output is correct |
21 |
Correct |
61 ms |
70204 KB |
Output is correct |
22 |
Correct |
59 ms |
70264 KB |
Output is correct |
23 |
Correct |
59 ms |
70296 KB |
Output is correct |
24 |
Correct |
61 ms |
70264 KB |
Output is correct |
25 |
Correct |
59 ms |
70268 KB |
Output is correct |
26 |
Correct |
59 ms |
70136 KB |
Output is correct |
27 |
Correct |
58 ms |
70264 KB |
Output is correct |
28 |
Correct |
60 ms |
70200 KB |
Output is correct |
29 |
Correct |
59 ms |
70264 KB |
Output is correct |
30 |
Correct |
63 ms |
70356 KB |
Output is correct |
31 |
Correct |
81 ms |
70264 KB |
Output is correct |
32 |
Correct |
58 ms |
70264 KB |
Output is correct |
33 |
Correct |
58 ms |
70136 KB |
Output is correct |
34 |
Correct |
69 ms |
70260 KB |
Output is correct |
35 |
Correct |
58 ms |
70260 KB |
Output is correct |
36 |
Correct |
59 ms |
70136 KB |
Output is correct |
37 |
Correct |
58 ms |
70204 KB |
Output is correct |
38 |
Correct |
59 ms |
70264 KB |
Output is correct |
39 |
Correct |
58 ms |
70264 KB |
Output is correct |
40 |
Correct |
57 ms |
70240 KB |
Output is correct |
41 |
Correct |
57 ms |
70260 KB |
Output is correct |
42 |
Correct |
57 ms |
70136 KB |
Output is correct |
43 |
Correct |
58 ms |
70264 KB |
Output is correct |
44 |
Correct |
58 ms |
70136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
70008 KB |
Output is correct |
2 |
Correct |
53 ms |
70008 KB |
Output is correct |
3 |
Correct |
60 ms |
70136 KB |
Output is correct |
4 |
Correct |
72 ms |
70188 KB |
Output is correct |
5 |
Correct |
60 ms |
70292 KB |
Output is correct |
6 |
Correct |
59 ms |
70264 KB |
Output is correct |
7 |
Correct |
59 ms |
70320 KB |
Output is correct |
8 |
Correct |
59 ms |
70152 KB |
Output is correct |
9 |
Correct |
61 ms |
70136 KB |
Output is correct |
10 |
Correct |
59 ms |
70136 KB |
Output is correct |
11 |
Correct |
65 ms |
70300 KB |
Output is correct |
12 |
Correct |
59 ms |
70264 KB |
Output is correct |
13 |
Correct |
59 ms |
70264 KB |
Output is correct |
14 |
Correct |
59 ms |
70264 KB |
Output is correct |
15 |
Correct |
62 ms |
70264 KB |
Output is correct |
16 |
Correct |
60 ms |
70308 KB |
Output is correct |
17 |
Correct |
59 ms |
70332 KB |
Output is correct |
18 |
Correct |
62 ms |
70392 KB |
Output is correct |
19 |
Correct |
59 ms |
70264 KB |
Output is correct |
20 |
Correct |
63 ms |
70268 KB |
Output is correct |
21 |
Correct |
61 ms |
70204 KB |
Output is correct |
22 |
Correct |
59 ms |
70264 KB |
Output is correct |
23 |
Correct |
59 ms |
70296 KB |
Output is correct |
24 |
Correct |
61 ms |
70264 KB |
Output is correct |
25 |
Correct |
59 ms |
70268 KB |
Output is correct |
26 |
Correct |
59 ms |
70136 KB |
Output is correct |
27 |
Correct |
58 ms |
70264 KB |
Output is correct |
28 |
Correct |
60 ms |
70200 KB |
Output is correct |
29 |
Correct |
59 ms |
70264 KB |
Output is correct |
30 |
Correct |
63 ms |
70356 KB |
Output is correct |
31 |
Correct |
81 ms |
70264 KB |
Output is correct |
32 |
Correct |
58 ms |
70264 KB |
Output is correct |
33 |
Correct |
58 ms |
70136 KB |
Output is correct |
34 |
Correct |
69 ms |
70260 KB |
Output is correct |
35 |
Correct |
58 ms |
70260 KB |
Output is correct |
36 |
Correct |
59 ms |
70136 KB |
Output is correct |
37 |
Correct |
58 ms |
70204 KB |
Output is correct |
38 |
Correct |
59 ms |
70264 KB |
Output is correct |
39 |
Correct |
58 ms |
70264 KB |
Output is correct |
40 |
Correct |
57 ms |
70240 KB |
Output is correct |
41 |
Correct |
57 ms |
70260 KB |
Output is correct |
42 |
Correct |
57 ms |
70136 KB |
Output is correct |
43 |
Correct |
58 ms |
70264 KB |
Output is correct |
44 |
Correct |
58 ms |
70136 KB |
Output is correct |
45 |
Correct |
59 ms |
70524 KB |
Output is correct |
46 |
Correct |
68 ms |
72184 KB |
Output is correct |
47 |
Correct |
69 ms |
72184 KB |
Output is correct |
48 |
Correct |
70 ms |
72184 KB |
Output is correct |
49 |
Correct |
57 ms |
70648 KB |
Output is correct |
50 |
Correct |
57 ms |
70652 KB |
Output is correct |
51 |
Correct |
57 ms |
70648 KB |
Output is correct |
52 |
Correct |
62 ms |
71672 KB |
Output is correct |
53 |
Correct |
65 ms |
71800 KB |
Output is correct |
54 |
Correct |
62 ms |
71800 KB |
Output is correct |
55 |
Correct |
63 ms |
71800 KB |
Output is correct |
56 |
Correct |
64 ms |
71420 KB |
Output is correct |
57 |
Correct |
77 ms |
72440 KB |
Output is correct |
58 |
Correct |
79 ms |
72592 KB |
Output is correct |
59 |
Correct |
84 ms |
72440 KB |
Output is correct |
60 |
Correct |
75 ms |
72440 KB |
Output is correct |
61 |
Correct |
67 ms |
71448 KB |
Output is correct |
62 |
Correct |
63 ms |
71416 KB |
Output is correct |
63 |
Correct |
63 ms |
71544 KB |
Output is correct |
64 |
Correct |
67 ms |
72204 KB |
Output is correct |
65 |
Correct |
75 ms |
72184 KB |
Output is correct |
66 |
Correct |
75 ms |
72184 KB |
Output is correct |
67 |
Correct |
78 ms |
72184 KB |
Output is correct |
68 |
Correct |
57 ms |
70520 KB |
Output is correct |
69 |
Correct |
63 ms |
71672 KB |
Output is correct |
70 |
Correct |
61 ms |
71544 KB |
Output is correct |
71 |
Correct |
60 ms |
71416 KB |
Output is correct |
72 |
Correct |
74 ms |
72440 KB |
Output is correct |
73 |
Correct |
73 ms |
72316 KB |
Output is correct |
74 |
Correct |
61 ms |
71424 KB |
Output is correct |
75 |
Correct |
63 ms |
71416 KB |
Output is correct |
76 |
Correct |
64 ms |
71416 KB |
Output is correct |
77 |
Correct |
63 ms |
71416 KB |
Output is correct |
78 |
Correct |
62 ms |
71416 KB |
Output is correct |
79 |
Correct |
61 ms |
71416 KB |
Output is correct |
80 |
Correct |
63 ms |
71416 KB |
Output is correct |
81 |
Correct |
63 ms |
71416 KB |
Output is correct |
82 |
Correct |
64 ms |
71416 KB |
Output is correct |
83 |
Correct |
64 ms |
71544 KB |
Output is correct |
84 |
Correct |
62 ms |
71416 KB |
Output is correct |
85 |
Correct |
62 ms |
71416 KB |
Output is correct |
86 |
Correct |
62 ms |
71416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
70008 KB |
Output is correct |
2 |
Correct |
53 ms |
70008 KB |
Output is correct |
3 |
Correct |
60 ms |
70136 KB |
Output is correct |
4 |
Correct |
72 ms |
70188 KB |
Output is correct |
5 |
Correct |
60 ms |
70292 KB |
Output is correct |
6 |
Correct |
59 ms |
70264 KB |
Output is correct |
7 |
Correct |
59 ms |
70320 KB |
Output is correct |
8 |
Correct |
59 ms |
70152 KB |
Output is correct |
9 |
Correct |
61 ms |
70136 KB |
Output is correct |
10 |
Correct |
59 ms |
70136 KB |
Output is correct |
11 |
Correct |
65 ms |
70300 KB |
Output is correct |
12 |
Correct |
59 ms |
70264 KB |
Output is correct |
13 |
Correct |
59 ms |
70264 KB |
Output is correct |
14 |
Correct |
59 ms |
70264 KB |
Output is correct |
15 |
Correct |
62 ms |
70264 KB |
Output is correct |
16 |
Correct |
60 ms |
70308 KB |
Output is correct |
17 |
Correct |
59 ms |
70332 KB |
Output is correct |
18 |
Correct |
62 ms |
70392 KB |
Output is correct |
19 |
Correct |
59 ms |
70264 KB |
Output is correct |
20 |
Correct |
63 ms |
70268 KB |
Output is correct |
21 |
Correct |
61 ms |
70204 KB |
Output is correct |
22 |
Correct |
59 ms |
70264 KB |
Output is correct |
23 |
Correct |
59 ms |
70296 KB |
Output is correct |
24 |
Correct |
61 ms |
70264 KB |
Output is correct |
25 |
Correct |
59 ms |
70268 KB |
Output is correct |
26 |
Correct |
59 ms |
70136 KB |
Output is correct |
27 |
Correct |
58 ms |
70264 KB |
Output is correct |
28 |
Correct |
60 ms |
70200 KB |
Output is correct |
29 |
Correct |
59 ms |
70264 KB |
Output is correct |
30 |
Correct |
63 ms |
70356 KB |
Output is correct |
31 |
Correct |
81 ms |
70264 KB |
Output is correct |
32 |
Correct |
58 ms |
70264 KB |
Output is correct |
33 |
Correct |
58 ms |
70136 KB |
Output is correct |
34 |
Correct |
69 ms |
70260 KB |
Output is correct |
35 |
Correct |
58 ms |
70260 KB |
Output is correct |
36 |
Correct |
59 ms |
70136 KB |
Output is correct |
37 |
Correct |
58 ms |
70204 KB |
Output is correct |
38 |
Correct |
59 ms |
70264 KB |
Output is correct |
39 |
Correct |
58 ms |
70264 KB |
Output is correct |
40 |
Correct |
57 ms |
70240 KB |
Output is correct |
41 |
Correct |
57 ms |
70260 KB |
Output is correct |
42 |
Correct |
57 ms |
70136 KB |
Output is correct |
43 |
Correct |
58 ms |
70264 KB |
Output is correct |
44 |
Correct |
58 ms |
70136 KB |
Output is correct |
45 |
Correct |
59 ms |
70524 KB |
Output is correct |
46 |
Correct |
68 ms |
72184 KB |
Output is correct |
47 |
Correct |
69 ms |
72184 KB |
Output is correct |
48 |
Correct |
70 ms |
72184 KB |
Output is correct |
49 |
Correct |
57 ms |
70648 KB |
Output is correct |
50 |
Correct |
57 ms |
70652 KB |
Output is correct |
51 |
Correct |
57 ms |
70648 KB |
Output is correct |
52 |
Correct |
62 ms |
71672 KB |
Output is correct |
53 |
Correct |
65 ms |
71800 KB |
Output is correct |
54 |
Correct |
62 ms |
71800 KB |
Output is correct |
55 |
Correct |
63 ms |
71800 KB |
Output is correct |
56 |
Correct |
64 ms |
71420 KB |
Output is correct |
57 |
Correct |
77 ms |
72440 KB |
Output is correct |
58 |
Correct |
79 ms |
72592 KB |
Output is correct |
59 |
Correct |
84 ms |
72440 KB |
Output is correct |
60 |
Correct |
75 ms |
72440 KB |
Output is correct |
61 |
Correct |
67 ms |
71448 KB |
Output is correct |
62 |
Correct |
63 ms |
71416 KB |
Output is correct |
63 |
Correct |
63 ms |
71544 KB |
Output is correct |
64 |
Correct |
67 ms |
72204 KB |
Output is correct |
65 |
Correct |
75 ms |
72184 KB |
Output is correct |
66 |
Correct |
75 ms |
72184 KB |
Output is correct |
67 |
Correct |
78 ms |
72184 KB |
Output is correct |
68 |
Correct |
57 ms |
70520 KB |
Output is correct |
69 |
Correct |
63 ms |
71672 KB |
Output is correct |
70 |
Correct |
61 ms |
71544 KB |
Output is correct |
71 |
Correct |
60 ms |
71416 KB |
Output is correct |
72 |
Correct |
74 ms |
72440 KB |
Output is correct |
73 |
Correct |
73 ms |
72316 KB |
Output is correct |
74 |
Correct |
61 ms |
71424 KB |
Output is correct |
75 |
Correct |
63 ms |
71416 KB |
Output is correct |
76 |
Correct |
64 ms |
71416 KB |
Output is correct |
77 |
Correct |
63 ms |
71416 KB |
Output is correct |
78 |
Correct |
62 ms |
71416 KB |
Output is correct |
79 |
Correct |
61 ms |
71416 KB |
Output is correct |
80 |
Correct |
63 ms |
71416 KB |
Output is correct |
81 |
Correct |
63 ms |
71416 KB |
Output is correct |
82 |
Correct |
64 ms |
71416 KB |
Output is correct |
83 |
Correct |
64 ms |
71544 KB |
Output is correct |
84 |
Correct |
62 ms |
71416 KB |
Output is correct |
85 |
Correct |
62 ms |
71416 KB |
Output is correct |
86 |
Correct |
62 ms |
71416 KB |
Output is correct |
87 |
Correct |
92 ms |
75384 KB |
Output is correct |
88 |
Correct |
193 ms |
86264 KB |
Output is correct |
89 |
Correct |
627 ms |
123928 KB |
Output is correct |
90 |
Correct |
640 ms |
123760 KB |
Output is correct |
91 |
Correct |
606 ms |
123820 KB |
Output is correct |
92 |
Correct |
178 ms |
84020 KB |
Output is correct |
93 |
Correct |
202 ms |
84072 KB |
Output is correct |
94 |
Correct |
211 ms |
84080 KB |
Output is correct |
95 |
Correct |
259 ms |
111088 KB |
Output is correct |
96 |
Correct |
304 ms |
111472 KB |
Output is correct |
97 |
Correct |
269 ms |
111468 KB |
Output is correct |
98 |
Correct |
265 ms |
111504 KB |
Output is correct |
99 |
Correct |
273 ms |
106220 KB |
Output is correct |
100 |
Correct |
1105 ms |
129804 KB |
Output is correct |
101 |
Correct |
1195 ms |
130116 KB |
Output is correct |
102 |
Correct |
1216 ms |
130092 KB |
Output is correct |
103 |
Correct |
1191 ms |
130140 KB |
Output is correct |
104 |
Correct |
263 ms |
106176 KB |
Output is correct |
105 |
Correct |
268 ms |
106096 KB |
Output is correct |
106 |
Correct |
270 ms |
106256 KB |
Output is correct |
107 |
Correct |
445 ms |
122124 KB |
Output is correct |
108 |
Correct |
533 ms |
122364 KB |
Output is correct |
109 |
Correct |
635 ms |
122992 KB |
Output is correct |
110 |
Correct |
167 ms |
82672 KB |
Output is correct |
111 |
Correct |
239 ms |
111080 KB |
Output is correct |
112 |
Correct |
221 ms |
108020 KB |
Output is correct |
113 |
Correct |
225 ms |
104688 KB |
Output is correct |
114 |
Correct |
1066 ms |
129816 KB |
Output is correct |
115 |
Correct |
925 ms |
128676 KB |
Output is correct |
116 |
Correct |
243 ms |
104840 KB |
Output is correct |
117 |
Correct |
289 ms |
104560 KB |
Output is correct |
118 |
Correct |
305 ms |
104360 KB |
Output is correct |
119 |
Correct |
319 ms |
104048 KB |
Output is correct |
120 |
Correct |
228 ms |
103448 KB |
Output is correct |
121 |
Correct |
215 ms |
103148 KB |
Output is correct |
122 |
Correct |
227 ms |
102896 KB |
Output is correct |
123 |
Correct |
288 ms |
104944 KB |
Output is correct |
124 |
Correct |
279 ms |
104312 KB |
Output is correct |
125 |
Correct |
285 ms |
104052 KB |
Output is correct |
126 |
Correct |
228 ms |
103536 KB |
Output is correct |
127 |
Correct |
230 ms |
103152 KB |
Output is correct |
128 |
Correct |
228 ms |
103024 KB |
Output is correct |