#include<bits/stdc++.h>
using namespace std;
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 1e5+7, LG = 20;
int n, a[N], par[N];
ii d[N];
vector <int> tree[N];
int to[N][LG], h[N];
int l[N], r[N], ptr = -1;
void dfs(int u) {
to[u][0] = par[u];
for (int i = 1; i < LG; ++i)
to[u][i] = to[to[u][i - 1]][i - 1];
l[u] = ++ptr;
for (int v : tree[u]) {
h[v] = h[u]+1;
dfs(v);
}
r[u] = ptr;
}
int mx[N << 2];
void upd(int v, int tl, int tr, int i, int x) {
mx[v] = x;
if (tl == tr)
return;
int tm = (tl + tr) >> 1;
if (i <= tm)
upd(v * 2 + 1, tl, tm, i, x);
else
upd(v * 2 + 2, tm + 1, tr, i, x);
}
int get(int v, int tl, int tr, int l, int r) {
if (tr < l || r < tl)
return -1;
if (l <= tl && tr <= r)
return mx[v];
int tm = (tl + tr) >> 1;
return max(get(v * 2 + 1, tl, tm, l, r), get(v * 2 + 2, tm + 1, tr, l, r));
}
int val[N];
int prv[N];
int last(int u) {
return prv[u] = get(0, 0, N, l[u], r[u]);
}
int getval(int u, int ls) {
if (ls == -1)
return a[u];
else
return val[ls];
}
struct Fen {
int f[N];
void add(int i, int x) {
for (; i < N; i |= i + 1)
f[i] += x;
}
void clear(int i) {
for (; i < N; i |= i + 1)
f[i] = 0;
}
int get(int i) {
int ans = 0;
for (; i >= 0; i &= i + 1, --i) ans += f[i];
return ans;
}
int get(int l, int r) {
return get(r) - get(l - 1);
}
} f;
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
for (int i = 0; i < (N << 2); ++i)
mx[i] = -1;
cin >> n;
vector <int> c;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
c.app(a[i]);
}
sort(all(c));
c.resize(unique(all(c)) - c.begin());
for (int i = 1; i <= n; ++i)
a[i] = lower_bound(all(c), a[i]) - c.begin();
for (int t = 0; t < n - 1; ++t) {
int u, v;
cin >> u >> v;
tree[u].app(v);
par[v] = u;
d[t] = mp(u,v);
}
par[1] = 1;
dfs(1);
int sum = 0;
for (int i = 0; i < N; ++i)
prv[i] = -1;
for (int t = 0; t < n - 1; ++t) {
int u, v;
tie(u, v) = d[t];
vector <ii> path;
while (1) {
int up = u;
int w = last(u);
for (int i = LG - 1; i >= 0; --i)
if (prv[to[up][i]] <= w && last(to[up][i]) == w)
up = to[up][i];
path.app(mp(getval(u, w), h[u] - h[up] + 1));
if (up == 1)
break;
else
u = par[up];
}
val[t] = a[v];
upd(0, 0, N, l[v], t);
sum += path.size();
if (sum > 20 * n)
exit(1);
ll ans = 0;
for (auto e : path) {
ans += f.get(e.f - 1) * e.s;
f.add(e.f, e.s);
}
cout << ans << endl;
for (auto e : path)
f.clear(e.f);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4736 KB |
Output is correct |
2 |
Correct |
7 ms |
4736 KB |
Output is correct |
3 |
Correct |
8 ms |
4736 KB |
Output is correct |
4 |
Correct |
9 ms |
4736 KB |
Output is correct |
5 |
Correct |
9 ms |
4736 KB |
Output is correct |
6 |
Correct |
9 ms |
4736 KB |
Output is correct |
7 |
Correct |
9 ms |
4864 KB |
Output is correct |
8 |
Correct |
9 ms |
4736 KB |
Output is correct |
9 |
Correct |
9 ms |
4864 KB |
Output is correct |
10 |
Correct |
9 ms |
4736 KB |
Output is correct |
11 |
Correct |
9 ms |
4916 KB |
Output is correct |
12 |
Correct |
10 ms |
4736 KB |
Output is correct |
13 |
Correct |
9 ms |
4736 KB |
Output is correct |
14 |
Correct |
9 ms |
4864 KB |
Output is correct |
15 |
Correct |
10 ms |
4864 KB |
Output is correct |
16 |
Correct |
11 ms |
4736 KB |
Output is correct |
17 |
Correct |
10 ms |
4736 KB |
Output is correct |
18 |
Correct |
11 ms |
4736 KB |
Output is correct |
19 |
Correct |
9 ms |
4736 KB |
Output is correct |
20 |
Correct |
11 ms |
4736 KB |
Output is correct |
21 |
Correct |
10 ms |
4864 KB |
Output is correct |
22 |
Correct |
9 ms |
4736 KB |
Output is correct |
23 |
Correct |
10 ms |
4736 KB |
Output is correct |
24 |
Correct |
9 ms |
4736 KB |
Output is correct |
25 |
Correct |
9 ms |
4864 KB |
Output is correct |
26 |
Correct |
9 ms |
4736 KB |
Output is correct |
27 |
Correct |
9 ms |
4736 KB |
Output is correct |
28 |
Correct |
9 ms |
4736 KB |
Output is correct |
29 |
Correct |
9 ms |
4736 KB |
Output is correct |
30 |
Correct |
10 ms |
4736 KB |
Output is correct |
31 |
Correct |
10 ms |
4864 KB |
Output is correct |
32 |
Correct |
9 ms |
4736 KB |
Output is correct |
33 |
Correct |
9 ms |
4736 KB |
Output is correct |
34 |
Correct |
10 ms |
4736 KB |
Output is correct |
35 |
Correct |
9 ms |
4864 KB |
Output is correct |
36 |
Correct |
9 ms |
4736 KB |
Output is correct |
37 |
Correct |
9 ms |
4736 KB |
Output is correct |
38 |
Correct |
9 ms |
4736 KB |
Output is correct |
39 |
Correct |
9 ms |
4736 KB |
Output is correct |
40 |
Correct |
9 ms |
4736 KB |
Output is correct |
41 |
Correct |
9 ms |
4736 KB |
Output is correct |
42 |
Correct |
10 ms |
4736 KB |
Output is correct |
43 |
Correct |
10 ms |
4864 KB |
Output is correct |
44 |
Correct |
9 ms |
4736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4736 KB |
Output is correct |
2 |
Correct |
7 ms |
4736 KB |
Output is correct |
3 |
Correct |
8 ms |
4736 KB |
Output is correct |
4 |
Correct |
9 ms |
4736 KB |
Output is correct |
5 |
Correct |
9 ms |
4736 KB |
Output is correct |
6 |
Correct |
9 ms |
4736 KB |
Output is correct |
7 |
Correct |
9 ms |
4864 KB |
Output is correct |
8 |
Correct |
9 ms |
4736 KB |
Output is correct |
9 |
Correct |
9 ms |
4864 KB |
Output is correct |
10 |
Correct |
9 ms |
4736 KB |
Output is correct |
11 |
Correct |
9 ms |
4916 KB |
Output is correct |
12 |
Correct |
10 ms |
4736 KB |
Output is correct |
13 |
Correct |
9 ms |
4736 KB |
Output is correct |
14 |
Correct |
9 ms |
4864 KB |
Output is correct |
15 |
Correct |
10 ms |
4864 KB |
Output is correct |
16 |
Correct |
11 ms |
4736 KB |
Output is correct |
17 |
Correct |
10 ms |
4736 KB |
Output is correct |
18 |
Correct |
11 ms |
4736 KB |
Output is correct |
19 |
Correct |
9 ms |
4736 KB |
Output is correct |
20 |
Correct |
11 ms |
4736 KB |
Output is correct |
21 |
Correct |
10 ms |
4864 KB |
Output is correct |
22 |
Correct |
9 ms |
4736 KB |
Output is correct |
23 |
Correct |
10 ms |
4736 KB |
Output is correct |
24 |
Correct |
9 ms |
4736 KB |
Output is correct |
25 |
Correct |
9 ms |
4864 KB |
Output is correct |
26 |
Correct |
9 ms |
4736 KB |
Output is correct |
27 |
Correct |
9 ms |
4736 KB |
Output is correct |
28 |
Correct |
9 ms |
4736 KB |
Output is correct |
29 |
Correct |
9 ms |
4736 KB |
Output is correct |
30 |
Correct |
10 ms |
4736 KB |
Output is correct |
31 |
Correct |
10 ms |
4864 KB |
Output is correct |
32 |
Correct |
9 ms |
4736 KB |
Output is correct |
33 |
Correct |
9 ms |
4736 KB |
Output is correct |
34 |
Correct |
10 ms |
4736 KB |
Output is correct |
35 |
Correct |
9 ms |
4864 KB |
Output is correct |
36 |
Correct |
9 ms |
4736 KB |
Output is correct |
37 |
Correct |
9 ms |
4736 KB |
Output is correct |
38 |
Correct |
9 ms |
4736 KB |
Output is correct |
39 |
Correct |
9 ms |
4736 KB |
Output is correct |
40 |
Correct |
9 ms |
4736 KB |
Output is correct |
41 |
Correct |
9 ms |
4736 KB |
Output is correct |
42 |
Correct |
10 ms |
4736 KB |
Output is correct |
43 |
Correct |
10 ms |
4864 KB |
Output is correct |
44 |
Correct |
9 ms |
4736 KB |
Output is correct |
45 |
Correct |
12 ms |
4864 KB |
Output is correct |
46 |
Correct |
28 ms |
5248 KB |
Output is correct |
47 |
Correct |
27 ms |
5248 KB |
Output is correct |
48 |
Correct |
25 ms |
5248 KB |
Output is correct |
49 |
Correct |
22 ms |
5376 KB |
Output is correct |
50 |
Correct |
20 ms |
5376 KB |
Output is correct |
51 |
Correct |
20 ms |
5496 KB |
Output is correct |
52 |
Correct |
20 ms |
5376 KB |
Output is correct |
53 |
Correct |
20 ms |
5376 KB |
Output is correct |
54 |
Correct |
20 ms |
5376 KB |
Output is correct |
55 |
Correct |
21 ms |
5376 KB |
Output is correct |
56 |
Correct |
21 ms |
5248 KB |
Output is correct |
57 |
Correct |
38 ms |
5248 KB |
Output is correct |
58 |
Correct |
38 ms |
5248 KB |
Output is correct |
59 |
Correct |
36 ms |
5248 KB |
Output is correct |
60 |
Correct |
37 ms |
5248 KB |
Output is correct |
61 |
Correct |
25 ms |
5248 KB |
Output is correct |
62 |
Correct |
25 ms |
5248 KB |
Output is correct |
63 |
Correct |
25 ms |
5376 KB |
Output is correct |
64 |
Correct |
27 ms |
5248 KB |
Output is correct |
65 |
Correct |
26 ms |
5248 KB |
Output is correct |
66 |
Correct |
26 ms |
5248 KB |
Output is correct |
67 |
Correct |
25 ms |
5248 KB |
Output is correct |
68 |
Correct |
19 ms |
5376 KB |
Output is correct |
69 |
Correct |
20 ms |
5376 KB |
Output is correct |
70 |
Correct |
20 ms |
5376 KB |
Output is correct |
71 |
Correct |
20 ms |
5248 KB |
Output is correct |
72 |
Correct |
37 ms |
5248 KB |
Output is correct |
73 |
Correct |
39 ms |
5248 KB |
Output is correct |
74 |
Correct |
24 ms |
5248 KB |
Output is correct |
75 |
Correct |
19 ms |
5248 KB |
Output is correct |
76 |
Correct |
22 ms |
5248 KB |
Output is correct |
77 |
Correct |
20 ms |
5248 KB |
Output is correct |
78 |
Correct |
20 ms |
5248 KB |
Output is correct |
79 |
Correct |
19 ms |
5248 KB |
Output is correct |
80 |
Correct |
20 ms |
5248 KB |
Output is correct |
81 |
Correct |
26 ms |
5248 KB |
Output is correct |
82 |
Correct |
25 ms |
5248 KB |
Output is correct |
83 |
Correct |
27 ms |
5248 KB |
Output is correct |
84 |
Correct |
25 ms |
5248 KB |
Output is correct |
85 |
Correct |
25 ms |
5248 KB |
Output is correct |
86 |
Correct |
27 ms |
5248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
4736 KB |
Output is correct |
2 |
Correct |
7 ms |
4736 KB |
Output is correct |
3 |
Correct |
8 ms |
4736 KB |
Output is correct |
4 |
Correct |
9 ms |
4736 KB |
Output is correct |
5 |
Correct |
9 ms |
4736 KB |
Output is correct |
6 |
Correct |
9 ms |
4736 KB |
Output is correct |
7 |
Correct |
9 ms |
4864 KB |
Output is correct |
8 |
Correct |
9 ms |
4736 KB |
Output is correct |
9 |
Correct |
9 ms |
4864 KB |
Output is correct |
10 |
Correct |
9 ms |
4736 KB |
Output is correct |
11 |
Correct |
9 ms |
4916 KB |
Output is correct |
12 |
Correct |
10 ms |
4736 KB |
Output is correct |
13 |
Correct |
9 ms |
4736 KB |
Output is correct |
14 |
Correct |
9 ms |
4864 KB |
Output is correct |
15 |
Correct |
10 ms |
4864 KB |
Output is correct |
16 |
Correct |
11 ms |
4736 KB |
Output is correct |
17 |
Correct |
10 ms |
4736 KB |
Output is correct |
18 |
Correct |
11 ms |
4736 KB |
Output is correct |
19 |
Correct |
9 ms |
4736 KB |
Output is correct |
20 |
Correct |
11 ms |
4736 KB |
Output is correct |
21 |
Correct |
10 ms |
4864 KB |
Output is correct |
22 |
Correct |
9 ms |
4736 KB |
Output is correct |
23 |
Correct |
10 ms |
4736 KB |
Output is correct |
24 |
Correct |
9 ms |
4736 KB |
Output is correct |
25 |
Correct |
9 ms |
4864 KB |
Output is correct |
26 |
Correct |
9 ms |
4736 KB |
Output is correct |
27 |
Correct |
9 ms |
4736 KB |
Output is correct |
28 |
Correct |
9 ms |
4736 KB |
Output is correct |
29 |
Correct |
9 ms |
4736 KB |
Output is correct |
30 |
Correct |
10 ms |
4736 KB |
Output is correct |
31 |
Correct |
10 ms |
4864 KB |
Output is correct |
32 |
Correct |
9 ms |
4736 KB |
Output is correct |
33 |
Correct |
9 ms |
4736 KB |
Output is correct |
34 |
Correct |
10 ms |
4736 KB |
Output is correct |
35 |
Correct |
9 ms |
4864 KB |
Output is correct |
36 |
Correct |
9 ms |
4736 KB |
Output is correct |
37 |
Correct |
9 ms |
4736 KB |
Output is correct |
38 |
Correct |
9 ms |
4736 KB |
Output is correct |
39 |
Correct |
9 ms |
4736 KB |
Output is correct |
40 |
Correct |
9 ms |
4736 KB |
Output is correct |
41 |
Correct |
9 ms |
4736 KB |
Output is correct |
42 |
Correct |
10 ms |
4736 KB |
Output is correct |
43 |
Correct |
10 ms |
4864 KB |
Output is correct |
44 |
Correct |
9 ms |
4736 KB |
Output is correct |
45 |
Correct |
12 ms |
4864 KB |
Output is correct |
46 |
Correct |
28 ms |
5248 KB |
Output is correct |
47 |
Correct |
27 ms |
5248 KB |
Output is correct |
48 |
Correct |
25 ms |
5248 KB |
Output is correct |
49 |
Correct |
22 ms |
5376 KB |
Output is correct |
50 |
Correct |
20 ms |
5376 KB |
Output is correct |
51 |
Correct |
20 ms |
5496 KB |
Output is correct |
52 |
Correct |
20 ms |
5376 KB |
Output is correct |
53 |
Correct |
20 ms |
5376 KB |
Output is correct |
54 |
Correct |
20 ms |
5376 KB |
Output is correct |
55 |
Correct |
21 ms |
5376 KB |
Output is correct |
56 |
Correct |
21 ms |
5248 KB |
Output is correct |
57 |
Correct |
38 ms |
5248 KB |
Output is correct |
58 |
Correct |
38 ms |
5248 KB |
Output is correct |
59 |
Correct |
36 ms |
5248 KB |
Output is correct |
60 |
Correct |
37 ms |
5248 KB |
Output is correct |
61 |
Correct |
25 ms |
5248 KB |
Output is correct |
62 |
Correct |
25 ms |
5248 KB |
Output is correct |
63 |
Correct |
25 ms |
5376 KB |
Output is correct |
64 |
Correct |
27 ms |
5248 KB |
Output is correct |
65 |
Correct |
26 ms |
5248 KB |
Output is correct |
66 |
Correct |
26 ms |
5248 KB |
Output is correct |
67 |
Correct |
25 ms |
5248 KB |
Output is correct |
68 |
Correct |
19 ms |
5376 KB |
Output is correct |
69 |
Correct |
20 ms |
5376 KB |
Output is correct |
70 |
Correct |
20 ms |
5376 KB |
Output is correct |
71 |
Correct |
20 ms |
5248 KB |
Output is correct |
72 |
Correct |
37 ms |
5248 KB |
Output is correct |
73 |
Correct |
39 ms |
5248 KB |
Output is correct |
74 |
Correct |
24 ms |
5248 KB |
Output is correct |
75 |
Correct |
19 ms |
5248 KB |
Output is correct |
76 |
Correct |
22 ms |
5248 KB |
Output is correct |
77 |
Correct |
20 ms |
5248 KB |
Output is correct |
78 |
Correct |
20 ms |
5248 KB |
Output is correct |
79 |
Correct |
19 ms |
5248 KB |
Output is correct |
80 |
Correct |
20 ms |
5248 KB |
Output is correct |
81 |
Correct |
26 ms |
5248 KB |
Output is correct |
82 |
Correct |
25 ms |
5248 KB |
Output is correct |
83 |
Correct |
27 ms |
5248 KB |
Output is correct |
84 |
Correct |
25 ms |
5248 KB |
Output is correct |
85 |
Correct |
25 ms |
5248 KB |
Output is correct |
86 |
Correct |
27 ms |
5248 KB |
Output is correct |
87 |
Correct |
62 ms |
6016 KB |
Output is correct |
88 |
Correct |
190 ms |
8824 KB |
Output is correct |
89 |
Correct |
905 ms |
18548 KB |
Output is correct |
90 |
Correct |
816 ms |
18480 KB |
Output is correct |
91 |
Correct |
791 ms |
18312 KB |
Output is correct |
92 |
Correct |
413 ms |
22892 KB |
Output is correct |
93 |
Correct |
415 ms |
22896 KB |
Output is correct |
94 |
Correct |
449 ms |
22900 KB |
Output is correct |
95 |
Correct |
531 ms |
20724 KB |
Output is correct |
96 |
Correct |
541 ms |
20724 KB |
Output is correct |
97 |
Correct |
502 ms |
20724 KB |
Output is correct |
98 |
Correct |
500 ms |
20432 KB |
Output is correct |
99 |
Correct |
520 ms |
19964 KB |
Output is correct |
100 |
Correct |
1292 ms |
18928 KB |
Output is correct |
101 |
Correct |
1266 ms |
20980 KB |
Output is correct |
102 |
Correct |
1335 ms |
21108 KB |
Output is correct |
103 |
Correct |
1333 ms |
20932 KB |
Output is correct |
104 |
Correct |
815 ms |
22004 KB |
Output is correct |
105 |
Correct |
869 ms |
22004 KB |
Output is correct |
106 |
Correct |
875 ms |
22036 KB |
Output is correct |
107 |
Correct |
809 ms |
19312 KB |
Output is correct |
108 |
Correct |
854 ms |
19444 KB |
Output is correct |
109 |
Correct |
816 ms |
19928 KB |
Output is correct |
110 |
Correct |
420 ms |
23972 KB |
Output is correct |
111 |
Correct |
487 ms |
22260 KB |
Output is correct |
112 |
Correct |
426 ms |
21748 KB |
Output is correct |
113 |
Correct |
427 ms |
21108 KB |
Output is correct |
114 |
Correct |
1194 ms |
20596 KB |
Output is correct |
115 |
Correct |
1271 ms |
20080 KB |
Output is correct |
116 |
Correct |
853 ms |
21108 KB |
Output is correct |
117 |
Correct |
451 ms |
20928 KB |
Output is correct |
118 |
Correct |
446 ms |
20724 KB |
Output is correct |
119 |
Correct |
464 ms |
20468 KB |
Output is correct |
120 |
Correct |
426 ms |
20084 KB |
Output is correct |
121 |
Correct |
463 ms |
19956 KB |
Output is correct |
122 |
Correct |
482 ms |
19880 KB |
Output is correct |
123 |
Correct |
892 ms |
20980 KB |
Output is correct |
124 |
Correct |
794 ms |
20652 KB |
Output is correct |
125 |
Correct |
798 ms |
20464 KB |
Output is correct |
126 |
Correct |
828 ms |
20420 KB |
Output is correct |
127 |
Correct |
846 ms |
20084 KB |
Output is correct |
128 |
Correct |
784 ms |
19952 KB |
Output is correct |