/*
-------------- | /
| | /
| | /
| * |/ | | ------ *
| | | | / \
| | |\ | | | |\ |
\ | | | \ | | | | \ |
\ | | | \ | | \ / \ |
V | | \ \__/| ----- \ |
*/
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr << "\e[1;31m" << #x << " = " << (x) << "\e[0m\n"
#define print(x) emilia_mata_tenshi(#x, begin(x), end(x))
template<typename T> void emilia_mata_tenshi(const char *s, T l, T r) {
cerr << "\e[1;33m" << s << " = [";
while(l != r) {
cerr << *l;
cerr << (++l == r ? ']' : ',');
}
cerr << "\e[0m\n";
}
#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(NULL);
using ll = int64_t;
using ull = uint64_t;
using ld = long double;
using uint = uint32_t;
const double EPS = 1e-8;
const int INF = 0x3F3F3F3F;
const ll LINF = 4611686018427387903;
const int MOD = 1e9+7;
static int Lamy_is_cute = []() {
EmiliaMyWife
return 48763;
}();
/*--------------------------------------------------------------------------------------*/
const int N = 2e5 + 25;
struct segtree {
pair<int, int> arr[N << 1];
int n, t = 1;
void init(int _n) { n = _n; }
void edt(int p, int v) {
for(arr[p += n] = make_pair(t++, v); p > 1; p >>= 1)
arr[p >> 1] = max(arr[p], arr[p ^ 1]);
}
int que(int l, int r) {
pair<int, int> res;
for(l += n, r += n; l < r; l >>= 1, r >>= 1) {
if(l & 1)
res = max(res, arr[l++]);
if(r & 1)
res = max(res, arr[--r]);
}
return res.second;
}
} tree;
struct fenwicktree {
int arr[N];
void edt(int p, int v) {
for(; p < N; p += p & -p)
arr[p] += v;
}
int que(int p) {
int res = 0;
for(; p; p -= p & -p)
res += arr[p];
return res;
}
} bit; signed main() {
int n;
cin >> n;
vector<int> arr(n + 1);
for(int i = 1; i <= n; i++)
cin >> arr[i];
vector<vector<int>> edge(n + 1);
vector<pair<int, int>> que(n - 1);
for(auto &[a, b] : que) {
cin >> a >> b;
edge[a].push_back(b);
}
{
vector<int> v = arr;
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for(int &a : arr)
a = lower_bound(v.begin(), v.end(), a) - v.begin();
}
vector<int> dep(n + 1), in(n + 1), out(n + 1);
int t = 0;
function<void(int)> dfs = [&](int u) {
in[u] = t++;
for(int v : edge[u]) {
dep[v] = dep[u] + 1;
dfs(v);
}
out[u] = t++;
};
dep[1] = 1;
dfs(1);
tree.init(n * 2);
vector<int> up(n + 1);
tree.edt(up[1], 1);
for(const auto &[a, b] : que) {
vector<pair<int, int>> owo;
int x = a;
while(x) {
int r = tree.que(in[x], out[x]);
//debug(in[x]);
//debug(out[x]);
//debug(r);
owo.push_back({arr[r], dep[x] - dep[up[r]]});
swap(up[r], x);
}
tree.edt(in[b], b);
ll ans = 0;
for(const auto &[v, c] : owo) {
ans += 1LL * bit.que(v - 1) * c;
bit.edt(v, c);
}
cout << ans << '\n';
for(const auto &[v, c] : owo)
bit.edt(v, -c);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3460 KB |
Output is correct |
5 |
Correct |
2 ms |
3468 KB |
Output is correct |
6 |
Correct |
2 ms |
3540 KB |
Output is correct |
7 |
Correct |
2 ms |
3540 KB |
Output is correct |
8 |
Correct |
2 ms |
3540 KB |
Output is correct |
9 |
Correct |
2 ms |
3540 KB |
Output is correct |
10 |
Correct |
2 ms |
3468 KB |
Output is correct |
11 |
Correct |
2 ms |
3540 KB |
Output is correct |
12 |
Correct |
2 ms |
3540 KB |
Output is correct |
13 |
Correct |
2 ms |
3540 KB |
Output is correct |
14 |
Correct |
2 ms |
3540 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3468 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
3 ms |
3472 KB |
Output is correct |
19 |
Correct |
2 ms |
3464 KB |
Output is correct |
20 |
Correct |
2 ms |
3468 KB |
Output is correct |
21 |
Correct |
2 ms |
3540 KB |
Output is correct |
22 |
Correct |
2 ms |
3412 KB |
Output is correct |
23 |
Correct |
2 ms |
3412 KB |
Output is correct |
24 |
Correct |
3 ms |
3412 KB |
Output is correct |
25 |
Correct |
2 ms |
3412 KB |
Output is correct |
26 |
Correct |
3 ms |
3540 KB |
Output is correct |
27 |
Correct |
3 ms |
3540 KB |
Output is correct |
28 |
Correct |
2 ms |
3424 KB |
Output is correct |
29 |
Correct |
2 ms |
3468 KB |
Output is correct |
30 |
Correct |
3 ms |
3412 KB |
Output is correct |
31 |
Correct |
2 ms |
3464 KB |
Output is correct |
32 |
Correct |
3 ms |
3540 KB |
Output is correct |
33 |
Correct |
2 ms |
3412 KB |
Output is correct |
34 |
Correct |
2 ms |
3412 KB |
Output is correct |
35 |
Correct |
2 ms |
3460 KB |
Output is correct |
36 |
Correct |
3 ms |
3540 KB |
Output is correct |
37 |
Correct |
2 ms |
3412 KB |
Output is correct |
38 |
Correct |
2 ms |
3412 KB |
Output is correct |
39 |
Correct |
2 ms |
3540 KB |
Output is correct |
40 |
Correct |
2 ms |
3412 KB |
Output is correct |
41 |
Correct |
2 ms |
3412 KB |
Output is correct |
42 |
Correct |
2 ms |
3540 KB |
Output is correct |
43 |
Correct |
2 ms |
3468 KB |
Output is correct |
44 |
Correct |
3 ms |
3412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3460 KB |
Output is correct |
5 |
Correct |
2 ms |
3468 KB |
Output is correct |
6 |
Correct |
2 ms |
3540 KB |
Output is correct |
7 |
Correct |
2 ms |
3540 KB |
Output is correct |
8 |
Correct |
2 ms |
3540 KB |
Output is correct |
9 |
Correct |
2 ms |
3540 KB |
Output is correct |
10 |
Correct |
2 ms |
3468 KB |
Output is correct |
11 |
Correct |
2 ms |
3540 KB |
Output is correct |
12 |
Correct |
2 ms |
3540 KB |
Output is correct |
13 |
Correct |
2 ms |
3540 KB |
Output is correct |
14 |
Correct |
2 ms |
3540 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3468 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
3 ms |
3472 KB |
Output is correct |
19 |
Correct |
2 ms |
3464 KB |
Output is correct |
20 |
Correct |
2 ms |
3468 KB |
Output is correct |
21 |
Correct |
2 ms |
3540 KB |
Output is correct |
22 |
Correct |
2 ms |
3412 KB |
Output is correct |
23 |
Correct |
2 ms |
3412 KB |
Output is correct |
24 |
Correct |
3 ms |
3412 KB |
Output is correct |
25 |
Correct |
2 ms |
3412 KB |
Output is correct |
26 |
Correct |
3 ms |
3540 KB |
Output is correct |
27 |
Correct |
3 ms |
3540 KB |
Output is correct |
28 |
Correct |
2 ms |
3424 KB |
Output is correct |
29 |
Correct |
2 ms |
3468 KB |
Output is correct |
30 |
Correct |
3 ms |
3412 KB |
Output is correct |
31 |
Correct |
2 ms |
3464 KB |
Output is correct |
32 |
Correct |
3 ms |
3540 KB |
Output is correct |
33 |
Correct |
2 ms |
3412 KB |
Output is correct |
34 |
Correct |
2 ms |
3412 KB |
Output is correct |
35 |
Correct |
2 ms |
3460 KB |
Output is correct |
36 |
Correct |
3 ms |
3540 KB |
Output is correct |
37 |
Correct |
2 ms |
3412 KB |
Output is correct |
38 |
Correct |
2 ms |
3412 KB |
Output is correct |
39 |
Correct |
2 ms |
3540 KB |
Output is correct |
40 |
Correct |
2 ms |
3412 KB |
Output is correct |
41 |
Correct |
2 ms |
3412 KB |
Output is correct |
42 |
Correct |
2 ms |
3540 KB |
Output is correct |
43 |
Correct |
2 ms |
3468 KB |
Output is correct |
44 |
Correct |
3 ms |
3412 KB |
Output is correct |
45 |
Correct |
3 ms |
3480 KB |
Output is correct |
46 |
Correct |
6 ms |
3848 KB |
Output is correct |
47 |
Correct |
7 ms |
3796 KB |
Output is correct |
48 |
Correct |
7 ms |
3812 KB |
Output is correct |
49 |
Correct |
5 ms |
4188 KB |
Output is correct |
50 |
Correct |
5 ms |
4108 KB |
Output is correct |
51 |
Correct |
5 ms |
4180 KB |
Output is correct |
52 |
Correct |
5 ms |
3964 KB |
Output is correct |
53 |
Correct |
5 ms |
3924 KB |
Output is correct |
54 |
Correct |
5 ms |
3924 KB |
Output is correct |
55 |
Correct |
5 ms |
3924 KB |
Output is correct |
56 |
Correct |
5 ms |
3924 KB |
Output is correct |
57 |
Correct |
10 ms |
3796 KB |
Output is correct |
58 |
Correct |
10 ms |
3820 KB |
Output is correct |
59 |
Correct |
14 ms |
3856 KB |
Output is correct |
60 |
Correct |
12 ms |
3860 KB |
Output is correct |
61 |
Correct |
5 ms |
3924 KB |
Output is correct |
62 |
Correct |
5 ms |
3924 KB |
Output is correct |
63 |
Correct |
6 ms |
3984 KB |
Output is correct |
64 |
Correct |
6 ms |
3796 KB |
Output is correct |
65 |
Correct |
6 ms |
3796 KB |
Output is correct |
66 |
Correct |
7 ms |
3732 KB |
Output is correct |
67 |
Correct |
7 ms |
3796 KB |
Output is correct |
68 |
Correct |
4 ms |
4180 KB |
Output is correct |
69 |
Correct |
5 ms |
3984 KB |
Output is correct |
70 |
Correct |
5 ms |
3924 KB |
Output is correct |
71 |
Correct |
5 ms |
3924 KB |
Output is correct |
72 |
Correct |
11 ms |
3840 KB |
Output is correct |
73 |
Correct |
10 ms |
3796 KB |
Output is correct |
74 |
Correct |
7 ms |
3924 KB |
Output is correct |
75 |
Correct |
5 ms |
3796 KB |
Output is correct |
76 |
Correct |
5 ms |
3796 KB |
Output is correct |
77 |
Correct |
5 ms |
3856 KB |
Output is correct |
78 |
Correct |
5 ms |
3860 KB |
Output is correct |
79 |
Correct |
5 ms |
3796 KB |
Output is correct |
80 |
Correct |
4 ms |
3780 KB |
Output is correct |
81 |
Correct |
7 ms |
3920 KB |
Output is correct |
82 |
Correct |
5 ms |
3796 KB |
Output is correct |
83 |
Correct |
5 ms |
3796 KB |
Output is correct |
84 |
Correct |
6 ms |
3796 KB |
Output is correct |
85 |
Correct |
6 ms |
3856 KB |
Output is correct |
86 |
Correct |
8 ms |
3796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
2 ms |
3412 KB |
Output is correct |
3 |
Correct |
2 ms |
3412 KB |
Output is correct |
4 |
Correct |
2 ms |
3460 KB |
Output is correct |
5 |
Correct |
2 ms |
3468 KB |
Output is correct |
6 |
Correct |
2 ms |
3540 KB |
Output is correct |
7 |
Correct |
2 ms |
3540 KB |
Output is correct |
8 |
Correct |
2 ms |
3540 KB |
Output is correct |
9 |
Correct |
2 ms |
3540 KB |
Output is correct |
10 |
Correct |
2 ms |
3468 KB |
Output is correct |
11 |
Correct |
2 ms |
3540 KB |
Output is correct |
12 |
Correct |
2 ms |
3540 KB |
Output is correct |
13 |
Correct |
2 ms |
3540 KB |
Output is correct |
14 |
Correct |
2 ms |
3540 KB |
Output is correct |
15 |
Correct |
2 ms |
3412 KB |
Output is correct |
16 |
Correct |
2 ms |
3468 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
3 ms |
3472 KB |
Output is correct |
19 |
Correct |
2 ms |
3464 KB |
Output is correct |
20 |
Correct |
2 ms |
3468 KB |
Output is correct |
21 |
Correct |
2 ms |
3540 KB |
Output is correct |
22 |
Correct |
2 ms |
3412 KB |
Output is correct |
23 |
Correct |
2 ms |
3412 KB |
Output is correct |
24 |
Correct |
3 ms |
3412 KB |
Output is correct |
25 |
Correct |
2 ms |
3412 KB |
Output is correct |
26 |
Correct |
3 ms |
3540 KB |
Output is correct |
27 |
Correct |
3 ms |
3540 KB |
Output is correct |
28 |
Correct |
2 ms |
3424 KB |
Output is correct |
29 |
Correct |
2 ms |
3468 KB |
Output is correct |
30 |
Correct |
3 ms |
3412 KB |
Output is correct |
31 |
Correct |
2 ms |
3464 KB |
Output is correct |
32 |
Correct |
3 ms |
3540 KB |
Output is correct |
33 |
Correct |
2 ms |
3412 KB |
Output is correct |
34 |
Correct |
2 ms |
3412 KB |
Output is correct |
35 |
Correct |
2 ms |
3460 KB |
Output is correct |
36 |
Correct |
3 ms |
3540 KB |
Output is correct |
37 |
Correct |
2 ms |
3412 KB |
Output is correct |
38 |
Correct |
2 ms |
3412 KB |
Output is correct |
39 |
Correct |
2 ms |
3540 KB |
Output is correct |
40 |
Correct |
2 ms |
3412 KB |
Output is correct |
41 |
Correct |
2 ms |
3412 KB |
Output is correct |
42 |
Correct |
2 ms |
3540 KB |
Output is correct |
43 |
Correct |
2 ms |
3468 KB |
Output is correct |
44 |
Correct |
3 ms |
3412 KB |
Output is correct |
45 |
Correct |
3 ms |
3480 KB |
Output is correct |
46 |
Correct |
6 ms |
3848 KB |
Output is correct |
47 |
Correct |
7 ms |
3796 KB |
Output is correct |
48 |
Correct |
7 ms |
3812 KB |
Output is correct |
49 |
Correct |
5 ms |
4188 KB |
Output is correct |
50 |
Correct |
5 ms |
4108 KB |
Output is correct |
51 |
Correct |
5 ms |
4180 KB |
Output is correct |
52 |
Correct |
5 ms |
3964 KB |
Output is correct |
53 |
Correct |
5 ms |
3924 KB |
Output is correct |
54 |
Correct |
5 ms |
3924 KB |
Output is correct |
55 |
Correct |
5 ms |
3924 KB |
Output is correct |
56 |
Correct |
5 ms |
3924 KB |
Output is correct |
57 |
Correct |
10 ms |
3796 KB |
Output is correct |
58 |
Correct |
10 ms |
3820 KB |
Output is correct |
59 |
Correct |
14 ms |
3856 KB |
Output is correct |
60 |
Correct |
12 ms |
3860 KB |
Output is correct |
61 |
Correct |
5 ms |
3924 KB |
Output is correct |
62 |
Correct |
5 ms |
3924 KB |
Output is correct |
63 |
Correct |
6 ms |
3984 KB |
Output is correct |
64 |
Correct |
6 ms |
3796 KB |
Output is correct |
65 |
Correct |
6 ms |
3796 KB |
Output is correct |
66 |
Correct |
7 ms |
3732 KB |
Output is correct |
67 |
Correct |
7 ms |
3796 KB |
Output is correct |
68 |
Correct |
4 ms |
4180 KB |
Output is correct |
69 |
Correct |
5 ms |
3984 KB |
Output is correct |
70 |
Correct |
5 ms |
3924 KB |
Output is correct |
71 |
Correct |
5 ms |
3924 KB |
Output is correct |
72 |
Correct |
11 ms |
3840 KB |
Output is correct |
73 |
Correct |
10 ms |
3796 KB |
Output is correct |
74 |
Correct |
7 ms |
3924 KB |
Output is correct |
75 |
Correct |
5 ms |
3796 KB |
Output is correct |
76 |
Correct |
5 ms |
3796 KB |
Output is correct |
77 |
Correct |
5 ms |
3856 KB |
Output is correct |
78 |
Correct |
5 ms |
3860 KB |
Output is correct |
79 |
Correct |
5 ms |
3796 KB |
Output is correct |
80 |
Correct |
4 ms |
3780 KB |
Output is correct |
81 |
Correct |
7 ms |
3920 KB |
Output is correct |
82 |
Correct |
5 ms |
3796 KB |
Output is correct |
83 |
Correct |
5 ms |
3796 KB |
Output is correct |
84 |
Correct |
6 ms |
3796 KB |
Output is correct |
85 |
Correct |
6 ms |
3856 KB |
Output is correct |
86 |
Correct |
8 ms |
3796 KB |
Output is correct |
87 |
Correct |
17 ms |
4408 KB |
Output is correct |
88 |
Correct |
52 ms |
6276 KB |
Output is correct |
89 |
Correct |
205 ms |
12944 KB |
Output is correct |
90 |
Correct |
201 ms |
13000 KB |
Output is correct |
91 |
Correct |
258 ms |
12912 KB |
Output is correct |
92 |
Correct |
94 ms |
22136 KB |
Output is correct |
93 |
Correct |
100 ms |
22180 KB |
Output is correct |
94 |
Correct |
97 ms |
22184 KB |
Output is correct |
95 |
Correct |
101 ms |
17224 KB |
Output is correct |
96 |
Correct |
117 ms |
17632 KB |
Output is correct |
97 |
Correct |
107 ms |
17652 KB |
Output is correct |
98 |
Correct |
99 ms |
17576 KB |
Output is correct |
99 |
Correct |
113 ms |
16924 KB |
Output is correct |
100 |
Correct |
380 ms |
12992 KB |
Output is correct |
101 |
Correct |
372 ms |
13380 KB |
Output is correct |
102 |
Correct |
399 ms |
13420 KB |
Output is correct |
103 |
Correct |
353 ms |
13380 KB |
Output is correct |
104 |
Correct |
124 ms |
16792 KB |
Output is correct |
105 |
Correct |
130 ms |
16832 KB |
Output is correct |
106 |
Correct |
111 ms |
16872 KB |
Output is correct |
107 |
Correct |
207 ms |
11688 KB |
Output is correct |
108 |
Correct |
227 ms |
12012 KB |
Output is correct |
109 |
Correct |
197 ms |
12312 KB |
Output is correct |
110 |
Correct |
93 ms |
21192 KB |
Output is correct |
111 |
Correct |
119 ms |
17216 KB |
Output is correct |
112 |
Correct |
83 ms |
16652 KB |
Output is correct |
113 |
Correct |
87 ms |
15860 KB |
Output is correct |
114 |
Correct |
424 ms |
13068 KB |
Output is correct |
115 |
Correct |
357 ms |
12412 KB |
Output is correct |
116 |
Correct |
134 ms |
15776 KB |
Output is correct |
117 |
Correct |
98 ms |
14468 KB |
Output is correct |
118 |
Correct |
112 ms |
13932 KB |
Output is correct |
119 |
Correct |
104 ms |
13572 KB |
Output is correct |
120 |
Correct |
95 ms |
13772 KB |
Output is correct |
121 |
Correct |
95 ms |
13180 KB |
Output is correct |
122 |
Correct |
98 ms |
12880 KB |
Output is correct |
123 |
Correct |
118 ms |
14596 KB |
Output is correct |
124 |
Correct |
124 ms |
14092 KB |
Output is correct |
125 |
Correct |
133 ms |
13636 KB |
Output is correct |
126 |
Correct |
117 ms |
13888 KB |
Output is correct |
127 |
Correct |
105 ms |
13260 KB |
Output is correct |
128 |
Correct |
124 ms |
13020 KB |
Output is correct |