#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 12, lg = 17;
int n, s, cnt, L[maxn * 4], R[maxn * 4], sum[maxn * 4], par[maxn][lg], mx[maxn * 4], st[maxn], en[maxn], h[maxn], U[maxn], V[maxn], bcnv[maxn], c[maxn];
vector <int> conn[maxn], pos;
vector <tuple<int, int, int>> seg;
bitset <maxn> marked;
map <int, int> cnv;
void make_tree(int l, int r, int ind){
int mid = (l + r) / 2;
L[ind] = l;
R[ind] = r;
if(l == r)
return;
make_tree(l, mid, ind * 2);
make_tree(mid + 1, r, ind * 2 + 1);
}
void update_tree_sum(int l, int r, int u, int k, bool add = 0){
if(r < L[u] || R[u] < l)
return;
if(l <= L[u] && R[u] <= r){
if(add == 0)
sum[u] = k;
else
sum[u] += k;
return;
}
update_tree_sum(l, r, u * 2, k, add);
update_tree_sum(l, r, u * 2 + 1, k, add);
sum[u] = (sum[u * 2] + sum[u * 2 + 1]);
}
void update_tree_mx(int l, int r, int u, int k){
if(r < L[u] || R[u] < l)
return;
if(l <= L[u] && R[u] <= r){
mx[u] = k;
return;
}
update_tree_mx(l, r, u * 2, k);
update_tree_mx(l, r, u * 2 + 1, k);
mx[u] = max(mx[u * 2], mx[u * 2 + 1]);
}
int get_sum(int l, int r, int u){
if(r < L[u] || R[u] < l)
return 0;
if(l <= L[u] && R[u] <= r)
return sum[u];
return get_sum(l, r, u * 2) + get_sum(l, r, u * 2 + 1);
}
int get_max(int l, int r, int u){
if(r < L[u] || R[u] < l)
return 0;
if(l <= L[u] && R[u] <= r)
return mx[u];
return max(get_max(l, r, u * 2), get_max(l, r, u * 2 + 1));
}
int kpar(int u, int k){
for(int i = 0; i < lg; i++)
if((k >> i) & 1)
u = par[u][i];
return u;
}
int lca(int a, int b){
if(h[a] < h[b])
swap(a, b);
// cout << a << '-' << b << endl;
// cout << h[a] << ' ' << h[b] << endl;
a = kpar(a, h[a] - h[b]);
if(a == b)
return a;
//cout << a << '-' << b << endl;
for(int i = lg - 1; i >= 0; i--)
if(par[a][i] != par[b][i]){
a = par[a][i];
b = par[b][i];
}
return par[a][0];
}
void dfs_set(int u){
marked[u] = 1;
for(int i = 1; i < lg; i++)
par[u][i] = par[par[u][i - 1]][i - 1];
st[u] = s;
s += 1;
for(int v: conn[u])
if(!marked[v]){
h[v] = h[u] + 1;
// cout << u << ' ' << v << ' ' << h[v] << endl;
par[v][0] = u;
dfs_set(v);
}
en[u] = s - 1;
}
void get_seg(int u){
seg.clear();
int now = 1, res = -1;
while(res != u){
// cout << st[now] << ' ' << en[now] << endl;
res = get_max(st[now], en[now], 1);
// cout << res << endl;
res = bcnv[res];
// cout << res << endl;
//cout << u << ' ' << res << endl;
int nc = c[res];
res = lca(res, u);
// cout << res << endl;
// cout << now << ' ' << res << endl;
seg.push_back(make_tuple(now, res, nc));
if(res != u)
now = kpar(u, h[u] - h[res] - 1);
//cout << now << endl;
}
return;
}
long long get_ans(){
long long nat = 0;
for(auto i: seg){
// cout << get<0>(i) << ' ' << get<1>(i) << ' ' << get<2>(i) << endl;
cnt += 1;
int len = h[get<1>(i)] - h[get<0>(i)] + 1;
nat += get_sum(get<2>(i) + 1, n, 1) * len;
update_tree_sum(get<2>(i), get<2>(i), 1, len, 1);
}
for(auto i: seg)
update_tree_sum(get<2>(i), get<2>(i), 1, 0);
return nat;
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(0);
cin >> n;
for(int i = 0; i < n; i++){
cin >> c[i + 1];
pos.push_back(c[i + 1]);
}
sort(pos.begin(), pos.end());
int now = 1;
for(int i: pos)
if(cnv[i] == 0){
cnv[i] = now;
now += 1;
}
for(int i = 1; i <= n; i++)
c[i] = cnv[c[i]];
for(int i = 0; i < n - 1; i++){
cin >> U[i] >> V[i];
conn[U[i]].push_back(V[i]);
conn[V[i]].push_back(U[i]);
}
dfs_set(1);
make_tree(0, n, 1);
update_tree_mx(st[1], st[1], 1, 1);
bcnv[1] = 1;
for(int i = 0; i < n - 1; i++){
long long ans = 0;
get_seg(U[i]);
ans += get_ans();
// cout << st[V[i]] << endl;
update_tree_mx(st[V[i]], st[V[i]], 1, i + 2);
// cout << get_max(0, 4, 1) << endl;
bcnv[i + 2] = V[i];
cout << ans << "\n";
// cout << endl;
// cout << endl;
//if(i == 2)
// return 0;
if(cnt > 18 * n)
cout << (10 / 0) << endl;
}
}
Compilation message
construction.cpp: In function 'int main()':
construction.cpp:179:25: warning: division by zero [-Wdiv-by-zero]
179 | cout << (10 / 0) << endl;
| ~~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5196 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
6 ms |
5204 KB |
Output is correct |
16 |
Correct |
6 ms |
5204 KB |
Output is correct |
17 |
Correct |
6 ms |
5196 KB |
Output is correct |
18 |
Correct |
6 ms |
5076 KB |
Output is correct |
19 |
Correct |
5 ms |
5208 KB |
Output is correct |
20 |
Correct |
4 ms |
5204 KB |
Output is correct |
21 |
Correct |
4 ms |
5204 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
6 ms |
5204 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
5 ms |
5204 KB |
Output is correct |
31 |
Correct |
5 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5144 KB |
Output is correct |
33 |
Correct |
4 ms |
5204 KB |
Output is correct |
34 |
Correct |
5 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5204 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
4 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
5 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5196 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
6 ms |
5204 KB |
Output is correct |
16 |
Correct |
6 ms |
5204 KB |
Output is correct |
17 |
Correct |
6 ms |
5196 KB |
Output is correct |
18 |
Correct |
6 ms |
5076 KB |
Output is correct |
19 |
Correct |
5 ms |
5208 KB |
Output is correct |
20 |
Correct |
4 ms |
5204 KB |
Output is correct |
21 |
Correct |
4 ms |
5204 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
6 ms |
5204 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
5 ms |
5204 KB |
Output is correct |
31 |
Correct |
5 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5144 KB |
Output is correct |
33 |
Correct |
4 ms |
5204 KB |
Output is correct |
34 |
Correct |
5 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5204 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
4 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
5 ms |
5076 KB |
Output is correct |
45 |
Correct |
6 ms |
5204 KB |
Output is correct |
46 |
Correct |
18 ms |
5920 KB |
Output is correct |
47 |
Correct |
19 ms |
5932 KB |
Output is correct |
48 |
Correct |
18 ms |
5844 KB |
Output is correct |
49 |
Correct |
9 ms |
6100 KB |
Output is correct |
50 |
Correct |
9 ms |
6100 KB |
Output is correct |
51 |
Correct |
10 ms |
6100 KB |
Output is correct |
52 |
Correct |
11 ms |
6008 KB |
Output is correct |
53 |
Correct |
12 ms |
6100 KB |
Output is correct |
54 |
Correct |
12 ms |
6040 KB |
Output is correct |
55 |
Correct |
10 ms |
6036 KB |
Output is correct |
56 |
Correct |
10 ms |
5972 KB |
Output is correct |
57 |
Correct |
30 ms |
5916 KB |
Output is correct |
58 |
Correct |
33 ms |
5924 KB |
Output is correct |
59 |
Correct |
34 ms |
5944 KB |
Output is correct |
60 |
Correct |
39 ms |
5844 KB |
Output is correct |
61 |
Correct |
13 ms |
6020 KB |
Output is correct |
62 |
Correct |
11 ms |
5972 KB |
Output is correct |
63 |
Correct |
11 ms |
5972 KB |
Output is correct |
64 |
Correct |
15 ms |
5716 KB |
Output is correct |
65 |
Correct |
19 ms |
5716 KB |
Output is correct |
66 |
Correct |
17 ms |
5716 KB |
Output is correct |
67 |
Correct |
23 ms |
5804 KB |
Output is correct |
68 |
Correct |
9 ms |
5908 KB |
Output is correct |
69 |
Correct |
11 ms |
6052 KB |
Output is correct |
70 |
Correct |
9 ms |
5844 KB |
Output is correct |
71 |
Correct |
8 ms |
5864 KB |
Output is correct |
72 |
Correct |
29 ms |
5932 KB |
Output is correct |
73 |
Correct |
28 ms |
5720 KB |
Output is correct |
74 |
Correct |
10 ms |
5844 KB |
Output is correct |
75 |
Correct |
11 ms |
5988 KB |
Output is correct |
76 |
Correct |
10 ms |
5972 KB |
Output is correct |
77 |
Correct |
10 ms |
5972 KB |
Output is correct |
78 |
Correct |
8 ms |
5716 KB |
Output is correct |
79 |
Correct |
10 ms |
5716 KB |
Output is correct |
80 |
Correct |
8 ms |
5744 KB |
Output is correct |
81 |
Correct |
12 ms |
5964 KB |
Output is correct |
82 |
Correct |
12 ms |
5972 KB |
Output is correct |
83 |
Correct |
14 ms |
5960 KB |
Output is correct |
84 |
Correct |
10 ms |
5716 KB |
Output is correct |
85 |
Correct |
11 ms |
5716 KB |
Output is correct |
86 |
Correct |
10 ms |
5716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
4 ms |
5196 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5204 KB |
Output is correct |
12 |
Correct |
3 ms |
5204 KB |
Output is correct |
13 |
Correct |
3 ms |
5204 KB |
Output is correct |
14 |
Correct |
4 ms |
5204 KB |
Output is correct |
15 |
Correct |
6 ms |
5204 KB |
Output is correct |
16 |
Correct |
6 ms |
5204 KB |
Output is correct |
17 |
Correct |
6 ms |
5196 KB |
Output is correct |
18 |
Correct |
6 ms |
5076 KB |
Output is correct |
19 |
Correct |
5 ms |
5208 KB |
Output is correct |
20 |
Correct |
4 ms |
5204 KB |
Output is correct |
21 |
Correct |
4 ms |
5204 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
4 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
6 ms |
5204 KB |
Output is correct |
28 |
Correct |
3 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
5 ms |
5204 KB |
Output is correct |
31 |
Correct |
5 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5144 KB |
Output is correct |
33 |
Correct |
4 ms |
5204 KB |
Output is correct |
34 |
Correct |
5 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5076 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
5076 KB |
Output is correct |
38 |
Correct |
3 ms |
5076 KB |
Output is correct |
39 |
Correct |
3 ms |
5204 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5076 KB |
Output is correct |
42 |
Correct |
4 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5076 KB |
Output is correct |
44 |
Correct |
5 ms |
5076 KB |
Output is correct |
45 |
Correct |
6 ms |
5204 KB |
Output is correct |
46 |
Correct |
18 ms |
5920 KB |
Output is correct |
47 |
Correct |
19 ms |
5932 KB |
Output is correct |
48 |
Correct |
18 ms |
5844 KB |
Output is correct |
49 |
Correct |
9 ms |
6100 KB |
Output is correct |
50 |
Correct |
9 ms |
6100 KB |
Output is correct |
51 |
Correct |
10 ms |
6100 KB |
Output is correct |
52 |
Correct |
11 ms |
6008 KB |
Output is correct |
53 |
Correct |
12 ms |
6100 KB |
Output is correct |
54 |
Correct |
12 ms |
6040 KB |
Output is correct |
55 |
Correct |
10 ms |
6036 KB |
Output is correct |
56 |
Correct |
10 ms |
5972 KB |
Output is correct |
57 |
Correct |
30 ms |
5916 KB |
Output is correct |
58 |
Correct |
33 ms |
5924 KB |
Output is correct |
59 |
Correct |
34 ms |
5944 KB |
Output is correct |
60 |
Correct |
39 ms |
5844 KB |
Output is correct |
61 |
Correct |
13 ms |
6020 KB |
Output is correct |
62 |
Correct |
11 ms |
5972 KB |
Output is correct |
63 |
Correct |
11 ms |
5972 KB |
Output is correct |
64 |
Correct |
15 ms |
5716 KB |
Output is correct |
65 |
Correct |
19 ms |
5716 KB |
Output is correct |
66 |
Correct |
17 ms |
5716 KB |
Output is correct |
67 |
Correct |
23 ms |
5804 KB |
Output is correct |
68 |
Correct |
9 ms |
5908 KB |
Output is correct |
69 |
Correct |
11 ms |
6052 KB |
Output is correct |
70 |
Correct |
9 ms |
5844 KB |
Output is correct |
71 |
Correct |
8 ms |
5864 KB |
Output is correct |
72 |
Correct |
29 ms |
5932 KB |
Output is correct |
73 |
Correct |
28 ms |
5720 KB |
Output is correct |
74 |
Correct |
10 ms |
5844 KB |
Output is correct |
75 |
Correct |
11 ms |
5988 KB |
Output is correct |
76 |
Correct |
10 ms |
5972 KB |
Output is correct |
77 |
Correct |
10 ms |
5972 KB |
Output is correct |
78 |
Correct |
8 ms |
5716 KB |
Output is correct |
79 |
Correct |
10 ms |
5716 KB |
Output is correct |
80 |
Correct |
8 ms |
5744 KB |
Output is correct |
81 |
Correct |
12 ms |
5964 KB |
Output is correct |
82 |
Correct |
12 ms |
5972 KB |
Output is correct |
83 |
Correct |
14 ms |
5960 KB |
Output is correct |
84 |
Correct |
10 ms |
5716 KB |
Output is correct |
85 |
Correct |
11 ms |
5716 KB |
Output is correct |
86 |
Correct |
10 ms |
5716 KB |
Output is correct |
87 |
Correct |
57 ms |
7372 KB |
Output is correct |
88 |
Correct |
222 ms |
11640 KB |
Output is correct |
89 |
Correct |
855 ms |
27112 KB |
Output is correct |
90 |
Correct |
1006 ms |
27272 KB |
Output is correct |
91 |
Correct |
961 ms |
27160 KB |
Output is correct |
92 |
Correct |
249 ms |
33272 KB |
Output is correct |
93 |
Correct |
241 ms |
33164 KB |
Output is correct |
94 |
Correct |
238 ms |
33172 KB |
Output is correct |
95 |
Correct |
299 ms |
31260 KB |
Output is correct |
96 |
Correct |
322 ms |
31388 KB |
Output is correct |
97 |
Correct |
303 ms |
31356 KB |
Output is correct |
98 |
Correct |
364 ms |
31308 KB |
Output is correct |
99 |
Correct |
317 ms |
30340 KB |
Output is correct |
100 |
Correct |
1719 ms |
27192 KB |
Output is correct |
101 |
Execution timed out |
2064 ms |
27080 KB |
Time limit exceeded |
102 |
Halted |
0 ms |
0 KB |
- |