#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define maxn 100100
int n, a[maxn], b[maxn], c[maxn], sz[maxn], pai[maxn], ant[maxn], h[maxn], bit[maxn];
vector<int> g[maxn];
vector<pair<pair<int,int>,int>> bl[maxn];
void att(int pos, int val) {
for(int i = pos; i <= n; i+= i&-i) {
bit[i]+= val;
}
}
int qrr(int pos) {
int val = 0;
for(int i = pos; i > 0; i-= i&-i) {
val+= bit[i];
}
return val;
}
void dfshld(int u) {
pair<int,int> mx;
for(auto v : g[u]) {
mx = max(mx,mp(sz[v],v));
}
if(mx.sc == 0) return;
pai[mx.sc] = pai[u];
ant[mx.sc] = u;
dfshld(mx.sc);
for(auto v : g[u]) {
if(v == mx.sc) continue;
pai[v] = v;
ant[v] = u;
dfshld(v);
}
}
void dfssz(int u, int ant) {
sz[u] = 1;
for(auto v : g[u]) {
if(v == ant) continue;
h[v] = h[u]+1;
dfssz(v,u);
sz[u]+= sz[v];
}
}
void solve() {
cin >> n;
vector<int> cc;
for(int i = 1; i <= n; i++) {
cin >> c[i];
c[i] = -c[i];
cc.pb(c[i]);
}
sort(all(cc));
cc.erase(unique(all(cc)),cc.end());
for(int i = 1; i <= n; i++) {
c[i] = upper_bound(all(cc),c[i]) - cc.begin();
}
for(int i = 1; i <= n-1; i++) {
cin >> a[i] >> b[i];
g[a[i]].pb(b[i]);
}
dfssz(1,1);
ant[1] = 0;
pai[1] = 1;
dfshld(1);
// for(int i = 1; i <= n; i++) {
// cout << i << " " << ant[i] << " " << pai[i] << endl;
// }
bl[1].pb(mp(mp(0,0),c[1]));
//mp(mp(altura minima,altura maxima),cor)
int cnt = 0;
for(int i = 1; i <= n-1; i++) {
int u = a[i];
vector<pair<pair<int,int>,int>> cols;
int v = u;
while(v != 0) {
// cout << "- " << v << endl;
if(pai[v] != v) {
for(auto x : bl[pai[v]]) {
if(x.fr.fr <= h[v]) cols.pb(mp(mp(x.fr.fr,min(h[v],x.fr.sc)),x.sc));
}
}
else {
cols.pb(mp(mp(h[v],h[v]),bl[v].back().sc));
}
v = ant[pai[v]];
}
// cout << b[i] << " " << c[b[i]] << endl;
sort(all(cols));
int ans = 0;
for(auto X : cols) {
int qtd = X.fr.sc-X.fr.fr+1;
int col = X.sc;
// cout << " " << X.fr.fr << " " << X.fr.sc << " " << col << endl;
ans+= qtd*qrr(col-1);
att(col,qtd);
cnt++;
}
cout << ans << endl;
int ant1 = 0;
for(auto X : cols) {
int qtd = X.fr.sc-X.fr.fr+1;
int col = X.sc;
if(ant1 != col) cnt++;
ant1 = col;
att(col,-qtd);
}
//atualizacao
//bl[i] tem que estar em ordem decrescente, com o bloco do pai sendo o ultimo
v = b[i];
int newc = c[b[i]];
while(v != 0) {
// cout << " " << v << endl;
while(bl[pai[v]].size() && bl[pai[v]].back().fr.sc <= h[v]) bl[pai[v]].pop_back();
if(bl[pai[v]].size() != 0) {
auto x = bl[pai[v]].back();
// cout << " " << x.fr.fr << " " << x.fr.sc << " " << x.sc << " " << h[v] << endl;
bl[pai[v]].pop_back();
bl[pai[v]].pb(mp(mp(h[v]+1,x.fr.sc),x.sc));
}
bl[pai[v]].pb(mp(mp(h[pai[v]],h[v]),newc));
v = ant[pai[v]];
}
}
assert(cnt <= n*65);
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
//freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
5 ms |
5060 KB |
Output is correct |
10 |
Correct |
5 ms |
5068 KB |
Output is correct |
11 |
Correct |
4 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
4 ms |
5068 KB |
Output is correct |
16 |
Correct |
4 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
4 ms |
5068 KB |
Output is correct |
19 |
Correct |
3 ms |
5132 KB |
Output is correct |
20 |
Correct |
3 ms |
5068 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5068 KB |
Output is correct |
24 |
Correct |
3 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5068 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
3 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5068 KB |
Output is correct |
30 |
Correct |
3 ms |
5068 KB |
Output is correct |
31 |
Correct |
3 ms |
5068 KB |
Output is correct |
32 |
Correct |
3 ms |
5068 KB |
Output is correct |
33 |
Correct |
3 ms |
5068 KB |
Output is correct |
34 |
Correct |
3 ms |
5068 KB |
Output is correct |
35 |
Correct |
3 ms |
5068 KB |
Output is correct |
36 |
Correct |
4 ms |
5068 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
5 ms |
5104 KB |
Output is correct |
39 |
Correct |
3 ms |
5068 KB |
Output is correct |
40 |
Correct |
4 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5068 KB |
Output is correct |
42 |
Correct |
3 ms |
5068 KB |
Output is correct |
43 |
Correct |
3 ms |
5068 KB |
Output is correct |
44 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
5 ms |
5060 KB |
Output is correct |
10 |
Correct |
5 ms |
5068 KB |
Output is correct |
11 |
Correct |
4 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
4 ms |
5068 KB |
Output is correct |
16 |
Correct |
4 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
4 ms |
5068 KB |
Output is correct |
19 |
Correct |
3 ms |
5132 KB |
Output is correct |
20 |
Correct |
3 ms |
5068 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5068 KB |
Output is correct |
24 |
Correct |
3 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5068 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
3 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5068 KB |
Output is correct |
30 |
Correct |
3 ms |
5068 KB |
Output is correct |
31 |
Correct |
3 ms |
5068 KB |
Output is correct |
32 |
Correct |
3 ms |
5068 KB |
Output is correct |
33 |
Correct |
3 ms |
5068 KB |
Output is correct |
34 |
Correct |
3 ms |
5068 KB |
Output is correct |
35 |
Correct |
3 ms |
5068 KB |
Output is correct |
36 |
Correct |
4 ms |
5068 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
5 ms |
5104 KB |
Output is correct |
39 |
Correct |
3 ms |
5068 KB |
Output is correct |
40 |
Correct |
4 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5068 KB |
Output is correct |
42 |
Correct |
3 ms |
5068 KB |
Output is correct |
43 |
Correct |
3 ms |
5068 KB |
Output is correct |
44 |
Correct |
3 ms |
5068 KB |
Output is correct |
45 |
Correct |
4 ms |
5068 KB |
Output is correct |
46 |
Correct |
7 ms |
5452 KB |
Output is correct |
47 |
Correct |
9 ms |
5452 KB |
Output is correct |
48 |
Correct |
7 ms |
5452 KB |
Output is correct |
49 |
Correct |
5 ms |
5708 KB |
Output is correct |
50 |
Correct |
5 ms |
5764 KB |
Output is correct |
51 |
Correct |
7 ms |
5788 KB |
Output is correct |
52 |
Correct |
7 ms |
5708 KB |
Output is correct |
53 |
Correct |
10 ms |
5684 KB |
Output is correct |
54 |
Correct |
11 ms |
5708 KB |
Output is correct |
55 |
Correct |
8 ms |
5708 KB |
Output is correct |
56 |
Correct |
9 ms |
5580 KB |
Output is correct |
57 |
Correct |
12 ms |
5452 KB |
Output is correct |
58 |
Correct |
9 ms |
5420 KB |
Output is correct |
59 |
Correct |
9 ms |
5452 KB |
Output is correct |
60 |
Correct |
9 ms |
5488 KB |
Output is correct |
61 |
Correct |
5 ms |
5580 KB |
Output is correct |
62 |
Correct |
6 ms |
5580 KB |
Output is correct |
63 |
Correct |
6 ms |
5636 KB |
Output is correct |
64 |
Correct |
8 ms |
5496 KB |
Output is correct |
65 |
Correct |
7 ms |
5452 KB |
Output is correct |
66 |
Correct |
8 ms |
5452 KB |
Output is correct |
67 |
Correct |
8 ms |
5472 KB |
Output is correct |
68 |
Correct |
5 ms |
5708 KB |
Output is correct |
69 |
Correct |
7 ms |
5708 KB |
Output is correct |
70 |
Correct |
6 ms |
5708 KB |
Output is correct |
71 |
Correct |
8 ms |
5580 KB |
Output is correct |
72 |
Correct |
8 ms |
5524 KB |
Output is correct |
73 |
Correct |
8 ms |
5452 KB |
Output is correct |
74 |
Correct |
5 ms |
5580 KB |
Output is correct |
75 |
Correct |
7 ms |
5540 KB |
Output is correct |
76 |
Correct |
8 ms |
5488 KB |
Output is correct |
77 |
Correct |
6 ms |
5452 KB |
Output is correct |
78 |
Correct |
10 ms |
5452 KB |
Output is correct |
79 |
Correct |
6 ms |
5452 KB |
Output is correct |
80 |
Correct |
6 ms |
5452 KB |
Output is correct |
81 |
Correct |
9 ms |
5452 KB |
Output is correct |
82 |
Correct |
10 ms |
5452 KB |
Output is correct |
83 |
Correct |
9 ms |
5452 KB |
Output is correct |
84 |
Correct |
6 ms |
5504 KB |
Output is correct |
85 |
Correct |
6 ms |
5452 KB |
Output is correct |
86 |
Correct |
6 ms |
5480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
5068 KB |
Output is correct |
5 |
Correct |
3 ms |
5068 KB |
Output is correct |
6 |
Correct |
3 ms |
5068 KB |
Output is correct |
7 |
Correct |
4 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
5 ms |
5060 KB |
Output is correct |
10 |
Correct |
5 ms |
5068 KB |
Output is correct |
11 |
Correct |
4 ms |
5068 KB |
Output is correct |
12 |
Correct |
4 ms |
5068 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
4 ms |
5068 KB |
Output is correct |
16 |
Correct |
4 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
4 ms |
5068 KB |
Output is correct |
19 |
Correct |
3 ms |
5132 KB |
Output is correct |
20 |
Correct |
3 ms |
5068 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5068 KB |
Output is correct |
24 |
Correct |
3 ms |
5068 KB |
Output is correct |
25 |
Correct |
3 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5068 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
3 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5068 KB |
Output is correct |
30 |
Correct |
3 ms |
5068 KB |
Output is correct |
31 |
Correct |
3 ms |
5068 KB |
Output is correct |
32 |
Correct |
3 ms |
5068 KB |
Output is correct |
33 |
Correct |
3 ms |
5068 KB |
Output is correct |
34 |
Correct |
3 ms |
5068 KB |
Output is correct |
35 |
Correct |
3 ms |
5068 KB |
Output is correct |
36 |
Correct |
4 ms |
5068 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
5 ms |
5104 KB |
Output is correct |
39 |
Correct |
3 ms |
5068 KB |
Output is correct |
40 |
Correct |
4 ms |
5068 KB |
Output is correct |
41 |
Correct |
3 ms |
5068 KB |
Output is correct |
42 |
Correct |
3 ms |
5068 KB |
Output is correct |
43 |
Correct |
3 ms |
5068 KB |
Output is correct |
44 |
Correct |
3 ms |
5068 KB |
Output is correct |
45 |
Correct |
4 ms |
5068 KB |
Output is correct |
46 |
Correct |
7 ms |
5452 KB |
Output is correct |
47 |
Correct |
9 ms |
5452 KB |
Output is correct |
48 |
Correct |
7 ms |
5452 KB |
Output is correct |
49 |
Correct |
5 ms |
5708 KB |
Output is correct |
50 |
Correct |
5 ms |
5764 KB |
Output is correct |
51 |
Correct |
7 ms |
5788 KB |
Output is correct |
52 |
Correct |
7 ms |
5708 KB |
Output is correct |
53 |
Correct |
10 ms |
5684 KB |
Output is correct |
54 |
Correct |
11 ms |
5708 KB |
Output is correct |
55 |
Correct |
8 ms |
5708 KB |
Output is correct |
56 |
Correct |
9 ms |
5580 KB |
Output is correct |
57 |
Correct |
12 ms |
5452 KB |
Output is correct |
58 |
Correct |
9 ms |
5420 KB |
Output is correct |
59 |
Correct |
9 ms |
5452 KB |
Output is correct |
60 |
Correct |
9 ms |
5488 KB |
Output is correct |
61 |
Correct |
5 ms |
5580 KB |
Output is correct |
62 |
Correct |
6 ms |
5580 KB |
Output is correct |
63 |
Correct |
6 ms |
5636 KB |
Output is correct |
64 |
Correct |
8 ms |
5496 KB |
Output is correct |
65 |
Correct |
7 ms |
5452 KB |
Output is correct |
66 |
Correct |
8 ms |
5452 KB |
Output is correct |
67 |
Correct |
8 ms |
5472 KB |
Output is correct |
68 |
Correct |
5 ms |
5708 KB |
Output is correct |
69 |
Correct |
7 ms |
5708 KB |
Output is correct |
70 |
Correct |
6 ms |
5708 KB |
Output is correct |
71 |
Correct |
8 ms |
5580 KB |
Output is correct |
72 |
Correct |
8 ms |
5524 KB |
Output is correct |
73 |
Correct |
8 ms |
5452 KB |
Output is correct |
74 |
Correct |
5 ms |
5580 KB |
Output is correct |
75 |
Correct |
7 ms |
5540 KB |
Output is correct |
76 |
Correct |
8 ms |
5488 KB |
Output is correct |
77 |
Correct |
6 ms |
5452 KB |
Output is correct |
78 |
Correct |
10 ms |
5452 KB |
Output is correct |
79 |
Correct |
6 ms |
5452 KB |
Output is correct |
80 |
Correct |
6 ms |
5452 KB |
Output is correct |
81 |
Correct |
9 ms |
5452 KB |
Output is correct |
82 |
Correct |
10 ms |
5452 KB |
Output is correct |
83 |
Correct |
9 ms |
5452 KB |
Output is correct |
84 |
Correct |
6 ms |
5504 KB |
Output is correct |
85 |
Correct |
6 ms |
5452 KB |
Output is correct |
86 |
Correct |
6 ms |
5480 KB |
Output is correct |
87 |
Correct |
17 ms |
6092 KB |
Output is correct |
88 |
Correct |
47 ms |
8328 KB |
Output is correct |
89 |
Correct |
206 ms |
16248 KB |
Output is correct |
90 |
Correct |
259 ms |
16288 KB |
Output is correct |
91 |
Correct |
192 ms |
16288 KB |
Output is correct |
92 |
Correct |
101 ms |
23104 KB |
Output is correct |
93 |
Correct |
122 ms |
23124 KB |
Output is correct |
94 |
Correct |
88 ms |
23156 KB |
Output is correct |
95 |
Correct |
1193 ms |
22780 KB |
Output is correct |
96 |
Correct |
1067 ms |
22716 KB |
Output is correct |
97 |
Correct |
1383 ms |
22832 KB |
Output is correct |
98 |
Correct |
1062 ms |
22816 KB |
Output is correct |
99 |
Correct |
104 ms |
19384 KB |
Output is correct |
100 |
Correct |
230 ms |
16776 KB |
Output is correct |
101 |
Correct |
339 ms |
17712 KB |
Output is correct |
102 |
Correct |
381 ms |
17708 KB |
Output is correct |
103 |
Correct |
329 ms |
17724 KB |
Output is correct |
104 |
Correct |
149 ms |
20420 KB |
Output is correct |
105 |
Correct |
120 ms |
20428 KB |
Output is correct |
106 |
Correct |
110 ms |
20376 KB |
Output is correct |
107 |
Correct |
214 ms |
16516 KB |
Output is correct |
108 |
Correct |
193 ms |
16492 KB |
Output is correct |
109 |
Correct |
218 ms |
16744 KB |
Output is correct |
110 |
Correct |
108 ms |
23468 KB |
Output is correct |
111 |
Correct |
1095 ms |
23800 KB |
Output is correct |
112 |
Correct |
1231 ms |
22996 KB |
Output is correct |
113 |
Correct |
84 ms |
19612 KB |
Output is correct |
114 |
Correct |
284 ms |
17764 KB |
Output is correct |
115 |
Correct |
273 ms |
16996 KB |
Output is correct |
116 |
Correct |
110 ms |
19680 KB |
Output is correct |
117 |
Correct |
95 ms |
18432 KB |
Output is correct |
118 |
Correct |
119 ms |
17752 KB |
Output is correct |
119 |
Correct |
100 ms |
17440 KB |
Output is correct |
120 |
Correct |
134 ms |
17612 KB |
Output is correct |
121 |
Correct |
129 ms |
17056 KB |
Output is correct |
122 |
Correct |
96 ms |
16704 KB |
Output is correct |
123 |
Correct |
118 ms |
18400 KB |
Output is correct |
124 |
Correct |
121 ms |
17736 KB |
Output is correct |
125 |
Correct |
142 ms |
17580 KB |
Output is correct |
126 |
Correct |
133 ms |
17700 KB |
Output is correct |
127 |
Correct |
100 ms |
17124 KB |
Output is correct |
128 |
Correct |
107 ms |
16892 KB |
Output is correct |