#include<bits/stdc++.h>
#define ll int
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ld long double
#define sz(a) ((ll)(a).size())
using namespace std;
const ll maxn=100005;
vector <pll> edges;
vector <ll> A[maxn];
ll a[maxn], h[maxn], dep[maxn];
ll Time, tin[maxn], tout[maxn], pa[maxn];
void dfs(ll u)
{
tin[u]=++Time;
for (ll v:A[u])
if (v!=pa[u])
dep[v]=dep[u]+1, pa[v]=u, dfs(v);
tout[u]=Time;
}
namespace SegTree
{
ll n;
pll St[maxn*4];
void init(ll sz){n=sz;}
void update(ll i, ll Start, ll End, ll idx, pll v)
{
if (Start==End) {St[i]=v; return;}
ll mid=(Start+End)/2;
if (mid>=idx) update(i*2, Start, mid, idx, v);
else update(i*2+1, mid+1, End, idx, v);
St[i]=max(St[i*2], St[i*2+1]);
}
void update(ll idx, pll v) {update(1, 1, n, idx, v);}
pll query(ll i, ll Start, ll End, ll l, ll r)
{
if (Start>r || End<l) return {0, 0};
if (Start>=l && End<=r) return St[i];
ll mid=(Start+End)/2;
if (mid<l) return query(i*2+1, mid+1, End, l, r);
if (mid+1>r) return query(i*2, Start, mid, l, r);
return max(query(i*2, Start, mid, l, r), query(i*2+1, mid+1, End, l, r));
}
pll query(ll l, ll r){return query(1, 1, n, l, r);}
}
struct BIT
{
ll n;
vector <ll> bit;
BIT (ll n): n(n){bit.assign(n+1, 0);}
void update(ll i, ll val)
{
for (; i<=n; i+=i&(-i))
bit[i]+=val;
}
ll query(ll l, ll r)
{
ll ans=0; l--;
for (; r; r-=r&(-r)) ans+=bit[r];
for (; l; l-=l&(-l)) ans-=bit[l];
return ans;
}
};
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
ll n; cin >> n; SegTree::init(n);
for (ll i=1; i<=n; i++) cin >> a[i], h[i]=1;
edges.resize(n-1);
for (auto &[u, v]:edges)
cin >> u >> v, A[u].pb(v), A[v].pb(u);
dfs(1), SegTree::update(tin[1], {0, 1}), h[1]=1;
for (ll i=1; i<=n; i++)
{
for (ll &j:A[i]) if (j==pa[i])
{swap(j, A[i].back()), A[i].pop_back(); break;}
sort(A[i].begin(), A[i].end(), [&](ll a, ll b){return tin[a]<tin[b];});
}
for (ll i=0; i<sz(edges); i++)
{
ll U=edges[i].fi, V=edges[i].se, crr=U;
vector <ll> v;
vector <pll> num;
while (crr)
{
ll x=SegTree::query(tin[crr], tout[crr]).se, top=h[x];
if (x!=crr)
{
ll p=upper_bound(A[crr].begin(), A[crr].end(), x, [&](ll a, ll b){return tin[a]<tin[b];})-A[crr].begin();
h[x]=A[crr][p-1];
}
num.pb({dep[crr]-dep[top]+1, a[x]}), v.pb(a[x]), crr=pa[top];
}
SegTree::update(tin[V], {i+1, V});
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end())-v.begin());
ll k=sz(v), ans=0;
BIT B(k);
for (auto [cnt, x]:num)
{
x=lower_bound(v.begin(), v.end(), x)-v.begin()+1;
ans+=B.query(1, x-1)*cnt, B.update(x, cnt);
}
cout << ans << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5976 KB |
Output is correct |
2 |
Correct |
1 ms |
5980 KB |
Output is correct |
3 |
Correct |
1 ms |
6068 KB |
Output is correct |
4 |
Correct |
2 ms |
5980 KB |
Output is correct |
5 |
Correct |
3 ms |
5980 KB |
Output is correct |
6 |
Correct |
2 ms |
5980 KB |
Output is correct |
7 |
Correct |
3 ms |
5980 KB |
Output is correct |
8 |
Correct |
2 ms |
6060 KB |
Output is correct |
9 |
Correct |
3 ms |
5976 KB |
Output is correct |
10 |
Correct |
2 ms |
5976 KB |
Output is correct |
11 |
Correct |
2 ms |
5980 KB |
Output is correct |
12 |
Correct |
2 ms |
5980 KB |
Output is correct |
13 |
Correct |
2 ms |
6004 KB |
Output is correct |
14 |
Correct |
2 ms |
5980 KB |
Output is correct |
15 |
Correct |
2 ms |
5980 KB |
Output is correct |
16 |
Correct |
3 ms |
5980 KB |
Output is correct |
17 |
Correct |
2 ms |
6060 KB |
Output is correct |
18 |
Correct |
3 ms |
5980 KB |
Output is correct |
19 |
Correct |
2 ms |
5980 KB |
Output is correct |
20 |
Correct |
2 ms |
5980 KB |
Output is correct |
21 |
Correct |
2 ms |
5980 KB |
Output is correct |
22 |
Correct |
2 ms |
5980 KB |
Output is correct |
23 |
Correct |
2 ms |
5980 KB |
Output is correct |
24 |
Correct |
2 ms |
5980 KB |
Output is correct |
25 |
Correct |
2 ms |
5980 KB |
Output is correct |
26 |
Correct |
5 ms |
5980 KB |
Output is correct |
27 |
Correct |
3 ms |
5980 KB |
Output is correct |
28 |
Correct |
2 ms |
5980 KB |
Output is correct |
29 |
Correct |
2 ms |
5980 KB |
Output is correct |
30 |
Correct |
2 ms |
5976 KB |
Output is correct |
31 |
Correct |
2 ms |
5980 KB |
Output is correct |
32 |
Correct |
2 ms |
6236 KB |
Output is correct |
33 |
Correct |
2 ms |
6060 KB |
Output is correct |
34 |
Correct |
2 ms |
5980 KB |
Output is correct |
35 |
Correct |
2 ms |
5984 KB |
Output is correct |
36 |
Correct |
2 ms |
5984 KB |
Output is correct |
37 |
Correct |
2 ms |
5984 KB |
Output is correct |
38 |
Correct |
2 ms |
5980 KB |
Output is correct |
39 |
Correct |
3 ms |
5980 KB |
Output is correct |
40 |
Correct |
2 ms |
5980 KB |
Output is correct |
41 |
Correct |
2 ms |
6060 KB |
Output is correct |
42 |
Correct |
2 ms |
5980 KB |
Output is correct |
43 |
Correct |
2 ms |
5980 KB |
Output is correct |
44 |
Correct |
2 ms |
5980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5976 KB |
Output is correct |
2 |
Correct |
1 ms |
5980 KB |
Output is correct |
3 |
Correct |
1 ms |
6068 KB |
Output is correct |
4 |
Correct |
2 ms |
5980 KB |
Output is correct |
5 |
Correct |
3 ms |
5980 KB |
Output is correct |
6 |
Correct |
2 ms |
5980 KB |
Output is correct |
7 |
Correct |
3 ms |
5980 KB |
Output is correct |
8 |
Correct |
2 ms |
6060 KB |
Output is correct |
9 |
Correct |
3 ms |
5976 KB |
Output is correct |
10 |
Correct |
2 ms |
5976 KB |
Output is correct |
11 |
Correct |
2 ms |
5980 KB |
Output is correct |
12 |
Correct |
2 ms |
5980 KB |
Output is correct |
13 |
Correct |
2 ms |
6004 KB |
Output is correct |
14 |
Correct |
2 ms |
5980 KB |
Output is correct |
15 |
Correct |
2 ms |
5980 KB |
Output is correct |
16 |
Correct |
3 ms |
5980 KB |
Output is correct |
17 |
Correct |
2 ms |
6060 KB |
Output is correct |
18 |
Correct |
3 ms |
5980 KB |
Output is correct |
19 |
Correct |
2 ms |
5980 KB |
Output is correct |
20 |
Correct |
2 ms |
5980 KB |
Output is correct |
21 |
Correct |
2 ms |
5980 KB |
Output is correct |
22 |
Correct |
2 ms |
5980 KB |
Output is correct |
23 |
Correct |
2 ms |
5980 KB |
Output is correct |
24 |
Correct |
2 ms |
5980 KB |
Output is correct |
25 |
Correct |
2 ms |
5980 KB |
Output is correct |
26 |
Correct |
5 ms |
5980 KB |
Output is correct |
27 |
Correct |
3 ms |
5980 KB |
Output is correct |
28 |
Correct |
2 ms |
5980 KB |
Output is correct |
29 |
Correct |
2 ms |
5980 KB |
Output is correct |
30 |
Correct |
2 ms |
5976 KB |
Output is correct |
31 |
Correct |
2 ms |
5980 KB |
Output is correct |
32 |
Correct |
2 ms |
6236 KB |
Output is correct |
33 |
Correct |
2 ms |
6060 KB |
Output is correct |
34 |
Correct |
2 ms |
5980 KB |
Output is correct |
35 |
Correct |
2 ms |
5984 KB |
Output is correct |
36 |
Correct |
2 ms |
5984 KB |
Output is correct |
37 |
Correct |
2 ms |
5984 KB |
Output is correct |
38 |
Correct |
2 ms |
5980 KB |
Output is correct |
39 |
Correct |
3 ms |
5980 KB |
Output is correct |
40 |
Correct |
2 ms |
5980 KB |
Output is correct |
41 |
Correct |
2 ms |
6060 KB |
Output is correct |
42 |
Correct |
2 ms |
5980 KB |
Output is correct |
43 |
Correct |
2 ms |
5980 KB |
Output is correct |
44 |
Correct |
2 ms |
5980 KB |
Output is correct |
45 |
Correct |
3 ms |
5984 KB |
Output is correct |
46 |
Correct |
7 ms |
6232 KB |
Output is correct |
47 |
Correct |
8 ms |
6328 KB |
Output is correct |
48 |
Correct |
8 ms |
6232 KB |
Output is correct |
49 |
Correct |
4 ms |
6492 KB |
Output is correct |
50 |
Correct |
4 ms |
6492 KB |
Output is correct |
51 |
Correct |
4 ms |
6496 KB |
Output is correct |
52 |
Correct |
4 ms |
6488 KB |
Output is correct |
53 |
Correct |
5 ms |
6492 KB |
Output is correct |
54 |
Correct |
5 ms |
6496 KB |
Output is correct |
55 |
Correct |
5 ms |
6492 KB |
Output is correct |
56 |
Correct |
4 ms |
6236 KB |
Output is correct |
57 |
Correct |
9 ms |
6236 KB |
Output is correct |
58 |
Correct |
10 ms |
6236 KB |
Output is correct |
59 |
Correct |
10 ms |
6356 KB |
Output is correct |
60 |
Correct |
10 ms |
6236 KB |
Output is correct |
61 |
Correct |
5 ms |
6236 KB |
Output is correct |
62 |
Correct |
5 ms |
6488 KB |
Output is correct |
63 |
Correct |
5 ms |
6492 KB |
Output is correct |
64 |
Correct |
7 ms |
6232 KB |
Output is correct |
65 |
Correct |
7 ms |
6236 KB |
Output is correct |
66 |
Correct |
7 ms |
6236 KB |
Output is correct |
67 |
Correct |
7 ms |
6236 KB |
Output is correct |
68 |
Correct |
4 ms |
6412 KB |
Output is correct |
69 |
Correct |
6 ms |
6488 KB |
Output is correct |
70 |
Correct |
4 ms |
6236 KB |
Output is correct |
71 |
Correct |
6 ms |
6452 KB |
Output is correct |
72 |
Correct |
9 ms |
6232 KB |
Output is correct |
73 |
Correct |
10 ms |
6236 KB |
Output is correct |
74 |
Correct |
5 ms |
6236 KB |
Output is correct |
75 |
Correct |
4 ms |
6236 KB |
Output is correct |
76 |
Correct |
4 ms |
6488 KB |
Output is correct |
77 |
Correct |
4 ms |
6236 KB |
Output is correct |
78 |
Correct |
4 ms |
6152 KB |
Output is correct |
79 |
Correct |
5 ms |
6488 KB |
Output is correct |
80 |
Correct |
4 ms |
6232 KB |
Output is correct |
81 |
Correct |
5 ms |
6232 KB |
Output is correct |
82 |
Correct |
5 ms |
6236 KB |
Output is correct |
83 |
Correct |
5 ms |
6236 KB |
Output is correct |
84 |
Correct |
5 ms |
6324 KB |
Output is correct |
85 |
Correct |
6 ms |
6236 KB |
Output is correct |
86 |
Correct |
5 ms |
6236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5976 KB |
Output is correct |
2 |
Correct |
1 ms |
5980 KB |
Output is correct |
3 |
Correct |
1 ms |
6068 KB |
Output is correct |
4 |
Correct |
2 ms |
5980 KB |
Output is correct |
5 |
Correct |
3 ms |
5980 KB |
Output is correct |
6 |
Correct |
2 ms |
5980 KB |
Output is correct |
7 |
Correct |
3 ms |
5980 KB |
Output is correct |
8 |
Correct |
2 ms |
6060 KB |
Output is correct |
9 |
Correct |
3 ms |
5976 KB |
Output is correct |
10 |
Correct |
2 ms |
5976 KB |
Output is correct |
11 |
Correct |
2 ms |
5980 KB |
Output is correct |
12 |
Correct |
2 ms |
5980 KB |
Output is correct |
13 |
Correct |
2 ms |
6004 KB |
Output is correct |
14 |
Correct |
2 ms |
5980 KB |
Output is correct |
15 |
Correct |
2 ms |
5980 KB |
Output is correct |
16 |
Correct |
3 ms |
5980 KB |
Output is correct |
17 |
Correct |
2 ms |
6060 KB |
Output is correct |
18 |
Correct |
3 ms |
5980 KB |
Output is correct |
19 |
Correct |
2 ms |
5980 KB |
Output is correct |
20 |
Correct |
2 ms |
5980 KB |
Output is correct |
21 |
Correct |
2 ms |
5980 KB |
Output is correct |
22 |
Correct |
2 ms |
5980 KB |
Output is correct |
23 |
Correct |
2 ms |
5980 KB |
Output is correct |
24 |
Correct |
2 ms |
5980 KB |
Output is correct |
25 |
Correct |
2 ms |
5980 KB |
Output is correct |
26 |
Correct |
5 ms |
5980 KB |
Output is correct |
27 |
Correct |
3 ms |
5980 KB |
Output is correct |
28 |
Correct |
2 ms |
5980 KB |
Output is correct |
29 |
Correct |
2 ms |
5980 KB |
Output is correct |
30 |
Correct |
2 ms |
5976 KB |
Output is correct |
31 |
Correct |
2 ms |
5980 KB |
Output is correct |
32 |
Correct |
2 ms |
6236 KB |
Output is correct |
33 |
Correct |
2 ms |
6060 KB |
Output is correct |
34 |
Correct |
2 ms |
5980 KB |
Output is correct |
35 |
Correct |
2 ms |
5984 KB |
Output is correct |
36 |
Correct |
2 ms |
5984 KB |
Output is correct |
37 |
Correct |
2 ms |
5984 KB |
Output is correct |
38 |
Correct |
2 ms |
5980 KB |
Output is correct |
39 |
Correct |
3 ms |
5980 KB |
Output is correct |
40 |
Correct |
2 ms |
5980 KB |
Output is correct |
41 |
Correct |
2 ms |
6060 KB |
Output is correct |
42 |
Correct |
2 ms |
5980 KB |
Output is correct |
43 |
Correct |
2 ms |
5980 KB |
Output is correct |
44 |
Correct |
2 ms |
5980 KB |
Output is correct |
45 |
Correct |
3 ms |
5984 KB |
Output is correct |
46 |
Correct |
7 ms |
6232 KB |
Output is correct |
47 |
Correct |
8 ms |
6328 KB |
Output is correct |
48 |
Correct |
8 ms |
6232 KB |
Output is correct |
49 |
Correct |
4 ms |
6492 KB |
Output is correct |
50 |
Correct |
4 ms |
6492 KB |
Output is correct |
51 |
Correct |
4 ms |
6496 KB |
Output is correct |
52 |
Correct |
4 ms |
6488 KB |
Output is correct |
53 |
Correct |
5 ms |
6492 KB |
Output is correct |
54 |
Correct |
5 ms |
6496 KB |
Output is correct |
55 |
Correct |
5 ms |
6492 KB |
Output is correct |
56 |
Correct |
4 ms |
6236 KB |
Output is correct |
57 |
Correct |
9 ms |
6236 KB |
Output is correct |
58 |
Correct |
10 ms |
6236 KB |
Output is correct |
59 |
Correct |
10 ms |
6356 KB |
Output is correct |
60 |
Correct |
10 ms |
6236 KB |
Output is correct |
61 |
Correct |
5 ms |
6236 KB |
Output is correct |
62 |
Correct |
5 ms |
6488 KB |
Output is correct |
63 |
Correct |
5 ms |
6492 KB |
Output is correct |
64 |
Correct |
7 ms |
6232 KB |
Output is correct |
65 |
Correct |
7 ms |
6236 KB |
Output is correct |
66 |
Correct |
7 ms |
6236 KB |
Output is correct |
67 |
Correct |
7 ms |
6236 KB |
Output is correct |
68 |
Correct |
4 ms |
6412 KB |
Output is correct |
69 |
Correct |
6 ms |
6488 KB |
Output is correct |
70 |
Correct |
4 ms |
6236 KB |
Output is correct |
71 |
Correct |
6 ms |
6452 KB |
Output is correct |
72 |
Correct |
9 ms |
6232 KB |
Output is correct |
73 |
Correct |
10 ms |
6236 KB |
Output is correct |
74 |
Correct |
5 ms |
6236 KB |
Output is correct |
75 |
Correct |
4 ms |
6236 KB |
Output is correct |
76 |
Correct |
4 ms |
6488 KB |
Output is correct |
77 |
Correct |
4 ms |
6236 KB |
Output is correct |
78 |
Correct |
4 ms |
6152 KB |
Output is correct |
79 |
Correct |
5 ms |
6488 KB |
Output is correct |
80 |
Correct |
4 ms |
6232 KB |
Output is correct |
81 |
Correct |
5 ms |
6232 KB |
Output is correct |
82 |
Correct |
5 ms |
6236 KB |
Output is correct |
83 |
Correct |
5 ms |
6236 KB |
Output is correct |
84 |
Correct |
5 ms |
6324 KB |
Output is correct |
85 |
Correct |
6 ms |
6236 KB |
Output is correct |
86 |
Correct |
5 ms |
6236 KB |
Output is correct |
87 |
Correct |
17 ms |
8796 KB |
Output is correct |
88 |
Correct |
56 ms |
10076 KB |
Output is correct |
89 |
Correct |
280 ms |
14688 KB |
Output is correct |
90 |
Correct |
233 ms |
14672 KB |
Output is correct |
91 |
Correct |
237 ms |
14676 KB |
Output is correct |
92 |
Correct |
73 ms |
20760 KB |
Output is correct |
93 |
Correct |
68 ms |
20564 KB |
Output is correct |
94 |
Correct |
65 ms |
20560 KB |
Output is correct |
95 |
Correct |
86 ms |
17872 KB |
Output is correct |
96 |
Correct |
95 ms |
18476 KB |
Output is correct |
97 |
Correct |
101 ms |
18372 KB |
Output is correct |
98 |
Correct |
94 ms |
18352 KB |
Output is correct |
99 |
Correct |
88 ms |
17748 KB |
Output is correct |
100 |
Correct |
403 ms |
14332 KB |
Output is correct |
101 |
Correct |
417 ms |
14536 KB |
Output is correct |
102 |
Correct |
395 ms |
14932 KB |
Output is correct |
103 |
Correct |
417 ms |
14576 KB |
Output is correct |
104 |
Correct |
118 ms |
17796 KB |
Output is correct |
105 |
Correct |
106 ms |
17624 KB |
Output is correct |
106 |
Correct |
132 ms |
17748 KB |
Output is correct |
107 |
Correct |
220 ms |
13652 KB |
Output is correct |
108 |
Correct |
235 ms |
14172 KB |
Output is correct |
109 |
Correct |
249 ms |
14164 KB |
Output is correct |
110 |
Correct |
71 ms |
20332 KB |
Output is correct |
111 |
Correct |
85 ms |
18080 KB |
Output is correct |
112 |
Correct |
116 ms |
17828 KB |
Output is correct |
113 |
Correct |
98 ms |
16976 KB |
Output is correct |
114 |
Correct |
353 ms |
14164 KB |
Output is correct |
115 |
Correct |
381 ms |
13924 KB |
Output is correct |
116 |
Correct |
117 ms |
17192 KB |
Output is correct |
117 |
Correct |
86 ms |
15800 KB |
Output is correct |
118 |
Correct |
83 ms |
15252 KB |
Output is correct |
119 |
Correct |
94 ms |
14932 KB |
Output is correct |
120 |
Correct |
97 ms |
15468 KB |
Output is correct |
121 |
Correct |
81 ms |
14932 KB |
Output is correct |
122 |
Correct |
84 ms |
14744 KB |
Output is correct |
123 |
Correct |
119 ms |
15764 KB |
Output is correct |
124 |
Correct |
107 ms |
15236 KB |
Output is correct |
125 |
Correct |
113 ms |
15100 KB |
Output is correct |
126 |
Correct |
115 ms |
15512 KB |
Output is correct |
127 |
Correct |
99 ms |
15136 KB |
Output is correct |
128 |
Correct |
104 ms |
14580 KB |
Output is correct |