#include "bits/stdc++.h"
using namespace std;
#ifdef Nero
#include "Deb.h"
#else
#define deb(...)
#endif
const int N = 1e5 + 5;
int a[N];
int tree[N];
vector<int> g[N];
int pr[N], sz[N], dep[N];
int cnt, id[N], top[N], chain[N];
deque<pair<int, int>> in_chain[N];
inline void upd (int idd, int v) {
while (idd < N) {
tree[idd] += v;
idd += idd & -idd;
}
}
inline long long qry (int idd) {
long long ret = 0;
while (idd) {
ret += tree[idd];
idd -= idd & -idd;
}
return ret;
}
void dfs(int v, int p) {
sz[v] = 1;
for (int u : g[v]) {
if (u == p) {
continue;
}
pr[u] = v;
dep[u] = dep[v] + 1;
dfs(u, v);
sz[v] += sz[u];
}
}
void dfs_hld(int v, int p, int tp) {
top[v] = tp;
deque<pair<int, int>>& deq = in_chain[chain[v]];
if (deq.empty() || deq.back().first != a[v]) {
deq.emplace_back(a[v], 1);
} else {
deq.back().second++;
}
int big = 0;
for (int u : g[v]) {
if (u != p && sz[u] > sz[big]) {
big = u;
}
}
if (big) {
chain[big] = chain[v];
id[big] = id[v] + 1;
dfs_hld(big, v, tp);
}
for (int u : g[v]) {
if (u != p && u != big) {
id[u] = 1;
chain[u] = cnt++;
dfs_hld(u, v, u);
}
}
}
vector<vector<pair<int, int>>> get(int v, int c) {
vector<vector<pair<int, int>>> ret;
while (v) {
vector<pair<int, int>> tv;
int to_delete = id[v];
int deleted = 0;
deque<pair<int, int>>& deq = in_chain[chain[v]];
while (!deq.empty() && deleted < to_delete) {
if (deq.front().second + deleted > to_delete) {
tv.push_back({deq.front().first, to_delete - deleted});
deq.front().second -= (to_delete - deleted);
deleted = to_delete;
break;
} else {
tv.push_back(deq.front());
deleted += deq.front().second;
deq.pop_front();
}
}
assert(to_delete == deleted);
deq.push_front({c, to_delete});
ret.push_back(tv);
v = pr[top[v]];
}
return ret;
}
long long solve(vector<vector<pair<int, int>>> vec) {
reverse(vec.begin(), vec.end());
long long ret = 0;
for (int i = 0; i < (int) vec.size(); ++i) {
for (auto [c, freq] : vec[i]) {
ret += (qry(N - 1) - qry(c)) * freq;
upd(c, freq);
}
}
for (int i = 0; i < (int) vec.size(); ++i) {
for (auto [c, freq] : vec[i]) {
upd(c, -freq);
}
}
return ret;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
map<int, int> mp;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
mp[a[i]];
}
int cc = 0;
for (auto& it : mp) {
it.second = ++cc;
}
for (int i = 1; i <= n; ++i) {
a[i] = mp[a[i]];
}
vector<int> qu(n - 1), qv(n - 1);
for (int i = 0; i < n - 1; ++i) {
int u, v;
cin >> u >> v;
qu[i] = u, qv[i] = v;
g[u].push_back(v);
g[v].push_back(u);
}
dfs(1, 1);
id[1] = 1;
cnt = 2;
chain[1] = 1;
dfs_hld(1, 1, 1);
for (int i = 0; i < n - 1; ++i) {
vector<vector<pair<int, int>>> vec = get(qu[i], a[qv[i]]);
cout << solve(vec) << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
72272 KB |
Output is correct |
2 |
Correct |
31 ms |
72284 KB |
Output is correct |
3 |
Correct |
32 ms |
72284 KB |
Output is correct |
4 |
Correct |
33 ms |
72488 KB |
Output is correct |
5 |
Correct |
33 ms |
72540 KB |
Output is correct |
6 |
Correct |
32 ms |
72540 KB |
Output is correct |
7 |
Correct |
32 ms |
72540 KB |
Output is correct |
8 |
Correct |
32 ms |
72512 KB |
Output is correct |
9 |
Correct |
32 ms |
72536 KB |
Output is correct |
10 |
Correct |
32 ms |
72536 KB |
Output is correct |
11 |
Correct |
32 ms |
72540 KB |
Output is correct |
12 |
Correct |
32 ms |
72524 KB |
Output is correct |
13 |
Correct |
31 ms |
72540 KB |
Output is correct |
14 |
Correct |
32 ms |
72540 KB |
Output is correct |
15 |
Correct |
32 ms |
72532 KB |
Output is correct |
16 |
Correct |
32 ms |
72528 KB |
Output is correct |
17 |
Correct |
32 ms |
72540 KB |
Output is correct |
18 |
Correct |
32 ms |
72540 KB |
Output is correct |
19 |
Correct |
33 ms |
72792 KB |
Output is correct |
20 |
Correct |
32 ms |
72532 KB |
Output is correct |
21 |
Correct |
33 ms |
72540 KB |
Output is correct |
22 |
Correct |
35 ms |
72540 KB |
Output is correct |
23 |
Correct |
36 ms |
72532 KB |
Output is correct |
24 |
Correct |
35 ms |
72532 KB |
Output is correct |
25 |
Correct |
34 ms |
72528 KB |
Output is correct |
26 |
Correct |
32 ms |
72528 KB |
Output is correct |
27 |
Correct |
32 ms |
72536 KB |
Output is correct |
28 |
Correct |
32 ms |
72540 KB |
Output is correct |
29 |
Correct |
33 ms |
72568 KB |
Output is correct |
30 |
Correct |
33 ms |
72532 KB |
Output is correct |
31 |
Correct |
33 ms |
72540 KB |
Output is correct |
32 |
Correct |
32 ms |
72532 KB |
Output is correct |
33 |
Correct |
32 ms |
72532 KB |
Output is correct |
34 |
Correct |
32 ms |
72528 KB |
Output is correct |
35 |
Correct |
33 ms |
72540 KB |
Output is correct |
36 |
Correct |
32 ms |
72532 KB |
Output is correct |
37 |
Correct |
32 ms |
72324 KB |
Output is correct |
38 |
Correct |
39 ms |
72532 KB |
Output is correct |
39 |
Correct |
31 ms |
72536 KB |
Output is correct |
40 |
Correct |
32 ms |
72540 KB |
Output is correct |
41 |
Correct |
32 ms |
72536 KB |
Output is correct |
42 |
Correct |
32 ms |
72540 KB |
Output is correct |
43 |
Correct |
32 ms |
72284 KB |
Output is correct |
44 |
Correct |
32 ms |
72532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
72272 KB |
Output is correct |
2 |
Correct |
31 ms |
72284 KB |
Output is correct |
3 |
Correct |
32 ms |
72284 KB |
Output is correct |
4 |
Correct |
33 ms |
72488 KB |
Output is correct |
5 |
Correct |
33 ms |
72540 KB |
Output is correct |
6 |
Correct |
32 ms |
72540 KB |
Output is correct |
7 |
Correct |
32 ms |
72540 KB |
Output is correct |
8 |
Correct |
32 ms |
72512 KB |
Output is correct |
9 |
Correct |
32 ms |
72536 KB |
Output is correct |
10 |
Correct |
32 ms |
72536 KB |
Output is correct |
11 |
Correct |
32 ms |
72540 KB |
Output is correct |
12 |
Correct |
32 ms |
72524 KB |
Output is correct |
13 |
Correct |
31 ms |
72540 KB |
Output is correct |
14 |
Correct |
32 ms |
72540 KB |
Output is correct |
15 |
Correct |
32 ms |
72532 KB |
Output is correct |
16 |
Correct |
32 ms |
72528 KB |
Output is correct |
17 |
Correct |
32 ms |
72540 KB |
Output is correct |
18 |
Correct |
32 ms |
72540 KB |
Output is correct |
19 |
Correct |
33 ms |
72792 KB |
Output is correct |
20 |
Correct |
32 ms |
72532 KB |
Output is correct |
21 |
Correct |
33 ms |
72540 KB |
Output is correct |
22 |
Correct |
35 ms |
72540 KB |
Output is correct |
23 |
Correct |
36 ms |
72532 KB |
Output is correct |
24 |
Correct |
35 ms |
72532 KB |
Output is correct |
25 |
Correct |
34 ms |
72528 KB |
Output is correct |
26 |
Correct |
32 ms |
72528 KB |
Output is correct |
27 |
Correct |
32 ms |
72536 KB |
Output is correct |
28 |
Correct |
32 ms |
72540 KB |
Output is correct |
29 |
Correct |
33 ms |
72568 KB |
Output is correct |
30 |
Correct |
33 ms |
72532 KB |
Output is correct |
31 |
Correct |
33 ms |
72540 KB |
Output is correct |
32 |
Correct |
32 ms |
72532 KB |
Output is correct |
33 |
Correct |
32 ms |
72532 KB |
Output is correct |
34 |
Correct |
32 ms |
72528 KB |
Output is correct |
35 |
Correct |
33 ms |
72540 KB |
Output is correct |
36 |
Correct |
32 ms |
72532 KB |
Output is correct |
37 |
Correct |
32 ms |
72324 KB |
Output is correct |
38 |
Correct |
39 ms |
72532 KB |
Output is correct |
39 |
Correct |
31 ms |
72536 KB |
Output is correct |
40 |
Correct |
32 ms |
72540 KB |
Output is correct |
41 |
Correct |
32 ms |
72536 KB |
Output is correct |
42 |
Correct |
32 ms |
72540 KB |
Output is correct |
43 |
Correct |
32 ms |
72284 KB |
Output is correct |
44 |
Correct |
32 ms |
72532 KB |
Output is correct |
45 |
Correct |
34 ms |
72532 KB |
Output is correct |
46 |
Correct |
37 ms |
72752 KB |
Output is correct |
47 |
Correct |
38 ms |
72788 KB |
Output is correct |
48 |
Correct |
38 ms |
72796 KB |
Output is correct |
49 |
Correct |
36 ms |
73308 KB |
Output is correct |
50 |
Correct |
38 ms |
73224 KB |
Output is correct |
51 |
Correct |
37 ms |
73048 KB |
Output is correct |
52 |
Correct |
36 ms |
73044 KB |
Output is correct |
53 |
Correct |
35 ms |
73048 KB |
Output is correct |
54 |
Correct |
35 ms |
73048 KB |
Output is correct |
55 |
Correct |
35 ms |
73052 KB |
Output is correct |
56 |
Correct |
35 ms |
73044 KB |
Output is correct |
57 |
Correct |
38 ms |
72796 KB |
Output is correct |
58 |
Correct |
40 ms |
72784 KB |
Output is correct |
59 |
Correct |
41 ms |
72796 KB |
Output is correct |
60 |
Correct |
40 ms |
72776 KB |
Output is correct |
61 |
Correct |
36 ms |
73044 KB |
Output is correct |
62 |
Correct |
34 ms |
73304 KB |
Output is correct |
63 |
Correct |
36 ms |
72880 KB |
Output is correct |
64 |
Correct |
38 ms |
72796 KB |
Output is correct |
65 |
Correct |
44 ms |
72792 KB |
Output is correct |
66 |
Correct |
36 ms |
72660 KB |
Output is correct |
67 |
Correct |
37 ms |
72676 KB |
Output is correct |
68 |
Correct |
35 ms |
73048 KB |
Output is correct |
69 |
Correct |
35 ms |
73052 KB |
Output is correct |
70 |
Correct |
35 ms |
72936 KB |
Output is correct |
71 |
Correct |
35 ms |
72784 KB |
Output is correct |
72 |
Correct |
38 ms |
73180 KB |
Output is correct |
73 |
Correct |
38 ms |
72528 KB |
Output is correct |
74 |
Correct |
34 ms |
72788 KB |
Output is correct |
75 |
Correct |
35 ms |
73048 KB |
Output is correct |
76 |
Correct |
35 ms |
73000 KB |
Output is correct |
77 |
Correct |
36 ms |
72796 KB |
Output is correct |
78 |
Correct |
35 ms |
73040 KB |
Output is correct |
79 |
Correct |
35 ms |
72796 KB |
Output is correct |
80 |
Correct |
35 ms |
72796 KB |
Output is correct |
81 |
Correct |
36 ms |
72792 KB |
Output is correct |
82 |
Correct |
36 ms |
72796 KB |
Output is correct |
83 |
Correct |
37 ms |
72784 KB |
Output is correct |
84 |
Correct |
34 ms |
72784 KB |
Output is correct |
85 |
Correct |
35 ms |
72796 KB |
Output is correct |
86 |
Correct |
35 ms |
72532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
72272 KB |
Output is correct |
2 |
Correct |
31 ms |
72284 KB |
Output is correct |
3 |
Correct |
32 ms |
72284 KB |
Output is correct |
4 |
Correct |
33 ms |
72488 KB |
Output is correct |
5 |
Correct |
33 ms |
72540 KB |
Output is correct |
6 |
Correct |
32 ms |
72540 KB |
Output is correct |
7 |
Correct |
32 ms |
72540 KB |
Output is correct |
8 |
Correct |
32 ms |
72512 KB |
Output is correct |
9 |
Correct |
32 ms |
72536 KB |
Output is correct |
10 |
Correct |
32 ms |
72536 KB |
Output is correct |
11 |
Correct |
32 ms |
72540 KB |
Output is correct |
12 |
Correct |
32 ms |
72524 KB |
Output is correct |
13 |
Correct |
31 ms |
72540 KB |
Output is correct |
14 |
Correct |
32 ms |
72540 KB |
Output is correct |
15 |
Correct |
32 ms |
72532 KB |
Output is correct |
16 |
Correct |
32 ms |
72528 KB |
Output is correct |
17 |
Correct |
32 ms |
72540 KB |
Output is correct |
18 |
Correct |
32 ms |
72540 KB |
Output is correct |
19 |
Correct |
33 ms |
72792 KB |
Output is correct |
20 |
Correct |
32 ms |
72532 KB |
Output is correct |
21 |
Correct |
33 ms |
72540 KB |
Output is correct |
22 |
Correct |
35 ms |
72540 KB |
Output is correct |
23 |
Correct |
36 ms |
72532 KB |
Output is correct |
24 |
Correct |
35 ms |
72532 KB |
Output is correct |
25 |
Correct |
34 ms |
72528 KB |
Output is correct |
26 |
Correct |
32 ms |
72528 KB |
Output is correct |
27 |
Correct |
32 ms |
72536 KB |
Output is correct |
28 |
Correct |
32 ms |
72540 KB |
Output is correct |
29 |
Correct |
33 ms |
72568 KB |
Output is correct |
30 |
Correct |
33 ms |
72532 KB |
Output is correct |
31 |
Correct |
33 ms |
72540 KB |
Output is correct |
32 |
Correct |
32 ms |
72532 KB |
Output is correct |
33 |
Correct |
32 ms |
72532 KB |
Output is correct |
34 |
Correct |
32 ms |
72528 KB |
Output is correct |
35 |
Correct |
33 ms |
72540 KB |
Output is correct |
36 |
Correct |
32 ms |
72532 KB |
Output is correct |
37 |
Correct |
32 ms |
72324 KB |
Output is correct |
38 |
Correct |
39 ms |
72532 KB |
Output is correct |
39 |
Correct |
31 ms |
72536 KB |
Output is correct |
40 |
Correct |
32 ms |
72540 KB |
Output is correct |
41 |
Correct |
32 ms |
72536 KB |
Output is correct |
42 |
Correct |
32 ms |
72540 KB |
Output is correct |
43 |
Correct |
32 ms |
72284 KB |
Output is correct |
44 |
Correct |
32 ms |
72532 KB |
Output is correct |
45 |
Correct |
34 ms |
72532 KB |
Output is correct |
46 |
Correct |
37 ms |
72752 KB |
Output is correct |
47 |
Correct |
38 ms |
72788 KB |
Output is correct |
48 |
Correct |
38 ms |
72796 KB |
Output is correct |
49 |
Correct |
36 ms |
73308 KB |
Output is correct |
50 |
Correct |
38 ms |
73224 KB |
Output is correct |
51 |
Correct |
37 ms |
73048 KB |
Output is correct |
52 |
Correct |
36 ms |
73044 KB |
Output is correct |
53 |
Correct |
35 ms |
73048 KB |
Output is correct |
54 |
Correct |
35 ms |
73048 KB |
Output is correct |
55 |
Correct |
35 ms |
73052 KB |
Output is correct |
56 |
Correct |
35 ms |
73044 KB |
Output is correct |
57 |
Correct |
38 ms |
72796 KB |
Output is correct |
58 |
Correct |
40 ms |
72784 KB |
Output is correct |
59 |
Correct |
41 ms |
72796 KB |
Output is correct |
60 |
Correct |
40 ms |
72776 KB |
Output is correct |
61 |
Correct |
36 ms |
73044 KB |
Output is correct |
62 |
Correct |
34 ms |
73304 KB |
Output is correct |
63 |
Correct |
36 ms |
72880 KB |
Output is correct |
64 |
Correct |
38 ms |
72796 KB |
Output is correct |
65 |
Correct |
44 ms |
72792 KB |
Output is correct |
66 |
Correct |
36 ms |
72660 KB |
Output is correct |
67 |
Correct |
37 ms |
72676 KB |
Output is correct |
68 |
Correct |
35 ms |
73048 KB |
Output is correct |
69 |
Correct |
35 ms |
73052 KB |
Output is correct |
70 |
Correct |
35 ms |
72936 KB |
Output is correct |
71 |
Correct |
35 ms |
72784 KB |
Output is correct |
72 |
Correct |
38 ms |
73180 KB |
Output is correct |
73 |
Correct |
38 ms |
72528 KB |
Output is correct |
74 |
Correct |
34 ms |
72788 KB |
Output is correct |
75 |
Correct |
35 ms |
73048 KB |
Output is correct |
76 |
Correct |
35 ms |
73000 KB |
Output is correct |
77 |
Correct |
36 ms |
72796 KB |
Output is correct |
78 |
Correct |
35 ms |
73040 KB |
Output is correct |
79 |
Correct |
35 ms |
72796 KB |
Output is correct |
80 |
Correct |
35 ms |
72796 KB |
Output is correct |
81 |
Correct |
36 ms |
72792 KB |
Output is correct |
82 |
Correct |
36 ms |
72796 KB |
Output is correct |
83 |
Correct |
37 ms |
72784 KB |
Output is correct |
84 |
Correct |
34 ms |
72784 KB |
Output is correct |
85 |
Correct |
35 ms |
72796 KB |
Output is correct |
86 |
Correct |
35 ms |
72532 KB |
Output is correct |
87 |
Correct |
46 ms |
73556 KB |
Output is correct |
88 |
Correct |
83 ms |
75860 KB |
Output is correct |
89 |
Correct |
233 ms |
84308 KB |
Output is correct |
90 |
Correct |
238 ms |
84696 KB |
Output is correct |
91 |
Correct |
229 ms |
84376 KB |
Output is correct |
92 |
Correct |
154 ms |
92608 KB |
Output is correct |
93 |
Correct |
161 ms |
92856 KB |
Output is correct |
94 |
Correct |
151 ms |
92752 KB |
Output is correct |
95 |
Correct |
148 ms |
88992 KB |
Output is correct |
96 |
Correct |
158 ms |
89576 KB |
Output is correct |
97 |
Correct |
148 ms |
89236 KB |
Output is correct |
98 |
Correct |
147 ms |
89300 KB |
Output is correct |
99 |
Correct |
149 ms |
88572 KB |
Output is correct |
100 |
Correct |
283 ms |
83796 KB |
Output is correct |
101 |
Correct |
319 ms |
84236 KB |
Output is correct |
102 |
Correct |
330 ms |
84268 KB |
Output is correct |
103 |
Correct |
319 ms |
84184 KB |
Output is correct |
104 |
Correct |
159 ms |
88748 KB |
Output is correct |
105 |
Correct |
157 ms |
88400 KB |
Output is correct |
106 |
Correct |
154 ms |
88404 KB |
Output is correct |
107 |
Correct |
191 ms |
82836 KB |
Output is correct |
108 |
Correct |
186 ms |
78716 KB |
Output is correct |
109 |
Correct |
215 ms |
80272 KB |
Output is correct |
110 |
Correct |
111 ms |
87120 KB |
Output is correct |
111 |
Correct |
144 ms |
88856 KB |
Output is correct |
112 |
Correct |
103 ms |
83656 KB |
Output is correct |
113 |
Correct |
106 ms |
82772 KB |
Output is correct |
114 |
Correct |
297 ms |
83792 KB |
Output is correct |
115 |
Correct |
274 ms |
78752 KB |
Output is correct |
116 |
Correct |
108 ms |
82964 KB |
Output is correct |
117 |
Correct |
156 ms |
85904 KB |
Output is correct |
118 |
Correct |
149 ms |
85328 KB |
Output is correct |
119 |
Correct |
152 ms |
85116 KB |
Output is correct |
120 |
Correct |
112 ms |
80720 KB |
Output is correct |
121 |
Correct |
111 ms |
80212 KB |
Output is correct |
122 |
Correct |
108 ms |
80008 KB |
Output is correct |
123 |
Correct |
156 ms |
86100 KB |
Output is correct |
124 |
Correct |
156 ms |
85380 KB |
Output is correct |
125 |
Correct |
162 ms |
85076 KB |
Output is correct |
126 |
Correct |
125 ms |
80976 KB |
Output is correct |
127 |
Correct |
117 ms |
80396 KB |
Output is correct |
128 |
Correct |
117 ms |
79948 KB |
Output is correct |