#include <bits/stdc++.h>
using namespace std;
long long a[100005], t[100005], sz[100005], depth[100005], par[100005], heavypar[100005], specialadj[100005], specialpar[100005], seg[400005], cnt;
vector<pair<long long, long long> > vec;
pair<long long, long long> edge[100005];
vector<long long> adj[100005];
void dfs1(long long u)
{
sz[u]=1;
for (long long v:adj[u])
{
depth[v]=depth[u]+1;
dfs1(v);
sz[u]+=sz[v];
}
}
void dfs2(long long u)
{
long long heavy=0;
t[u]=(++cnt);
for (long long v:adj[u])
if (!heavy || sz[heavy]<sz[v])
heavy=v;
if (heavy)
{
heavypar[heavy]=heavypar[u];
dfs2(heavy);
for (long long v:adj[u])
{
if (v==heavy)
continue;
heavypar[v]=v;
dfs2(v);
}
}
}
void push(long long i, long long l, long long r)
{
if (l==r || !seg[i])
return;
seg[i*2]=seg[i*2+1]=seg[i];
seg[i]=0;
}
void upd(long long i, long long l, long long r, long long ql, long long qr, long long x)
{
if (ql<=l && r<=qr)
{
seg[i]=x;
return;
}
push(i, l, r);
long long mid=(l+r)/2;
if (l<=qr && ql<=mid)
upd(i*2, l, mid, ql, qr, x);
if (mid<qr && ql<=r)
upd(i*2+1, mid+1, r, ql, qr, x);
}
long long qry(long long i, long long l, long long r, long long x)
{
if (l==r)
return seg[i];
push(i, l, r);
long long mid=(l+r)/2;
if (x<=mid)
return qry(i*2, l, mid, x);
else
return qry(i*2+1, mid+1, r, x);
}
long long calc(long long l, long long r)
{
/*cout << "calc " << l << ' ' << r << ": ";
for (long long i=l; i<=r; i++)
cout << vec[i].first << 'x' << vec[i].second << ' ';
cout << '\n';*/
if (l==r)
return 0;
long long mid=(l+r)/2, ind=0, sum=0, ans=0;
vector<pair<long long, long long> > vec1, vec2;
for (long long i=l; i<=mid; i++)
vec1.push_back(vec[i]);
for (long long i=mid+1; i<=r; i++)
vec2.push_back(vec[i]);
sort(vec1.begin(), vec1.end());
sort(vec2.begin(), vec2.end());
for (long long i=0; i<vec2.size(); i++)
{
while (ind<vec1.size() && vec1[ind].first<vec2[i].first)
{
sum+=vec1[ind].second;
ind++;
}
ans+=sum*vec2[i].second;
}
return ans+calc(l, mid)+calc(mid+1, r);
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
long long n;
cin >> n;
for (long long i=1; i<=n; i++)
cin >> a[i];
for (long long i=1; i<n; i++)
{
long long u, v;
cin >> u >> v;
edge[i]={u, v};
adj[u].push_back(v);
par[v]=u;
}
par[1]=heavypar[1]=1;
dfs1(1);
dfs2(1);
for (long long i=1; i<=n; i++)
{
upd(1, 1, n, t[i], t[i], i);
specialpar[i]=i;
}
for (long long i=1; i<n; i++)
{
long long u=edge[i].first, specialgrp=qry(1, 1, n, t[u]);
//cout << "u specialgrp " << u << ' ' << specialgrp << '\n';
vec.clear();
vec.push_back({a[specialgrp], depth[u]-depth[specialpar[specialgrp]]+1});
u=specialpar[specialgrp];
//long long tmp=0;
while (u!=1)
{
long long v=par[u], w=specialadj[v], specialgrp2=qry(1, 1, n, t[v]);
//tmp++;
//if (tmp<=10)
vec.push_back({a[specialgrp2], depth[u]-depth[specialpar[specialgrp2]]});
//cout << "u v w specialgrp specialgrp2 " << u << ' ' << v << ' ' << w << ' ' << specialgrp << ' ' << specialgrp2 << '\n';
specialadj[v]=u;
u=specialpar[specialgrp]=specialpar[specialgrp2];
specialpar[specialgrp2]=w;
}
cout << calc(0, vec.size()-1) << '\n';
u=edge[i].first;
long long v=heavypar[u];
upd(1, 1, n, t[v], t[u], edge[i].second);
while (v!=1)
{
//cout << "u v " << u << ' ' << v << '\n';
u=par[v];
v=heavypar[u];
upd(1, 1, n, t[v], t[u], edge[i].second);
}
u=edge[i].second, v=edge[i].first;
if (specialadj[v])
{
long long w=specialadj[v], specialgrp2=qry(1, 1, n, t[w]);
specialpar[specialgrp2]=w;
}
specialadj[v]=u;
specialpar[u]=1;
/*for (long long j=1; j<=n; j++)
cout << qry(1, 1, n, t[j]) << ' ';
cout << '\n';
for (long long j=1; j<=n; j++)
cout << specialadj[j] << ' ';
cout << '\n';
for (long long j=1; j<=n; j++)
cout << specialpar[j] << ' ';
cout << '\n';*/
}
}
Compilation message
construction.cpp: In function 'long long int calc(long long int, long long int)':
construction.cpp:85:26: 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]
85 | for (long long i=0; i<vec2.size(); i++)
| ~^~~~~~~~~~~~
construction.cpp:87:19: 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]
87 | while (ind<vec1.size() && vec1[ind].first<vec2[i].first)
| ~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2904 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
6 |
Correct |
2 ms |
2908 KB |
Output is correct |
7 |
Correct |
2 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
2836 KB |
Output is correct |
12 |
Correct |
1 ms |
2836 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
2 ms |
2908 KB |
Output is correct |
16 |
Correct |
3 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
1 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
2908 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2928 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2716 KB |
Output is correct |
29 |
Correct |
1 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
3 ms |
2908 KB |
Output is correct |
32 |
Correct |
1 ms |
2908 KB |
Output is correct |
33 |
Correct |
1 ms |
2932 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
35 |
Correct |
2 ms |
2908 KB |
Output is correct |
36 |
Correct |
2 ms |
2908 KB |
Output is correct |
37 |
Correct |
1 ms |
2908 KB |
Output is correct |
38 |
Correct |
2 ms |
2904 KB |
Output is correct |
39 |
Correct |
2 ms |
2908 KB |
Output is correct |
40 |
Correct |
2 ms |
2904 KB |
Output is correct |
41 |
Correct |
2 ms |
2904 KB |
Output is correct |
42 |
Correct |
2 ms |
2904 KB |
Output is correct |
43 |
Correct |
1 ms |
2908 KB |
Output is correct |
44 |
Correct |
1 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2904 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
6 |
Correct |
2 ms |
2908 KB |
Output is correct |
7 |
Correct |
2 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
2836 KB |
Output is correct |
12 |
Correct |
1 ms |
2836 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
2 ms |
2908 KB |
Output is correct |
16 |
Correct |
3 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
1 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
2908 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2928 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2716 KB |
Output is correct |
29 |
Correct |
1 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
3 ms |
2908 KB |
Output is correct |
32 |
Correct |
1 ms |
2908 KB |
Output is correct |
33 |
Correct |
1 ms |
2932 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
35 |
Correct |
2 ms |
2908 KB |
Output is correct |
36 |
Correct |
2 ms |
2908 KB |
Output is correct |
37 |
Correct |
1 ms |
2908 KB |
Output is correct |
38 |
Correct |
2 ms |
2904 KB |
Output is correct |
39 |
Correct |
2 ms |
2908 KB |
Output is correct |
40 |
Correct |
2 ms |
2904 KB |
Output is correct |
41 |
Correct |
2 ms |
2904 KB |
Output is correct |
42 |
Correct |
2 ms |
2904 KB |
Output is correct |
43 |
Correct |
1 ms |
2908 KB |
Output is correct |
44 |
Correct |
1 ms |
2908 KB |
Output is correct |
45 |
Correct |
2 ms |
2908 KB |
Output is correct |
46 |
Correct |
6 ms |
3164 KB |
Output is correct |
47 |
Correct |
7 ms |
3164 KB |
Output is correct |
48 |
Correct |
6 ms |
3164 KB |
Output is correct |
49 |
Correct |
3 ms |
3608 KB |
Output is correct |
50 |
Correct |
3 ms |
3676 KB |
Output is correct |
51 |
Correct |
3 ms |
3492 KB |
Output is correct |
52 |
Correct |
4 ms |
3428 KB |
Output is correct |
53 |
Correct |
4 ms |
3608 KB |
Output is correct |
54 |
Correct |
4 ms |
3420 KB |
Output is correct |
55 |
Correct |
4 ms |
3420 KB |
Output is correct |
56 |
Correct |
4 ms |
3420 KB |
Output is correct |
57 |
Correct |
9 ms |
3240 KB |
Output is correct |
58 |
Correct |
10 ms |
3392 KB |
Output is correct |
59 |
Correct |
10 ms |
3164 KB |
Output is correct |
60 |
Correct |
10 ms |
3164 KB |
Output is correct |
61 |
Correct |
4 ms |
3392 KB |
Output is correct |
62 |
Correct |
4 ms |
3420 KB |
Output is correct |
63 |
Correct |
4 ms |
3424 KB |
Output is correct |
64 |
Correct |
5 ms |
3164 KB |
Output is correct |
65 |
Correct |
6 ms |
3164 KB |
Output is correct |
66 |
Correct |
6 ms |
3388 KB |
Output is correct |
67 |
Correct |
6 ms |
3388 KB |
Output is correct |
68 |
Correct |
3 ms |
3420 KB |
Output is correct |
69 |
Correct |
3 ms |
3420 KB |
Output is correct |
70 |
Correct |
4 ms |
3416 KB |
Output is correct |
71 |
Correct |
3 ms |
3420 KB |
Output is correct |
72 |
Correct |
9 ms |
3368 KB |
Output is correct |
73 |
Correct |
10 ms |
3164 KB |
Output is correct |
74 |
Correct |
3 ms |
3472 KB |
Output is correct |
75 |
Correct |
4 ms |
3420 KB |
Output is correct |
76 |
Correct |
4 ms |
3420 KB |
Output is correct |
77 |
Correct |
3 ms |
3348 KB |
Output is correct |
78 |
Correct |
3 ms |
3420 KB |
Output is correct |
79 |
Correct |
3 ms |
3160 KB |
Output is correct |
80 |
Correct |
4 ms |
3164 KB |
Output is correct |
81 |
Correct |
4 ms |
3420 KB |
Output is correct |
82 |
Correct |
4 ms |
3420 KB |
Output is correct |
83 |
Correct |
4 ms |
3164 KB |
Output is correct |
84 |
Correct |
4 ms |
3224 KB |
Output is correct |
85 |
Correct |
4 ms |
3164 KB |
Output is correct |
86 |
Correct |
5 ms |
3160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2652 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2904 KB |
Output is correct |
5 |
Correct |
2 ms |
2908 KB |
Output is correct |
6 |
Correct |
2 ms |
2908 KB |
Output is correct |
7 |
Correct |
2 ms |
2908 KB |
Output is correct |
8 |
Correct |
1 ms |
2908 KB |
Output is correct |
9 |
Correct |
2 ms |
2908 KB |
Output is correct |
10 |
Correct |
1 ms |
2908 KB |
Output is correct |
11 |
Correct |
2 ms |
2836 KB |
Output is correct |
12 |
Correct |
1 ms |
2836 KB |
Output is correct |
13 |
Correct |
2 ms |
2908 KB |
Output is correct |
14 |
Correct |
1 ms |
2908 KB |
Output is correct |
15 |
Correct |
2 ms |
2908 KB |
Output is correct |
16 |
Correct |
3 ms |
2908 KB |
Output is correct |
17 |
Correct |
2 ms |
2908 KB |
Output is correct |
18 |
Correct |
2 ms |
2908 KB |
Output is correct |
19 |
Correct |
1 ms |
2908 KB |
Output is correct |
20 |
Correct |
2 ms |
2908 KB |
Output is correct |
21 |
Correct |
1 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
2908 KB |
Output is correct |
23 |
Correct |
2 ms |
2908 KB |
Output is correct |
24 |
Correct |
2 ms |
2928 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
1 ms |
2908 KB |
Output is correct |
27 |
Correct |
2 ms |
2908 KB |
Output is correct |
28 |
Correct |
2 ms |
2716 KB |
Output is correct |
29 |
Correct |
1 ms |
2908 KB |
Output is correct |
30 |
Correct |
2 ms |
2908 KB |
Output is correct |
31 |
Correct |
3 ms |
2908 KB |
Output is correct |
32 |
Correct |
1 ms |
2908 KB |
Output is correct |
33 |
Correct |
1 ms |
2932 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
35 |
Correct |
2 ms |
2908 KB |
Output is correct |
36 |
Correct |
2 ms |
2908 KB |
Output is correct |
37 |
Correct |
1 ms |
2908 KB |
Output is correct |
38 |
Correct |
2 ms |
2904 KB |
Output is correct |
39 |
Correct |
2 ms |
2908 KB |
Output is correct |
40 |
Correct |
2 ms |
2904 KB |
Output is correct |
41 |
Correct |
2 ms |
2904 KB |
Output is correct |
42 |
Correct |
2 ms |
2904 KB |
Output is correct |
43 |
Correct |
1 ms |
2908 KB |
Output is correct |
44 |
Correct |
1 ms |
2908 KB |
Output is correct |
45 |
Correct |
2 ms |
2908 KB |
Output is correct |
46 |
Correct |
6 ms |
3164 KB |
Output is correct |
47 |
Correct |
7 ms |
3164 KB |
Output is correct |
48 |
Correct |
6 ms |
3164 KB |
Output is correct |
49 |
Correct |
3 ms |
3608 KB |
Output is correct |
50 |
Correct |
3 ms |
3676 KB |
Output is correct |
51 |
Correct |
3 ms |
3492 KB |
Output is correct |
52 |
Correct |
4 ms |
3428 KB |
Output is correct |
53 |
Correct |
4 ms |
3608 KB |
Output is correct |
54 |
Correct |
4 ms |
3420 KB |
Output is correct |
55 |
Correct |
4 ms |
3420 KB |
Output is correct |
56 |
Correct |
4 ms |
3420 KB |
Output is correct |
57 |
Correct |
9 ms |
3240 KB |
Output is correct |
58 |
Correct |
10 ms |
3392 KB |
Output is correct |
59 |
Correct |
10 ms |
3164 KB |
Output is correct |
60 |
Correct |
10 ms |
3164 KB |
Output is correct |
61 |
Correct |
4 ms |
3392 KB |
Output is correct |
62 |
Correct |
4 ms |
3420 KB |
Output is correct |
63 |
Correct |
4 ms |
3424 KB |
Output is correct |
64 |
Correct |
5 ms |
3164 KB |
Output is correct |
65 |
Correct |
6 ms |
3164 KB |
Output is correct |
66 |
Correct |
6 ms |
3388 KB |
Output is correct |
67 |
Correct |
6 ms |
3388 KB |
Output is correct |
68 |
Correct |
3 ms |
3420 KB |
Output is correct |
69 |
Correct |
3 ms |
3420 KB |
Output is correct |
70 |
Correct |
4 ms |
3416 KB |
Output is correct |
71 |
Correct |
3 ms |
3420 KB |
Output is correct |
72 |
Correct |
9 ms |
3368 KB |
Output is correct |
73 |
Correct |
10 ms |
3164 KB |
Output is correct |
74 |
Correct |
3 ms |
3472 KB |
Output is correct |
75 |
Correct |
4 ms |
3420 KB |
Output is correct |
76 |
Correct |
4 ms |
3420 KB |
Output is correct |
77 |
Correct |
3 ms |
3348 KB |
Output is correct |
78 |
Correct |
3 ms |
3420 KB |
Output is correct |
79 |
Correct |
3 ms |
3160 KB |
Output is correct |
80 |
Correct |
4 ms |
3164 KB |
Output is correct |
81 |
Correct |
4 ms |
3420 KB |
Output is correct |
82 |
Correct |
4 ms |
3420 KB |
Output is correct |
83 |
Correct |
4 ms |
3164 KB |
Output is correct |
84 |
Correct |
4 ms |
3224 KB |
Output is correct |
85 |
Correct |
4 ms |
3164 KB |
Output is correct |
86 |
Correct |
5 ms |
3160 KB |
Output is correct |
87 |
Correct |
18 ms |
4188 KB |
Output is correct |
88 |
Correct |
53 ms |
6924 KB |
Output is correct |
89 |
Correct |
221 ms |
16620 KB |
Output is correct |
90 |
Correct |
218 ms |
16724 KB |
Output is correct |
91 |
Correct |
206 ms |
16760 KB |
Output is correct |
92 |
Correct |
61 ms |
24144 KB |
Output is correct |
93 |
Correct |
60 ms |
24200 KB |
Output is correct |
94 |
Correct |
61 ms |
24148 KB |
Output is correct |
95 |
Correct |
76 ms |
21440 KB |
Output is correct |
96 |
Correct |
97 ms |
21840 KB |
Output is correct |
97 |
Correct |
89 ms |
21704 KB |
Output is correct |
98 |
Correct |
96 ms |
21960 KB |
Output is correct |
99 |
Correct |
74 ms |
19540 KB |
Output is correct |
100 |
Correct |
381 ms |
16464 KB |
Output is correct |
101 |
Correct |
432 ms |
16852 KB |
Output is correct |
102 |
Correct |
403 ms |
16868 KB |
Output is correct |
103 |
Correct |
402 ms |
16868 KB |
Output is correct |
104 |
Correct |
89 ms |
19536 KB |
Output is correct |
105 |
Correct |
83 ms |
19552 KB |
Output is correct |
106 |
Correct |
81 ms |
19540 KB |
Output is correct |
107 |
Correct |
198 ms |
16064 KB |
Output is correct |
108 |
Correct |
188 ms |
16100 KB |
Output is correct |
109 |
Correct |
215 ms |
16464 KB |
Output is correct |
110 |
Correct |
61 ms |
23632 KB |
Output is correct |
111 |
Correct |
73 ms |
21448 KB |
Output is correct |
112 |
Correct |
100 ms |
21108 KB |
Output is correct |
113 |
Correct |
70 ms |
18800 KB |
Output is correct |
114 |
Correct |
388 ms |
16468 KB |
Output is correct |
115 |
Correct |
405 ms |
16212 KB |
Output is correct |
116 |
Correct |
86 ms |
18976 KB |
Output is correct |
117 |
Correct |
76 ms |
17712 KB |
Output is correct |
118 |
Correct |
76 ms |
16984 KB |
Output is correct |
119 |
Correct |
73 ms |
16720 KB |
Output is correct |
120 |
Correct |
74 ms |
17252 KB |
Output is correct |
121 |
Correct |
75 ms |
16800 KB |
Output is correct |
122 |
Correct |
74 ms |
16468 KB |
Output is correct |
123 |
Correct |
95 ms |
17748 KB |
Output is correct |
124 |
Correct |
92 ms |
17236 KB |
Output is correct |
125 |
Correct |
96 ms |
16776 KB |
Output is correct |
126 |
Correct |
87 ms |
17240 KB |
Output is correct |
127 |
Correct |
84 ms |
16720 KB |
Output is correct |
128 |
Correct |
90 ms |
16588 KB |
Output is correct |