/*
-------------- | /
| | /
| | /
| * |/ | | ------ *
| | | | / \
| | |\ | | | |\ |
\ | | | \ | | | | \ |
\ | | | \ | | \ / \ |
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]);
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 |
1 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 |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 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 |
3540 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 |
3412 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
2 ms |
3540 KB |
Output is correct |
20 |
Correct |
2 ms |
3540 KB |
Output is correct |
21 |
Correct |
2 ms |
3540 KB |
Output is correct |
22 |
Correct |
3 ms |
3412 KB |
Output is correct |
23 |
Correct |
2 ms |
3412 KB |
Output is correct |
24 |
Correct |
2 ms |
3412 KB |
Output is correct |
25 |
Correct |
2 ms |
3412 KB |
Output is correct |
26 |
Correct |
2 ms |
3540 KB |
Output is correct |
27 |
Correct |
2 ms |
3540 KB |
Output is correct |
28 |
Correct |
2 ms |
3540 KB |
Output is correct |
29 |
Correct |
2 ms |
3540 KB |
Output is correct |
30 |
Correct |
3 ms |
3412 KB |
Output is correct |
31 |
Correct |
2 ms |
3412 KB |
Output is correct |
32 |
Correct |
2 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 |
3412 KB |
Output is correct |
36 |
Correct |
2 ms |
3412 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 |
3412 KB |
Output is correct |
40 |
Correct |
3 ms |
3412 KB |
Output is correct |
41 |
Correct |
2 ms |
3412 KB |
Output is correct |
42 |
Correct |
2 ms |
3412 KB |
Output is correct |
43 |
Correct |
3 ms |
3412 KB |
Output is correct |
44 |
Correct |
2 ms |
3412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 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 |
3540 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 |
3412 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
2 ms |
3540 KB |
Output is correct |
20 |
Correct |
2 ms |
3540 KB |
Output is correct |
21 |
Correct |
2 ms |
3540 KB |
Output is correct |
22 |
Correct |
3 ms |
3412 KB |
Output is correct |
23 |
Correct |
2 ms |
3412 KB |
Output is correct |
24 |
Correct |
2 ms |
3412 KB |
Output is correct |
25 |
Correct |
2 ms |
3412 KB |
Output is correct |
26 |
Correct |
2 ms |
3540 KB |
Output is correct |
27 |
Correct |
2 ms |
3540 KB |
Output is correct |
28 |
Correct |
2 ms |
3540 KB |
Output is correct |
29 |
Correct |
2 ms |
3540 KB |
Output is correct |
30 |
Correct |
3 ms |
3412 KB |
Output is correct |
31 |
Correct |
2 ms |
3412 KB |
Output is correct |
32 |
Correct |
2 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 |
3412 KB |
Output is correct |
36 |
Correct |
2 ms |
3412 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 |
3412 KB |
Output is correct |
40 |
Correct |
3 ms |
3412 KB |
Output is correct |
41 |
Correct |
2 ms |
3412 KB |
Output is correct |
42 |
Correct |
2 ms |
3412 KB |
Output is correct |
43 |
Correct |
3 ms |
3412 KB |
Output is correct |
44 |
Correct |
2 ms |
3412 KB |
Output is correct |
45 |
Correct |
3 ms |
3540 KB |
Output is correct |
46 |
Correct |
7 ms |
3768 KB |
Output is correct |
47 |
Correct |
7 ms |
3668 KB |
Output is correct |
48 |
Correct |
7 ms |
3668 KB |
Output is correct |
49 |
Correct |
5 ms |
4052 KB |
Output is correct |
50 |
Correct |
4 ms |
4052 KB |
Output is correct |
51 |
Correct |
5 ms |
4052 KB |
Output is correct |
52 |
Correct |
5 ms |
3924 KB |
Output is correct |
53 |
Correct |
5 ms |
3912 KB |
Output is correct |
54 |
Correct |
6 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 |
9 ms |
3752 KB |
Output is correct |
58 |
Correct |
9 ms |
3668 KB |
Output is correct |
59 |
Correct |
10 ms |
3668 KB |
Output is correct |
60 |
Correct |
10 ms |
3764 KB |
Output is correct |
61 |
Correct |
5 ms |
3924 KB |
Output is correct |
62 |
Correct |
5 ms |
3924 KB |
Output is correct |
63 |
Correct |
5 ms |
3924 KB |
Output is correct |
64 |
Correct |
7 ms |
3668 KB |
Output is correct |
65 |
Correct |
6 ms |
3668 KB |
Output is correct |
66 |
Correct |
6 ms |
3668 KB |
Output is correct |
67 |
Correct |
6 ms |
3668 KB |
Output is correct |
68 |
Correct |
4 ms |
4116 KB |
Output is correct |
69 |
Correct |
5 ms |
3924 KB |
Output is correct |
70 |
Correct |
4 ms |
3924 KB |
Output is correct |
71 |
Correct |
4 ms |
3924 KB |
Output is correct |
72 |
Correct |
9 ms |
3668 KB |
Output is correct |
73 |
Correct |
9 ms |
3668 KB |
Output is correct |
74 |
Correct |
4 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 |
3796 KB |
Output is correct |
78 |
Correct |
4 ms |
3796 KB |
Output is correct |
79 |
Correct |
4 ms |
3796 KB |
Output is correct |
80 |
Correct |
4 ms |
3796 KB |
Output is correct |
81 |
Correct |
5 ms |
3796 KB |
Output is correct |
82 |
Correct |
5 ms |
3796 KB |
Output is correct |
83 |
Correct |
7 ms |
3796 KB |
Output is correct |
84 |
Correct |
5 ms |
3776 KB |
Output is correct |
85 |
Correct |
5 ms |
3796 KB |
Output is correct |
86 |
Correct |
5 ms |
3796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
2 ms |
3412 KB |
Output is correct |
7 |
Correct |
2 ms |
3412 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 |
3540 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 |
3412 KB |
Output is correct |
17 |
Correct |
2 ms |
3412 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
2 ms |
3540 KB |
Output is correct |
20 |
Correct |
2 ms |
3540 KB |
Output is correct |
21 |
Correct |
2 ms |
3540 KB |
Output is correct |
22 |
Correct |
3 ms |
3412 KB |
Output is correct |
23 |
Correct |
2 ms |
3412 KB |
Output is correct |
24 |
Correct |
2 ms |
3412 KB |
Output is correct |
25 |
Correct |
2 ms |
3412 KB |
Output is correct |
26 |
Correct |
2 ms |
3540 KB |
Output is correct |
27 |
Correct |
2 ms |
3540 KB |
Output is correct |
28 |
Correct |
2 ms |
3540 KB |
Output is correct |
29 |
Correct |
2 ms |
3540 KB |
Output is correct |
30 |
Correct |
3 ms |
3412 KB |
Output is correct |
31 |
Correct |
2 ms |
3412 KB |
Output is correct |
32 |
Correct |
2 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 |
3412 KB |
Output is correct |
36 |
Correct |
2 ms |
3412 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 |
3412 KB |
Output is correct |
40 |
Correct |
3 ms |
3412 KB |
Output is correct |
41 |
Correct |
2 ms |
3412 KB |
Output is correct |
42 |
Correct |
2 ms |
3412 KB |
Output is correct |
43 |
Correct |
3 ms |
3412 KB |
Output is correct |
44 |
Correct |
2 ms |
3412 KB |
Output is correct |
45 |
Correct |
3 ms |
3540 KB |
Output is correct |
46 |
Correct |
7 ms |
3768 KB |
Output is correct |
47 |
Correct |
7 ms |
3668 KB |
Output is correct |
48 |
Correct |
7 ms |
3668 KB |
Output is correct |
49 |
Correct |
5 ms |
4052 KB |
Output is correct |
50 |
Correct |
4 ms |
4052 KB |
Output is correct |
51 |
Correct |
5 ms |
4052 KB |
Output is correct |
52 |
Correct |
5 ms |
3924 KB |
Output is correct |
53 |
Correct |
5 ms |
3912 KB |
Output is correct |
54 |
Correct |
6 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 |
9 ms |
3752 KB |
Output is correct |
58 |
Correct |
9 ms |
3668 KB |
Output is correct |
59 |
Correct |
10 ms |
3668 KB |
Output is correct |
60 |
Correct |
10 ms |
3764 KB |
Output is correct |
61 |
Correct |
5 ms |
3924 KB |
Output is correct |
62 |
Correct |
5 ms |
3924 KB |
Output is correct |
63 |
Correct |
5 ms |
3924 KB |
Output is correct |
64 |
Correct |
7 ms |
3668 KB |
Output is correct |
65 |
Correct |
6 ms |
3668 KB |
Output is correct |
66 |
Correct |
6 ms |
3668 KB |
Output is correct |
67 |
Correct |
6 ms |
3668 KB |
Output is correct |
68 |
Correct |
4 ms |
4116 KB |
Output is correct |
69 |
Correct |
5 ms |
3924 KB |
Output is correct |
70 |
Correct |
4 ms |
3924 KB |
Output is correct |
71 |
Correct |
4 ms |
3924 KB |
Output is correct |
72 |
Correct |
9 ms |
3668 KB |
Output is correct |
73 |
Correct |
9 ms |
3668 KB |
Output is correct |
74 |
Correct |
4 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 |
3796 KB |
Output is correct |
78 |
Correct |
4 ms |
3796 KB |
Output is correct |
79 |
Correct |
4 ms |
3796 KB |
Output is correct |
80 |
Correct |
4 ms |
3796 KB |
Output is correct |
81 |
Correct |
5 ms |
3796 KB |
Output is correct |
82 |
Correct |
5 ms |
3796 KB |
Output is correct |
83 |
Correct |
7 ms |
3796 KB |
Output is correct |
84 |
Correct |
5 ms |
3776 KB |
Output is correct |
85 |
Correct |
5 ms |
3796 KB |
Output is correct |
86 |
Correct |
5 ms |
3796 KB |
Output is correct |
87 |
Correct |
15 ms |
4228 KB |
Output is correct |
88 |
Correct |
50 ms |
5680 KB |
Output is correct |
89 |
Correct |
189 ms |
10780 KB |
Output is correct |
90 |
Correct |
208 ms |
10820 KB |
Output is correct |
91 |
Correct |
189 ms |
10840 KB |
Output is correct |
92 |
Correct |
89 ms |
20084 KB |
Output is correct |
93 |
Correct |
97 ms |
19996 KB |
Output is correct |
94 |
Correct |
94 ms |
20100 KB |
Output is correct |
95 |
Correct |
96 ms |
15464 KB |
Output is correct |
96 |
Correct |
105 ms |
15500 KB |
Output is correct |
97 |
Correct |
107 ms |
15424 KB |
Output is correct |
98 |
Correct |
96 ms |
15420 KB |
Output is correct |
99 |
Correct |
103 ms |
14716 KB |
Output is correct |
100 |
Correct |
306 ms |
11336 KB |
Output is correct |
101 |
Correct |
355 ms |
11204 KB |
Output is correct |
102 |
Correct |
332 ms |
11244 KB |
Output is correct |
103 |
Correct |
360 ms |
11180 KB |
Output is correct |
104 |
Correct |
113 ms |
14740 KB |
Output is correct |
105 |
Correct |
109 ms |
14660 KB |
Output is correct |
106 |
Correct |
113 ms |
14780 KB |
Output is correct |
107 |
Correct |
168 ms |
10400 KB |
Output is correct |
108 |
Correct |
192 ms |
10432 KB |
Output is correct |
109 |
Correct |
192 ms |
10496 KB |
Output is correct |
110 |
Correct |
77 ms |
19648 KB |
Output is correct |
111 |
Correct |
92 ms |
15516 KB |
Output is correct |
112 |
Correct |
84 ms |
15080 KB |
Output is correct |
113 |
Correct |
83 ms |
14340 KB |
Output is correct |
114 |
Correct |
315 ms |
11352 KB |
Output is correct |
115 |
Correct |
330 ms |
10904 KB |
Output is correct |
116 |
Correct |
96 ms |
14372 KB |
Output is correct |
117 |
Correct |
95 ms |
12720 KB |
Output is correct |
118 |
Correct |
94 ms |
12020 KB |
Output is correct |
119 |
Correct |
97 ms |
11668 KB |
Output is correct |
120 |
Correct |
85 ms |
12280 KB |
Output is correct |
121 |
Correct |
82 ms |
11644 KB |
Output is correct |
122 |
Correct |
79 ms |
11400 KB |
Output is correct |
123 |
Correct |
126 ms |
12788 KB |
Output is correct |
124 |
Correct |
137 ms |
12120 KB |
Output is correct |
125 |
Correct |
111 ms |
11752 KB |
Output is correct |
126 |
Correct |
98 ms |
12320 KB |
Output is correct |
127 |
Correct |
100 ms |
11696 KB |
Output is correct |
128 |
Correct |
118 ms |
11492 KB |
Output is correct |