#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert
#define arr3 array<int, 3>
struct FT{
vector<int> bit;
int n;
FT(int ns){
n = ns;
bit.resize(n + 1);
}
void upd(int v, int k){
while (v <= n){
bit[v] += k;
v |= (v + 1);
}
}
int get(int v){
int out = 0;
while (v > 0){
out += bit[v];
v = (v & (v + 1)) - 1;
}
return out;
}
int get(int l, int r){
return get(r) - get(l - 1);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<int> a(n + 1), all;
for (int i = 1; i <= n; i++){
cin>>a[i];
all.pb(a[i]);
}
sort(all.begin(), all.end());
map<int, int> mp;
int i = 0, cnt = 0;
while (i < n){
int j = i + 1;
while (j < n && all[i] == all[j]){
j++;
}
mp[all[i]] = ++cnt;
i = j;
}
for (int i = 1; i <= n; i++) a[i] = mp[a[i]];
vector<int> x(n), y(n), g[n + 1];
for (int i = 1; i < n; i++){
cin>>x[i]>>y[i];
g[x[i]].pb(y[i]);
g[y[i]].pb(x[i]);
}
vector<int> sz(n + 1), d(n + 1), h(n + 1), p(n + 1);
function<void(int, int)> fill = [&](int v, int pr){
p[v] = pr;
d[v] = d[pr] + 1;
sz[v] = 1;
for (int i: g[v]){
if (i == pr) continue;
fill(i, v);
if (sz[i] > sz[h[v]]){
h[v] = i;
}
sz[v] += sz[i];
}
};
fill(1, 0);
vector<int> head(n + 1), pos(n + 1);
int timer = 0;
function<void(int, int)> fill_hld = [&](int v, int k){
head[v] = k;
pos[v] = ++timer;
if (!h[v]) return;
fill_hld(h[v], k);
for (int i: g[v]){
if (pos[i]) continue;
fill_hld(i, i);
}
};
fill_hld(1, 1);
set<arr3> st;
for (int i = 1; i <= n; i++) st.ins({pos[i], pos[i], a[i]});
vector<pii> ch;
vector<arr3> er, sg;
FT T(n);
for (int i = 1; i < n; i++){
int v = x[i];
while (v > 0){
ch.pb({pos[head[v]], pos[v]});
v = p[head[v]];
}
reverse(ch.begin(), ch.end());
for (auto [l, r]: ch){
auto it = st.lower_bound({l, 0});
while (it != st.end() && (*it)[1] <= r){
er.pb(*it);
it++;
}
for (auto f: er) st.erase(f);
it = st.lower_bound({l + 1, 0});
if (it != st.begin()){
it--;
auto [l1, r1, k] = *it;
if (l1 <= l && l <= r1){
st.erase(it);
if (l1 < l){
st.ins({l1, l - 1, k});
}
if (r < r1){
st.ins({r + 1, r1, k});
}
sg.pb({max(l1, l), min(r1, r), k});
}
}
for (auto f: er) sg.pb(f);
er.clear();
it = st.lower_bound({r + 1, 0});
if (it != st.begin()){
it--;
auto [l1, r1, k] = (*it);
if (l1 <= r && r <= r1){
st.erase(it);
assert(l1 >= l);
if (r < r1){
st.ins({r + 1, r1, k});
}
sg.pb({max(l1, l), min(r1, r), k});
}
}
}
ll out = 0;
for (auto [l, r, k]: sg){
out += 1LL * T.get(k + 1, n) * (r - l + 1);
T.upd(k, (r - l + 1));
}
for (auto [l, r, k]: sg) T.upd(k, -(r - l + 1));
cout<<out<<"\n";
for (auto [l, r]: ch) st.ins({l, r, a[y[i]]});
ch.clear(); sg.clear();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
420 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
548 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
456 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
464 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
420 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
548 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
456 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
464 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
2 ms |
584 KB |
Output is correct |
46 |
Correct |
9 ms |
1372 KB |
Output is correct |
47 |
Correct |
8 ms |
1332 KB |
Output is correct |
48 |
Correct |
8 ms |
1376 KB |
Output is correct |
49 |
Correct |
5 ms |
1588 KB |
Output is correct |
50 |
Correct |
4 ms |
1628 KB |
Output is correct |
51 |
Correct |
4 ms |
1628 KB |
Output is correct |
52 |
Correct |
3 ms |
1372 KB |
Output is correct |
53 |
Correct |
4 ms |
1548 KB |
Output is correct |
54 |
Correct |
4 ms |
1628 KB |
Output is correct |
55 |
Correct |
4 ms |
1372 KB |
Output is correct |
56 |
Correct |
4 ms |
1492 KB |
Output is correct |
57 |
Correct |
14 ms |
1348 KB |
Output is correct |
58 |
Correct |
12 ms |
1236 KB |
Output is correct |
59 |
Correct |
14 ms |
1232 KB |
Output is correct |
60 |
Correct |
12 ms |
1364 KB |
Output is correct |
61 |
Correct |
3 ms |
1368 KB |
Output is correct |
62 |
Correct |
3 ms |
1372 KB |
Output is correct |
63 |
Correct |
3 ms |
1492 KB |
Output is correct |
64 |
Correct |
8 ms |
1152 KB |
Output is correct |
65 |
Correct |
7 ms |
1116 KB |
Output is correct |
66 |
Correct |
7 ms |
1008 KB |
Output is correct |
67 |
Correct |
8 ms |
1116 KB |
Output is correct |
68 |
Correct |
4 ms |
1372 KB |
Output is correct |
69 |
Correct |
4 ms |
1536 KB |
Output is correct |
70 |
Correct |
3 ms |
1372 KB |
Output is correct |
71 |
Correct |
3 ms |
1232 KB |
Output is correct |
72 |
Correct |
11 ms |
1344 KB |
Output is correct |
73 |
Correct |
11 ms |
1116 KB |
Output is correct |
74 |
Correct |
4 ms |
1524 KB |
Output is correct |
75 |
Correct |
5 ms |
1372 KB |
Output is correct |
76 |
Correct |
4 ms |
1372 KB |
Output is correct |
77 |
Correct |
4 ms |
1404 KB |
Output is correct |
78 |
Correct |
3 ms |
1116 KB |
Output is correct |
79 |
Correct |
3 ms |
1116 KB |
Output is correct |
80 |
Correct |
3 ms |
1112 KB |
Output is correct |
81 |
Correct |
4 ms |
1372 KB |
Output is correct |
82 |
Correct |
4 ms |
1380 KB |
Output is correct |
83 |
Correct |
5 ms |
1336 KB |
Output is correct |
84 |
Correct |
3 ms |
1128 KB |
Output is correct |
85 |
Correct |
3 ms |
1116 KB |
Output is correct |
86 |
Correct |
4 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
420 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
460 KB |
Output is correct |
31 |
Correct |
1 ms |
548 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
456 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
464 KB |
Output is correct |
43 |
Correct |
1 ms |
348 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
2 ms |
584 KB |
Output is correct |
46 |
Correct |
9 ms |
1372 KB |
Output is correct |
47 |
Correct |
8 ms |
1332 KB |
Output is correct |
48 |
Correct |
8 ms |
1376 KB |
Output is correct |
49 |
Correct |
5 ms |
1588 KB |
Output is correct |
50 |
Correct |
4 ms |
1628 KB |
Output is correct |
51 |
Correct |
4 ms |
1628 KB |
Output is correct |
52 |
Correct |
3 ms |
1372 KB |
Output is correct |
53 |
Correct |
4 ms |
1548 KB |
Output is correct |
54 |
Correct |
4 ms |
1628 KB |
Output is correct |
55 |
Correct |
4 ms |
1372 KB |
Output is correct |
56 |
Correct |
4 ms |
1492 KB |
Output is correct |
57 |
Correct |
14 ms |
1348 KB |
Output is correct |
58 |
Correct |
12 ms |
1236 KB |
Output is correct |
59 |
Correct |
14 ms |
1232 KB |
Output is correct |
60 |
Correct |
12 ms |
1364 KB |
Output is correct |
61 |
Correct |
3 ms |
1368 KB |
Output is correct |
62 |
Correct |
3 ms |
1372 KB |
Output is correct |
63 |
Correct |
3 ms |
1492 KB |
Output is correct |
64 |
Correct |
8 ms |
1152 KB |
Output is correct |
65 |
Correct |
7 ms |
1116 KB |
Output is correct |
66 |
Correct |
7 ms |
1008 KB |
Output is correct |
67 |
Correct |
8 ms |
1116 KB |
Output is correct |
68 |
Correct |
4 ms |
1372 KB |
Output is correct |
69 |
Correct |
4 ms |
1536 KB |
Output is correct |
70 |
Correct |
3 ms |
1372 KB |
Output is correct |
71 |
Correct |
3 ms |
1232 KB |
Output is correct |
72 |
Correct |
11 ms |
1344 KB |
Output is correct |
73 |
Correct |
11 ms |
1116 KB |
Output is correct |
74 |
Correct |
4 ms |
1524 KB |
Output is correct |
75 |
Correct |
5 ms |
1372 KB |
Output is correct |
76 |
Correct |
4 ms |
1372 KB |
Output is correct |
77 |
Correct |
4 ms |
1404 KB |
Output is correct |
78 |
Correct |
3 ms |
1116 KB |
Output is correct |
79 |
Correct |
3 ms |
1116 KB |
Output is correct |
80 |
Correct |
3 ms |
1112 KB |
Output is correct |
81 |
Correct |
4 ms |
1372 KB |
Output is correct |
82 |
Correct |
4 ms |
1380 KB |
Output is correct |
83 |
Correct |
5 ms |
1336 KB |
Output is correct |
84 |
Correct |
3 ms |
1128 KB |
Output is correct |
85 |
Correct |
3 ms |
1116 KB |
Output is correct |
86 |
Correct |
4 ms |
1116 KB |
Output is correct |
87 |
Correct |
23 ms |
2648 KB |
Output is correct |
88 |
Correct |
89 ms |
7252 KB |
Output is correct |
89 |
Correct |
375 ms |
23708 KB |
Output is correct |
90 |
Correct |
406 ms |
23504 KB |
Output is correct |
91 |
Correct |
359 ms |
23508 KB |
Output is correct |
92 |
Correct |
130 ms |
32680 KB |
Output is correct |
93 |
Correct |
119 ms |
32684 KB |
Output is correct |
94 |
Correct |
121 ms |
32720 KB |
Output is correct |
95 |
Correct |
137 ms |
29540 KB |
Output is correct |
96 |
Correct |
128 ms |
29772 KB |
Output is correct |
97 |
Correct |
127 ms |
29772 KB |
Output is correct |
98 |
Correct |
125 ms |
29776 KB |
Output is correct |
99 |
Correct |
115 ms |
28372 KB |
Output is correct |
100 |
Correct |
655 ms |
23200 KB |
Output is correct |
101 |
Correct |
655 ms |
23504 KB |
Output is correct |
102 |
Correct |
655 ms |
23380 KB |
Output is correct |
103 |
Correct |
655 ms |
23504 KB |
Output is correct |
104 |
Correct |
122 ms |
28116 KB |
Output is correct |
105 |
Correct |
121 ms |
28220 KB |
Output is correct |
106 |
Correct |
122 ms |
28168 KB |
Output is correct |
107 |
Correct |
356 ms |
18132 KB |
Output is correct |
108 |
Correct |
327 ms |
18288 KB |
Output is correct |
109 |
Correct |
355 ms |
19804 KB |
Output is correct |
110 |
Correct |
95 ms |
27400 KB |
Output is correct |
111 |
Correct |
137 ms |
29572 KB |
Output is correct |
112 |
Correct |
105 ms |
24572 KB |
Output is correct |
113 |
Correct |
90 ms |
22992 KB |
Output is correct |
114 |
Correct |
615 ms |
23364 KB |
Output is correct |
115 |
Correct |
608 ms |
18128 KB |
Output is correct |
116 |
Correct |
92 ms |
22992 KB |
Output is correct |
117 |
Correct |
124 ms |
25412 KB |
Output is correct |
118 |
Correct |
133 ms |
24508 KB |
Output is correct |
119 |
Correct |
127 ms |
24020 KB |
Output is correct |
120 |
Correct |
98 ms |
20436 KB |
Output is correct |
121 |
Correct |
97 ms |
19664 KB |
Output is correct |
122 |
Correct |
102 ms |
19468 KB |
Output is correct |
123 |
Correct |
133 ms |
25300 KB |
Output is correct |
124 |
Correct |
144 ms |
24608 KB |
Output is correct |
125 |
Correct |
149 ms |
24272 KB |
Output is correct |
126 |
Correct |
117 ms |
20684 KB |
Output is correct |
127 |
Correct |
107 ms |
19920 KB |
Output is correct |
128 |
Correct |
110 ms |
19308 KB |
Output is correct |