// #pragma GCC optimize ("Ofast")
// #pragma GCC target ("avx2")
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include <debug.h>
#else
#define debug(...)
#endif
#define ft front
#define bk back
#define st first
#define nd second
#define ins insert
#define ers erase
#define pb push_back
#define pf push_front
#define _pb pop_back
#define _pf pop_front
#define lb lower_bound
#define ub upper_bound
#define mtp make_tuple
#define bg begin
#define ed end
#define all(x) (x).bg(), (x).ed()
#define sz(x) (int)(x).size()
// #define int long long
typedef long long ll; typedef unsigned long long ull;
typedef double db; typedef long double ldb;
typedef pair<int, int> pi; typedef pair<ll, ll> pll;
typedef vector<int> vi; typedef vector<ll> vll; typedef vector<pi> vpi; typedef vector<pll> vpll;
typedef string str;
#define FOR(i, l, r) for (int i = (l); i <= (r); ++i)
#define FOS(i, r, l) for (int i = (r); i >= (l); --i)
#define FRN(i, n) for (int i = 0; i < (n); ++i)
#define FSN(i, n) for (int i = (n) - 1; i >= 0; --i)
#define EACH(i, x) for (auto &i : (x))
#define WHILE while
template<typename T> T gcd(T a, T b) { T d2 = (a | b) & -(a | b); a /= d2; b /= d2; WHILE(b) { a = a % b; swap(a, b); } return a * d2; }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
void _assert(bool statement) { if (statement) return; cerr << "\n>> Assertion failed!\n"; exit(0); }
void _assert(bool statement, const str &message) { if (statement) return; cerr << "\n>> Assertion failed: " << message << '\n'; exit(0); }
void _error(const str &message) { cerr << "\n>> Error: " << message << '\n'; exit(0); }
#define file "TEST"
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { return uniform_int_distribution<ll>(l, r)(rd); }
/*
----------------------------------------------------------------
END OF TEMPLATE
----------------------------------------------------------------
Tran The Bao - ghostwriter
Training for VOI23 gold medal
----------------------------------------------------------------
GOAT
----------------------------------------------------------------
*/
const int N = 1e5 + 5;
int n, f[N], c[N], s[N], h[N], p[N], heavy[N], pos[N], head[N], num[N], timer = 1;
vi adj[N];
vpi a[N], hist;
pi e[N];
void input(int test_id) {
cin >> n;
FOR(i, 1, n) cin >> c[i];
FOR(i, 1, n - 1) {
cin >> e[i].st >> e[i].nd;
adj[e[i].st].pb(e[i].nd);
}
}
void compress(int c[]) {
vi v;
FOR(i, 1, n) v.pb(c[i]);
sort(all(v));
FOR(i, 1, n) c[i] = lb(all(v), c[i]) - v.bg() + 1;
}
void upd(int pos, int v) { hist.pb({pos, v}); for (int i = pos; i <= n; i += (i & -i)) f[i] += v; }
int get(int pos) { int rs = 0; for (int i = pos; i > 0; i -= (i & -i)) rs += f[i]; return rs; }
void clear() { EACH(j, hist) { int pos = j.st, v = j.nd; for (int i = pos; i <= n; i += (i & -i)) f[i] -= v; } hist.clear(); }
void dfs(int u) {
EACH(v, adj[u]) {
h[v] = h[u] + 1;
p[v] = u;
dfs(v);
s[u] += s[v];
if (!heavy[u]) heavy[u] = v;
else if (s[heavy[u]] < s[v]) heavy[v] = u;
}
}
void hld(int u) {
pos[u] = timer;
if (a[pos[u]].empty()) head[pos[u]] = u;
a[pos[u]].pb({c[u], 1});
num[u] = sz(a[pos[u]]);
if (heavy[u]) hld(heavy[u]);
EACH(v, adj[u]) {
if (v == heavy[u]) continue;
++timer;
hld(v);
}
}
ll get(int u, int v) {
ll rs = 0;
WHILE(u) {
vpi a1;
int cpos = pos[u], cnum = num[u];
FSN(j, sz(a[cpos])) {
if (!cnum) break;
int tk = min(cnum, a[cpos][j].nd);
bool ok = 1;
if (tk == a[cpos][j].nd) a[cpos]._pb();
else {
a[cpos][j].nd -= tk;
ok = 0;
}
cnum -= tk;
a1.pb({a[cpos][j].st, tk});
if (!ok) break;
}
reverse(all(a1));
EACH(i, a1) {
rs += 1LL * i.nd * get(i.st - 1);
upd(i.st, i.nd);
}
a[cpos].pb({v, num[u]});
u = p[head[cpos]];
}
clear();
return rs;
}
void solve(int test_id) {
compress(c);
dfs(1);
hld(1);
FOR(i, 1, timer) reverse(all(a[i]));
FOR(i, 1, n - 1) {
int u = e[i].st, v = e[i].nd;
cout << get(u, c[v]) << '\n';
}
}
void reinit(int test_id) {
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
// freopen(file".inp", "r", stdin);
// freopen(file".out", "w", stdout);
int test_num = 1;
// cin >> test_num; // comment if the problem does not requires multitest
FOR(i, 1, test_num) {
input(i); // input in noninteractive problems for case #i
solve(i); // main function to solve case #i
reinit(i); // reinit global data to default used in case #i
}
#ifdef LOCAL
cerr << "\nTime: " << setprecision(5) << fixed << (ldb)clock() / CLOCKS_PER_SEC << "ms.\n";
#endif
return 0;
}
/*
----------------------------------------------------------------
From Benq:
stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH
----------------------------------------------------------------
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
4 ms |
5048 KB |
Output is correct |
7 |
Correct |
4 ms |
5044 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5176 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5048 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5048 KB |
Output is correct |
15 |
Correct |
3 ms |
5048 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5112 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
4 ms |
5048 KB |
Output is correct |
20 |
Correct |
4 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5040 KB |
Output is correct |
26 |
Correct |
3 ms |
5172 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5040 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5044 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5040 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
5128 KB |
Output is correct |
38 |
Correct |
3 ms |
5124 KB |
Output is correct |
39 |
Correct |
3 ms |
5076 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5052 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5088 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
4 ms |
5048 KB |
Output is correct |
7 |
Correct |
4 ms |
5044 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5176 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5048 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5048 KB |
Output is correct |
15 |
Correct |
3 ms |
5048 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5112 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
4 ms |
5048 KB |
Output is correct |
20 |
Correct |
4 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5040 KB |
Output is correct |
26 |
Correct |
3 ms |
5172 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5040 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5044 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5040 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
5128 KB |
Output is correct |
38 |
Correct |
3 ms |
5124 KB |
Output is correct |
39 |
Correct |
3 ms |
5076 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5052 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5088 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
4 ms |
5076 KB |
Output is correct |
46 |
Correct |
8 ms |
5464 KB |
Output is correct |
47 |
Correct |
8 ms |
5352 KB |
Output is correct |
48 |
Correct |
8 ms |
5432 KB |
Output is correct |
49 |
Correct |
6 ms |
5768 KB |
Output is correct |
50 |
Correct |
7 ms |
5712 KB |
Output is correct |
51 |
Correct |
8 ms |
5844 KB |
Output is correct |
52 |
Correct |
6 ms |
5620 KB |
Output is correct |
53 |
Correct |
5 ms |
5636 KB |
Output is correct |
54 |
Correct |
6 ms |
5588 KB |
Output is correct |
55 |
Correct |
6 ms |
5588 KB |
Output is correct |
56 |
Correct |
6 ms |
5560 KB |
Output is correct |
57 |
Correct |
9 ms |
5360 KB |
Output is correct |
58 |
Correct |
8 ms |
5460 KB |
Output is correct |
59 |
Correct |
9 ms |
5432 KB |
Output is correct |
60 |
Correct |
8 ms |
5460 KB |
Output is correct |
61 |
Correct |
6 ms |
5564 KB |
Output is correct |
62 |
Correct |
6 ms |
5588 KB |
Output is correct |
63 |
Correct |
6 ms |
5588 KB |
Output is correct |
64 |
Correct |
6 ms |
5332 KB |
Output is correct |
65 |
Correct |
7 ms |
5332 KB |
Output is correct |
66 |
Correct |
8 ms |
5432 KB |
Output is correct |
67 |
Correct |
8 ms |
5332 KB |
Output is correct |
68 |
Correct |
6 ms |
5692 KB |
Output is correct |
69 |
Correct |
5 ms |
5588 KB |
Output is correct |
70 |
Correct |
5 ms |
5588 KB |
Output is correct |
71 |
Correct |
5 ms |
5588 KB |
Output is correct |
72 |
Correct |
10 ms |
5432 KB |
Output is correct |
73 |
Correct |
8 ms |
5332 KB |
Output is correct |
74 |
Correct |
5 ms |
5564 KB |
Output is correct |
75 |
Correct |
6 ms |
5456 KB |
Output is correct |
76 |
Correct |
6 ms |
5440 KB |
Output is correct |
77 |
Correct |
6 ms |
5460 KB |
Output is correct |
78 |
Correct |
5 ms |
5460 KB |
Output is correct |
79 |
Correct |
7 ms |
5432 KB |
Output is correct |
80 |
Correct |
6 ms |
5460 KB |
Output is correct |
81 |
Correct |
6 ms |
5460 KB |
Output is correct |
82 |
Correct |
6 ms |
5512 KB |
Output is correct |
83 |
Correct |
6 ms |
5460 KB |
Output is correct |
84 |
Correct |
5 ms |
5432 KB |
Output is correct |
85 |
Correct |
6 ms |
5460 KB |
Output is correct |
86 |
Correct |
6 ms |
5460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
4 ms |
5048 KB |
Output is correct |
7 |
Correct |
4 ms |
5044 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5176 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5048 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5048 KB |
Output is correct |
15 |
Correct |
3 ms |
5048 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5112 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
4 ms |
5048 KB |
Output is correct |
20 |
Correct |
4 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5040 KB |
Output is correct |
26 |
Correct |
3 ms |
5172 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
3 ms |
5040 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5076 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
4 ms |
5076 KB |
Output is correct |
33 |
Correct |
3 ms |
5044 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5040 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
5128 KB |
Output is correct |
38 |
Correct |
3 ms |
5124 KB |
Output is correct |
39 |
Correct |
3 ms |
5076 KB |
Output is correct |
40 |
Correct |
3 ms |
5076 KB |
Output is correct |
41 |
Correct |
3 ms |
5052 KB |
Output is correct |
42 |
Correct |
3 ms |
5076 KB |
Output is correct |
43 |
Correct |
3 ms |
5088 KB |
Output is correct |
44 |
Correct |
3 ms |
5076 KB |
Output is correct |
45 |
Correct |
4 ms |
5076 KB |
Output is correct |
46 |
Correct |
8 ms |
5464 KB |
Output is correct |
47 |
Correct |
8 ms |
5352 KB |
Output is correct |
48 |
Correct |
8 ms |
5432 KB |
Output is correct |
49 |
Correct |
6 ms |
5768 KB |
Output is correct |
50 |
Correct |
7 ms |
5712 KB |
Output is correct |
51 |
Correct |
8 ms |
5844 KB |
Output is correct |
52 |
Correct |
6 ms |
5620 KB |
Output is correct |
53 |
Correct |
5 ms |
5636 KB |
Output is correct |
54 |
Correct |
6 ms |
5588 KB |
Output is correct |
55 |
Correct |
6 ms |
5588 KB |
Output is correct |
56 |
Correct |
6 ms |
5560 KB |
Output is correct |
57 |
Correct |
9 ms |
5360 KB |
Output is correct |
58 |
Correct |
8 ms |
5460 KB |
Output is correct |
59 |
Correct |
9 ms |
5432 KB |
Output is correct |
60 |
Correct |
8 ms |
5460 KB |
Output is correct |
61 |
Correct |
6 ms |
5564 KB |
Output is correct |
62 |
Correct |
6 ms |
5588 KB |
Output is correct |
63 |
Correct |
6 ms |
5588 KB |
Output is correct |
64 |
Correct |
6 ms |
5332 KB |
Output is correct |
65 |
Correct |
7 ms |
5332 KB |
Output is correct |
66 |
Correct |
8 ms |
5432 KB |
Output is correct |
67 |
Correct |
8 ms |
5332 KB |
Output is correct |
68 |
Correct |
6 ms |
5692 KB |
Output is correct |
69 |
Correct |
5 ms |
5588 KB |
Output is correct |
70 |
Correct |
5 ms |
5588 KB |
Output is correct |
71 |
Correct |
5 ms |
5588 KB |
Output is correct |
72 |
Correct |
10 ms |
5432 KB |
Output is correct |
73 |
Correct |
8 ms |
5332 KB |
Output is correct |
74 |
Correct |
5 ms |
5564 KB |
Output is correct |
75 |
Correct |
6 ms |
5456 KB |
Output is correct |
76 |
Correct |
6 ms |
5440 KB |
Output is correct |
77 |
Correct |
6 ms |
5460 KB |
Output is correct |
78 |
Correct |
5 ms |
5460 KB |
Output is correct |
79 |
Correct |
7 ms |
5432 KB |
Output is correct |
80 |
Correct |
6 ms |
5460 KB |
Output is correct |
81 |
Correct |
6 ms |
5460 KB |
Output is correct |
82 |
Correct |
6 ms |
5512 KB |
Output is correct |
83 |
Correct |
6 ms |
5460 KB |
Output is correct |
84 |
Correct |
5 ms |
5432 KB |
Output is correct |
85 |
Correct |
6 ms |
5460 KB |
Output is correct |
86 |
Correct |
6 ms |
5460 KB |
Output is correct |
87 |
Correct |
17 ms |
5968 KB |
Output is correct |
88 |
Correct |
51 ms |
7524 KB |
Output is correct |
89 |
Correct |
176 ms |
12972 KB |
Output is correct |
90 |
Correct |
167 ms |
12972 KB |
Output is correct |
91 |
Correct |
182 ms |
12996 KB |
Output is correct |
92 |
Correct |
87 ms |
21084 KB |
Output is correct |
93 |
Correct |
103 ms |
21136 KB |
Output is correct |
94 |
Correct |
87 ms |
21184 KB |
Output is correct |
95 |
Correct |
83 ms |
18268 KB |
Output is correct |
96 |
Correct |
93 ms |
18124 KB |
Output is correct |
97 |
Correct |
87 ms |
18136 KB |
Output is correct |
98 |
Correct |
89 ms |
18152 KB |
Output is correct |
99 |
Correct |
93 ms |
17076 KB |
Output is correct |
100 |
Correct |
217 ms |
13060 KB |
Output is correct |
101 |
Correct |
248 ms |
13144 KB |
Output is correct |
102 |
Correct |
279 ms |
13048 KB |
Output is correct |
103 |
Correct |
257 ms |
13160 KB |
Output is correct |
104 |
Correct |
108 ms |
16984 KB |
Output is correct |
105 |
Correct |
103 ms |
17000 KB |
Output is correct |
106 |
Correct |
96 ms |
16948 KB |
Output is correct |
107 |
Correct |
157 ms |
12804 KB |
Output is correct |
108 |
Correct |
155 ms |
13020 KB |
Output is correct |
109 |
Correct |
197 ms |
13040 KB |
Output is correct |
110 |
Correct |
81 ms |
21176 KB |
Output is correct |
111 |
Correct |
87 ms |
18180 KB |
Output is correct |
112 |
Correct |
82 ms |
18188 KB |
Output is correct |
113 |
Correct |
80 ms |
16936 KB |
Output is correct |
114 |
Correct |
233 ms |
13060 KB |
Output is correct |
115 |
Correct |
240 ms |
13024 KB |
Output is correct |
116 |
Correct |
84 ms |
17024 KB |
Output is correct |
117 |
Correct |
96 ms |
14972 KB |
Output is correct |
118 |
Correct |
91 ms |
14300 KB |
Output is correct |
119 |
Correct |
90 ms |
14188 KB |
Output is correct |
120 |
Correct |
79 ms |
14912 KB |
Output is correct |
121 |
Correct |
89 ms |
14396 KB |
Output is correct |
122 |
Correct |
93 ms |
14144 KB |
Output is correct |
123 |
Correct |
95 ms |
14996 KB |
Output is correct |
124 |
Correct |
99 ms |
14436 KB |
Output is correct |
125 |
Correct |
94 ms |
14004 KB |
Output is correct |
126 |
Correct |
94 ms |
15072 KB |
Output is correct |
127 |
Correct |
97 ms |
14372 KB |
Output is correct |
128 |
Correct |
90 ms |
14064 KB |
Output is correct |