#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 |
45 ms |
69964 KB |
Output is correct |
2 |
Correct |
42 ms |
69920 KB |
Output is correct |
3 |
Correct |
55 ms |
70064 KB |
Output is correct |
4 |
Correct |
69 ms |
70084 KB |
Output is correct |
5 |
Correct |
55 ms |
70276 KB |
Output is correct |
6 |
Correct |
51 ms |
70288 KB |
Output is correct |
7 |
Correct |
48 ms |
70280 KB |
Output is correct |
8 |
Correct |
53 ms |
69988 KB |
Output is correct |
9 |
Correct |
46 ms |
70072 KB |
Output is correct |
10 |
Correct |
46 ms |
70072 KB |
Output is correct |
11 |
Correct |
62 ms |
70220 KB |
Output is correct |
12 |
Correct |
46 ms |
70180 KB |
Output is correct |
13 |
Correct |
48 ms |
70212 KB |
Output is correct |
14 |
Correct |
46 ms |
70116 KB |
Output is correct |
15 |
Correct |
58 ms |
70220 KB |
Output is correct |
16 |
Correct |
50 ms |
70260 KB |
Output is correct |
17 |
Correct |
52 ms |
70308 KB |
Output is correct |
18 |
Correct |
63 ms |
70264 KB |
Output is correct |
19 |
Correct |
54 ms |
70152 KB |
Output is correct |
20 |
Correct |
46 ms |
70204 KB |
Output is correct |
21 |
Correct |
46 ms |
70348 KB |
Output is correct |
22 |
Correct |
50 ms |
70212 KB |
Output is correct |
23 |
Correct |
45 ms |
70260 KB |
Output is correct |
24 |
Correct |
48 ms |
70276 KB |
Output is correct |
25 |
Correct |
48 ms |
70212 KB |
Output is correct |
26 |
Correct |
45 ms |
70032 KB |
Output is correct |
27 |
Correct |
49 ms |
70224 KB |
Output is correct |
28 |
Correct |
45 ms |
70120 KB |
Output is correct |
29 |
Correct |
49 ms |
70188 KB |
Output is correct |
30 |
Correct |
53 ms |
70248 KB |
Output is correct |
31 |
Correct |
47 ms |
70184 KB |
Output is correct |
32 |
Correct |
49 ms |
70192 KB |
Output is correct |
33 |
Correct |
48 ms |
70124 KB |
Output is correct |
34 |
Correct |
50 ms |
70072 KB |
Output is correct |
35 |
Correct |
47 ms |
70080 KB |
Output is correct |
36 |
Correct |
46 ms |
70140 KB |
Output is correct |
37 |
Correct |
50 ms |
70124 KB |
Output is correct |
38 |
Correct |
48 ms |
70176 KB |
Output is correct |
39 |
Correct |
49 ms |
70184 KB |
Output is correct |
40 |
Correct |
52 ms |
70120 KB |
Output is correct |
41 |
Correct |
53 ms |
70184 KB |
Output is correct |
42 |
Correct |
55 ms |
70104 KB |
Output is correct |
43 |
Correct |
64 ms |
70140 KB |
Output is correct |
44 |
Correct |
50 ms |
70184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
69964 KB |
Output is correct |
2 |
Correct |
42 ms |
69920 KB |
Output is correct |
3 |
Correct |
55 ms |
70064 KB |
Output is correct |
4 |
Correct |
69 ms |
70084 KB |
Output is correct |
5 |
Correct |
55 ms |
70276 KB |
Output is correct |
6 |
Correct |
51 ms |
70288 KB |
Output is correct |
7 |
Correct |
48 ms |
70280 KB |
Output is correct |
8 |
Correct |
53 ms |
69988 KB |
Output is correct |
9 |
Correct |
46 ms |
70072 KB |
Output is correct |
10 |
Correct |
46 ms |
70072 KB |
Output is correct |
11 |
Correct |
62 ms |
70220 KB |
Output is correct |
12 |
Correct |
46 ms |
70180 KB |
Output is correct |
13 |
Correct |
48 ms |
70212 KB |
Output is correct |
14 |
Correct |
46 ms |
70116 KB |
Output is correct |
15 |
Correct |
58 ms |
70220 KB |
Output is correct |
16 |
Correct |
50 ms |
70260 KB |
Output is correct |
17 |
Correct |
52 ms |
70308 KB |
Output is correct |
18 |
Correct |
63 ms |
70264 KB |
Output is correct |
19 |
Correct |
54 ms |
70152 KB |
Output is correct |
20 |
Correct |
46 ms |
70204 KB |
Output is correct |
21 |
Correct |
46 ms |
70348 KB |
Output is correct |
22 |
Correct |
50 ms |
70212 KB |
Output is correct |
23 |
Correct |
45 ms |
70260 KB |
Output is correct |
24 |
Correct |
48 ms |
70276 KB |
Output is correct |
25 |
Correct |
48 ms |
70212 KB |
Output is correct |
26 |
Correct |
45 ms |
70032 KB |
Output is correct |
27 |
Correct |
49 ms |
70224 KB |
Output is correct |
28 |
Correct |
45 ms |
70120 KB |
Output is correct |
29 |
Correct |
49 ms |
70188 KB |
Output is correct |
30 |
Correct |
53 ms |
70248 KB |
Output is correct |
31 |
Correct |
47 ms |
70184 KB |
Output is correct |
32 |
Correct |
49 ms |
70192 KB |
Output is correct |
33 |
Correct |
48 ms |
70124 KB |
Output is correct |
34 |
Correct |
50 ms |
70072 KB |
Output is correct |
35 |
Correct |
47 ms |
70080 KB |
Output is correct |
36 |
Correct |
46 ms |
70140 KB |
Output is correct |
37 |
Correct |
50 ms |
70124 KB |
Output is correct |
38 |
Correct |
48 ms |
70176 KB |
Output is correct |
39 |
Correct |
49 ms |
70184 KB |
Output is correct |
40 |
Correct |
52 ms |
70120 KB |
Output is correct |
41 |
Correct |
53 ms |
70184 KB |
Output is correct |
42 |
Correct |
55 ms |
70104 KB |
Output is correct |
43 |
Correct |
64 ms |
70140 KB |
Output is correct |
44 |
Correct |
50 ms |
70184 KB |
Output is correct |
45 |
Correct |
47 ms |
70552 KB |
Output is correct |
46 |
Correct |
80 ms |
72116 KB |
Output is correct |
47 |
Correct |
56 ms |
72152 KB |
Output is correct |
48 |
Correct |
59 ms |
72180 KB |
Output is correct |
49 |
Correct |
61 ms |
70620 KB |
Output is correct |
50 |
Correct |
51 ms |
70676 KB |
Output is correct |
51 |
Correct |
45 ms |
70596 KB |
Output is correct |
52 |
Correct |
52 ms |
71676 KB |
Output is correct |
53 |
Correct |
65 ms |
71748 KB |
Output is correct |
54 |
Correct |
73 ms |
71664 KB |
Output is correct |
55 |
Correct |
59 ms |
71704 KB |
Output is correct |
56 |
Correct |
54 ms |
71504 KB |
Output is correct |
57 |
Correct |
61 ms |
72312 KB |
Output is correct |
58 |
Correct |
68 ms |
72388 KB |
Output is correct |
59 |
Correct |
66 ms |
72288 KB |
Output is correct |
60 |
Correct |
68 ms |
72380 KB |
Output is correct |
61 |
Correct |
52 ms |
71408 KB |
Output is correct |
62 |
Correct |
64 ms |
71424 KB |
Output is correct |
63 |
Correct |
56 ms |
71500 KB |
Output is correct |
64 |
Correct |
53 ms |
72048 KB |
Output is correct |
65 |
Correct |
56 ms |
72080 KB |
Output is correct |
66 |
Correct |
62 ms |
72084 KB |
Output is correct |
67 |
Correct |
61 ms |
72032 KB |
Output is correct |
68 |
Correct |
58 ms |
70620 KB |
Output is correct |
69 |
Correct |
61 ms |
71704 KB |
Output is correct |
70 |
Correct |
56 ms |
71516 KB |
Output is correct |
71 |
Correct |
49 ms |
71384 KB |
Output is correct |
72 |
Correct |
60 ms |
72360 KB |
Output is correct |
73 |
Correct |
59 ms |
72260 KB |
Output is correct |
74 |
Correct |
49 ms |
71440 KB |
Output is correct |
75 |
Correct |
52 ms |
71412 KB |
Output is correct |
76 |
Correct |
57 ms |
71392 KB |
Output is correct |
77 |
Correct |
58 ms |
71316 KB |
Output is correct |
78 |
Correct |
62 ms |
71284 KB |
Output is correct |
79 |
Correct |
65 ms |
71208 KB |
Output is correct |
80 |
Correct |
81 ms |
71312 KB |
Output is correct |
81 |
Correct |
62 ms |
71412 KB |
Output is correct |
82 |
Correct |
63 ms |
71300 KB |
Output is correct |
83 |
Correct |
60 ms |
71276 KB |
Output is correct |
84 |
Correct |
58 ms |
71288 KB |
Output is correct |
85 |
Correct |
60 ms |
71236 KB |
Output is correct |
86 |
Correct |
59 ms |
71332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
69964 KB |
Output is correct |
2 |
Correct |
42 ms |
69920 KB |
Output is correct |
3 |
Correct |
55 ms |
70064 KB |
Output is correct |
4 |
Correct |
69 ms |
70084 KB |
Output is correct |
5 |
Correct |
55 ms |
70276 KB |
Output is correct |
6 |
Correct |
51 ms |
70288 KB |
Output is correct |
7 |
Correct |
48 ms |
70280 KB |
Output is correct |
8 |
Correct |
53 ms |
69988 KB |
Output is correct |
9 |
Correct |
46 ms |
70072 KB |
Output is correct |
10 |
Correct |
46 ms |
70072 KB |
Output is correct |
11 |
Correct |
62 ms |
70220 KB |
Output is correct |
12 |
Correct |
46 ms |
70180 KB |
Output is correct |
13 |
Correct |
48 ms |
70212 KB |
Output is correct |
14 |
Correct |
46 ms |
70116 KB |
Output is correct |
15 |
Correct |
58 ms |
70220 KB |
Output is correct |
16 |
Correct |
50 ms |
70260 KB |
Output is correct |
17 |
Correct |
52 ms |
70308 KB |
Output is correct |
18 |
Correct |
63 ms |
70264 KB |
Output is correct |
19 |
Correct |
54 ms |
70152 KB |
Output is correct |
20 |
Correct |
46 ms |
70204 KB |
Output is correct |
21 |
Correct |
46 ms |
70348 KB |
Output is correct |
22 |
Correct |
50 ms |
70212 KB |
Output is correct |
23 |
Correct |
45 ms |
70260 KB |
Output is correct |
24 |
Correct |
48 ms |
70276 KB |
Output is correct |
25 |
Correct |
48 ms |
70212 KB |
Output is correct |
26 |
Correct |
45 ms |
70032 KB |
Output is correct |
27 |
Correct |
49 ms |
70224 KB |
Output is correct |
28 |
Correct |
45 ms |
70120 KB |
Output is correct |
29 |
Correct |
49 ms |
70188 KB |
Output is correct |
30 |
Correct |
53 ms |
70248 KB |
Output is correct |
31 |
Correct |
47 ms |
70184 KB |
Output is correct |
32 |
Correct |
49 ms |
70192 KB |
Output is correct |
33 |
Correct |
48 ms |
70124 KB |
Output is correct |
34 |
Correct |
50 ms |
70072 KB |
Output is correct |
35 |
Correct |
47 ms |
70080 KB |
Output is correct |
36 |
Correct |
46 ms |
70140 KB |
Output is correct |
37 |
Correct |
50 ms |
70124 KB |
Output is correct |
38 |
Correct |
48 ms |
70176 KB |
Output is correct |
39 |
Correct |
49 ms |
70184 KB |
Output is correct |
40 |
Correct |
52 ms |
70120 KB |
Output is correct |
41 |
Correct |
53 ms |
70184 KB |
Output is correct |
42 |
Correct |
55 ms |
70104 KB |
Output is correct |
43 |
Correct |
64 ms |
70140 KB |
Output is correct |
44 |
Correct |
50 ms |
70184 KB |
Output is correct |
45 |
Correct |
47 ms |
70552 KB |
Output is correct |
46 |
Correct |
80 ms |
72116 KB |
Output is correct |
47 |
Correct |
56 ms |
72152 KB |
Output is correct |
48 |
Correct |
59 ms |
72180 KB |
Output is correct |
49 |
Correct |
61 ms |
70620 KB |
Output is correct |
50 |
Correct |
51 ms |
70676 KB |
Output is correct |
51 |
Correct |
45 ms |
70596 KB |
Output is correct |
52 |
Correct |
52 ms |
71676 KB |
Output is correct |
53 |
Correct |
65 ms |
71748 KB |
Output is correct |
54 |
Correct |
73 ms |
71664 KB |
Output is correct |
55 |
Correct |
59 ms |
71704 KB |
Output is correct |
56 |
Correct |
54 ms |
71504 KB |
Output is correct |
57 |
Correct |
61 ms |
72312 KB |
Output is correct |
58 |
Correct |
68 ms |
72388 KB |
Output is correct |
59 |
Correct |
66 ms |
72288 KB |
Output is correct |
60 |
Correct |
68 ms |
72380 KB |
Output is correct |
61 |
Correct |
52 ms |
71408 KB |
Output is correct |
62 |
Correct |
64 ms |
71424 KB |
Output is correct |
63 |
Correct |
56 ms |
71500 KB |
Output is correct |
64 |
Correct |
53 ms |
72048 KB |
Output is correct |
65 |
Correct |
56 ms |
72080 KB |
Output is correct |
66 |
Correct |
62 ms |
72084 KB |
Output is correct |
67 |
Correct |
61 ms |
72032 KB |
Output is correct |
68 |
Correct |
58 ms |
70620 KB |
Output is correct |
69 |
Correct |
61 ms |
71704 KB |
Output is correct |
70 |
Correct |
56 ms |
71516 KB |
Output is correct |
71 |
Correct |
49 ms |
71384 KB |
Output is correct |
72 |
Correct |
60 ms |
72360 KB |
Output is correct |
73 |
Correct |
59 ms |
72260 KB |
Output is correct |
74 |
Correct |
49 ms |
71440 KB |
Output is correct |
75 |
Correct |
52 ms |
71412 KB |
Output is correct |
76 |
Correct |
57 ms |
71392 KB |
Output is correct |
77 |
Correct |
58 ms |
71316 KB |
Output is correct |
78 |
Correct |
62 ms |
71284 KB |
Output is correct |
79 |
Correct |
65 ms |
71208 KB |
Output is correct |
80 |
Correct |
81 ms |
71312 KB |
Output is correct |
81 |
Correct |
62 ms |
71412 KB |
Output is correct |
82 |
Correct |
63 ms |
71300 KB |
Output is correct |
83 |
Correct |
60 ms |
71276 KB |
Output is correct |
84 |
Correct |
58 ms |
71288 KB |
Output is correct |
85 |
Correct |
60 ms |
71236 KB |
Output is correct |
86 |
Correct |
59 ms |
71332 KB |
Output is correct |
87 |
Correct |
74 ms |
75324 KB |
Output is correct |
88 |
Correct |
181 ms |
86164 KB |
Output is correct |
89 |
Correct |
553 ms |
123864 KB |
Output is correct |
90 |
Correct |
544 ms |
123696 KB |
Output is correct |
91 |
Correct |
539 ms |
123616 KB |
Output is correct |
92 |
Correct |
178 ms |
87156 KB |
Output is correct |
93 |
Correct |
165 ms |
87116 KB |
Output is correct |
94 |
Correct |
165 ms |
87112 KB |
Output is correct |
95 |
Correct |
181 ms |
112612 KB |
Output is correct |
96 |
Correct |
254 ms |
112916 KB |
Output is correct |
97 |
Correct |
213 ms |
112960 KB |
Output is correct |
98 |
Correct |
272 ms |
112924 KB |
Output is correct |
99 |
Correct |
205 ms |
107740 KB |
Output is correct |
100 |
Correct |
821 ms |
129648 KB |
Output is correct |
101 |
Correct |
901 ms |
129936 KB |
Output is correct |
102 |
Correct |
1126 ms |
129992 KB |
Output is correct |
103 |
Correct |
975 ms |
129908 KB |
Output is correct |
104 |
Correct |
282 ms |
107664 KB |
Output is correct |
105 |
Correct |
213 ms |
107640 KB |
Output is correct |
106 |
Correct |
215 ms |
107656 KB |
Output is correct |
107 |
Correct |
313 ms |
122092 KB |
Output is correct |
108 |
Correct |
401 ms |
122268 KB |
Output is correct |
109 |
Correct |
492 ms |
122848 KB |
Output is correct |
110 |
Correct |
148 ms |
85864 KB |
Output is correct |
111 |
Correct |
215 ms |
112544 KB |
Output is correct |
112 |
Correct |
195 ms |
109408 KB |
Output is correct |
113 |
Correct |
169 ms |
106280 KB |
Output is correct |
114 |
Correct |
831 ms |
129692 KB |
Output is correct |
115 |
Correct |
765 ms |
128636 KB |
Output is correct |
116 |
Correct |
198 ms |
106292 KB |
Output is correct |
117 |
Correct |
263 ms |
105284 KB |
Output is correct |
118 |
Correct |
235 ms |
104612 KB |
Output is correct |
119 |
Correct |
241 ms |
104232 KB |
Output is correct |
120 |
Correct |
178 ms |
104128 KB |
Output is correct |
121 |
Correct |
185 ms |
103544 KB |
Output is correct |
122 |
Correct |
202 ms |
103256 KB |
Output is correct |
123 |
Correct |
243 ms |
105348 KB |
Output is correct |
124 |
Correct |
258 ms |
104640 KB |
Output is correct |
125 |
Correct |
229 ms |
104320 KB |
Output is correct |
126 |
Correct |
202 ms |
104228 KB |
Output is correct |
127 |
Correct |
204 ms |
103632 KB |
Output is correct |
128 |
Correct |
212 ms |
103372 KB |
Output is correct |