// ↘ ⬇ ⬇ ⬇ ⬇ ⬇ ↙
// ➡ IOI, IZhO ⬅
// ↗ ⬆ ⬆ ⬆ ⬆ ⬆ ↖
//⠀⠀⢠⣤⡄⢠⣤⣤⣤⣤⣤⣤⡄⢠⣤⡄⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⢸⣿⡇⢸⣿⣿⣿⣿⣿⣿⡇⢸⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⢸⣿⡇⢸⣿⣿⣿⣿⣿⣿⡇⢸⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⢸⣿⡇⢸⣿⣿⣿⣿⣿⣿⡇⢸⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⢸⣿⡇⢸⣿⣿⣿⣿⣿⣿⡇⢸⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⢸⣿⡇⢸⣿⣿⣿⣿⣿⣿⡇⢸⣿⡇⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠙⠃⠘⠛⠛⠛⠛⠛⠛⠃⠘⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀ ⠘⠛⠛⠛⠛⠛⠛⠛⠛⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⣠⣶⣶⠂⣰⣶⣶⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⢀⣾⣿⣿⡏⢠⣿⣿⣿⣿⣿⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⡈⠻⣿⣿⣶⣾⣿⣿⣿⣿⠿⠋⡀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⣿⣶⣄⠙⣿⣿⣿⣿⣟⢁⣴⣾⡇⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⢹⣿⡏⢠⣿⠟⠛⢿⣿⣿⣿⡿⠁⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠙⠀⠀⣠⣶⣷⣤⡈⠛⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀
//⠀⠀⠀⠀⠀⠀⠈⠉⠛⠛⠋⠉⠀⠀
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(), x.end()
#define size(x) (int)x.size()
#define pb push_back
#define eb emplace_back
template<typename S>
bool chmin(S &a, const S b) {
if (a > b) {
a = b;
return true;
}
return false;
}
template<typename S>
bool chmax(S &a, const S b) {
if (a < b) {
a = b;
return true;
}
return false;
}
const int mod = 1e9 + 7;
const int inf = 1e9 + 7;
const int INF = 1e18 + 7;
const double eps = 1e-6;
const double EPS = 1e-9;
vector<int> path, g[100000];
struct Fenwick_Tree {
int n;
vector<int> t;
Fenwick_Tree(int n) {
this->n = n;
t.assign(n, 0);
}
void upd(int i, int x) {
for (; i < n; i = i | (i + 1)) {
t[i] += x;
}
}
int get(int i) {
int res = 0;
for (; i >= 0; i = (i & (i + 1)) - 1) {
res += t[i];
}
return res;
}
};
void compress(vector<int> &c) {
vector<int> v;
unordered_map<int, int> mp;
for (int i = 0; i < size(c); ++i) {
if (!mp.count(c[i])) {
v.pb(c[i]);
mp[c[i]];
}
}
sort(all(v));
for (int i = 0; i < size(v); ++i) {
mp[v[i]] = i;
}
for (int i = 0; i < size(c); ++i) {
c[i] = mp[c[i]];
}
}
bool dfs(int v, int p, int f) {
if (v == f) {
path.pb(v);
return true;
}
for (int to : g[v]) {
if (to == p) {
continue;
}
if (dfs(to, v, f)) {
path.pb(v);
return true;
}
}
return false;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> c(n);
for (int i = 0; i < n; ++i) {
cin >> c[i];
}
compress(c);
Fenwick_Tree t(100000);
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
a--, --b;
path.clear();
dfs(0, -1, a);
reverse(all(path));
t.upd(c[path[0]], 1);
g[a].pb(b);
g[b].pb(a);
int res = 0;
for (int j = 1; j < size(path); ++j) {
t.upd(c[path[j]], 1);
res += j + 1 - t.get(c[path[j]]);
}
cout << res << '\n';
for (int j = 0; j < size(path); ++j) {
t.upd(c[path[j]], -1);
c[path[j]] = c[b];
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
2 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
2 ms |
3420 KB |
Output is correct |
6 |
Correct |
2 ms |
3672 KB |
Output is correct |
7 |
Correct |
2 ms |
3420 KB |
Output is correct |
8 |
Correct |
6 ms |
3884 KB |
Output is correct |
9 |
Correct |
5 ms |
3672 KB |
Output is correct |
10 |
Correct |
5 ms |
3676 KB |
Output is correct |
11 |
Correct |
3 ms |
3420 KB |
Output is correct |
12 |
Correct |
3 ms |
3652 KB |
Output is correct |
13 |
Correct |
4 ms |
3420 KB |
Output is correct |
14 |
Correct |
4 ms |
3584 KB |
Output is correct |
15 |
Correct |
2 ms |
3416 KB |
Output is correct |
16 |
Correct |
3 ms |
3420 KB |
Output is correct |
17 |
Correct |
3 ms |
3416 KB |
Output is correct |
18 |
Correct |
2 ms |
3420 KB |
Output is correct |
19 |
Correct |
3 ms |
3420 KB |
Output is correct |
20 |
Correct |
3 ms |
3420 KB |
Output is correct |
21 |
Correct |
5 ms |
3420 KB |
Output is correct |
22 |
Correct |
2 ms |
3416 KB |
Output is correct |
23 |
Correct |
2 ms |
3572 KB |
Output is correct |
24 |
Correct |
2 ms |
3420 KB |
Output is correct |
25 |
Correct |
2 ms |
3420 KB |
Output is correct |
26 |
Correct |
7 ms |
3420 KB |
Output is correct |
27 |
Correct |
3 ms |
3420 KB |
Output is correct |
28 |
Correct |
4 ms |
3420 KB |
Output is correct |
29 |
Correct |
4 ms |
3420 KB |
Output is correct |
30 |
Correct |
2 ms |
3580 KB |
Output is correct |
31 |
Correct |
2 ms |
3420 KB |
Output is correct |
32 |
Correct |
4 ms |
3420 KB |
Output is correct |
33 |
Correct |
4 ms |
3420 KB |
Output is correct |
34 |
Correct |
2 ms |
3420 KB |
Output is correct |
35 |
Correct |
2 ms |
3420 KB |
Output is correct |
36 |
Correct |
3 ms |
3420 KB |
Output is correct |
37 |
Correct |
2 ms |
3584 KB |
Output is correct |
38 |
Correct |
2 ms |
3420 KB |
Output is correct |
39 |
Correct |
3 ms |
3420 KB |
Output is correct |
40 |
Correct |
2 ms |
3420 KB |
Output is correct |
41 |
Correct |
3 ms |
3420 KB |
Output is correct |
42 |
Correct |
4 ms |
3416 KB |
Output is correct |
43 |
Correct |
3 ms |
3420 KB |
Output is correct |
44 |
Correct |
2 ms |
3576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
2 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
2 ms |
3420 KB |
Output is correct |
6 |
Correct |
2 ms |
3672 KB |
Output is correct |
7 |
Correct |
2 ms |
3420 KB |
Output is correct |
8 |
Correct |
6 ms |
3884 KB |
Output is correct |
9 |
Correct |
5 ms |
3672 KB |
Output is correct |
10 |
Correct |
5 ms |
3676 KB |
Output is correct |
11 |
Correct |
3 ms |
3420 KB |
Output is correct |
12 |
Correct |
3 ms |
3652 KB |
Output is correct |
13 |
Correct |
4 ms |
3420 KB |
Output is correct |
14 |
Correct |
4 ms |
3584 KB |
Output is correct |
15 |
Correct |
2 ms |
3416 KB |
Output is correct |
16 |
Correct |
3 ms |
3420 KB |
Output is correct |
17 |
Correct |
3 ms |
3416 KB |
Output is correct |
18 |
Correct |
2 ms |
3420 KB |
Output is correct |
19 |
Correct |
3 ms |
3420 KB |
Output is correct |
20 |
Correct |
3 ms |
3420 KB |
Output is correct |
21 |
Correct |
5 ms |
3420 KB |
Output is correct |
22 |
Correct |
2 ms |
3416 KB |
Output is correct |
23 |
Correct |
2 ms |
3572 KB |
Output is correct |
24 |
Correct |
2 ms |
3420 KB |
Output is correct |
25 |
Correct |
2 ms |
3420 KB |
Output is correct |
26 |
Correct |
7 ms |
3420 KB |
Output is correct |
27 |
Correct |
3 ms |
3420 KB |
Output is correct |
28 |
Correct |
4 ms |
3420 KB |
Output is correct |
29 |
Correct |
4 ms |
3420 KB |
Output is correct |
30 |
Correct |
2 ms |
3580 KB |
Output is correct |
31 |
Correct |
2 ms |
3420 KB |
Output is correct |
32 |
Correct |
4 ms |
3420 KB |
Output is correct |
33 |
Correct |
4 ms |
3420 KB |
Output is correct |
34 |
Correct |
2 ms |
3420 KB |
Output is correct |
35 |
Correct |
2 ms |
3420 KB |
Output is correct |
36 |
Correct |
3 ms |
3420 KB |
Output is correct |
37 |
Correct |
2 ms |
3584 KB |
Output is correct |
38 |
Correct |
2 ms |
3420 KB |
Output is correct |
39 |
Correct |
3 ms |
3420 KB |
Output is correct |
40 |
Correct |
2 ms |
3420 KB |
Output is correct |
41 |
Correct |
3 ms |
3420 KB |
Output is correct |
42 |
Correct |
4 ms |
3416 KB |
Output is correct |
43 |
Correct |
3 ms |
3420 KB |
Output is correct |
44 |
Correct |
2 ms |
3576 KB |
Output is correct |
45 |
Correct |
3 ms |
3676 KB |
Output is correct |
46 |
Correct |
27 ms |
3912 KB |
Output is correct |
47 |
Correct |
27 ms |
3676 KB |
Output is correct |
48 |
Correct |
39 ms |
3672 KB |
Output is correct |
49 |
Correct |
261 ms |
4180 KB |
Output is correct |
50 |
Correct |
242 ms |
4076 KB |
Output is correct |
51 |
Correct |
251 ms |
4256 KB |
Output is correct |
52 |
Correct |
148 ms |
3968 KB |
Output is correct |
53 |
Correct |
126 ms |
3928 KB |
Output is correct |
54 |
Correct |
122 ms |
3996 KB |
Output is correct |
55 |
Correct |
138 ms |
4200 KB |
Output is correct |
56 |
Correct |
156 ms |
3992 KB |
Output is correct |
57 |
Correct |
28 ms |
3676 KB |
Output is correct |
58 |
Correct |
29 ms |
3928 KB |
Output is correct |
59 |
Correct |
37 ms |
3676 KB |
Output is correct |
60 |
Correct |
28 ms |
3676 KB |
Output is correct |
61 |
Correct |
121 ms |
3964 KB |
Output is correct |
62 |
Correct |
123 ms |
4008 KB |
Output is correct |
63 |
Correct |
129 ms |
4008 KB |
Output is correct |
64 |
Correct |
29 ms |
3812 KB |
Output is correct |
65 |
Correct |
28 ms |
3588 KB |
Output is correct |
66 |
Correct |
30 ms |
3676 KB |
Output is correct |
67 |
Correct |
26 ms |
3824 KB |
Output is correct |
68 |
Correct |
293 ms |
4064 KB |
Output is correct |
69 |
Correct |
121 ms |
3984 KB |
Output is correct |
70 |
Correct |
183 ms |
3852 KB |
Output is correct |
71 |
Correct |
142 ms |
3920 KB |
Output is correct |
72 |
Correct |
29 ms |
3672 KB |
Output is correct |
73 |
Correct |
26 ms |
3772 KB |
Output is correct |
74 |
Correct |
171 ms |
3904 KB |
Output is correct |
75 |
Correct |
84 ms |
3928 KB |
Output is correct |
76 |
Correct |
69 ms |
3936 KB |
Output is correct |
77 |
Correct |
64 ms |
3672 KB |
Output is correct |
78 |
Correct |
90 ms |
3836 KB |
Output is correct |
79 |
Correct |
77 ms |
3748 KB |
Output is correct |
80 |
Correct |
84 ms |
3736 KB |
Output is correct |
81 |
Correct |
115 ms |
3940 KB |
Output is correct |
82 |
Correct |
72 ms |
3932 KB |
Output is correct |
83 |
Correct |
72 ms |
3840 KB |
Output is correct |
84 |
Correct |
126 ms |
3672 KB |
Output is correct |
85 |
Correct |
89 ms |
3752 KB |
Output is correct |
86 |
Correct |
97 ms |
3776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3416 KB |
Output is correct |
2 |
Correct |
2 ms |
3420 KB |
Output is correct |
3 |
Correct |
2 ms |
3420 KB |
Output is correct |
4 |
Correct |
1 ms |
3420 KB |
Output is correct |
5 |
Correct |
2 ms |
3420 KB |
Output is correct |
6 |
Correct |
2 ms |
3672 KB |
Output is correct |
7 |
Correct |
2 ms |
3420 KB |
Output is correct |
8 |
Correct |
6 ms |
3884 KB |
Output is correct |
9 |
Correct |
5 ms |
3672 KB |
Output is correct |
10 |
Correct |
5 ms |
3676 KB |
Output is correct |
11 |
Correct |
3 ms |
3420 KB |
Output is correct |
12 |
Correct |
3 ms |
3652 KB |
Output is correct |
13 |
Correct |
4 ms |
3420 KB |
Output is correct |
14 |
Correct |
4 ms |
3584 KB |
Output is correct |
15 |
Correct |
2 ms |
3416 KB |
Output is correct |
16 |
Correct |
3 ms |
3420 KB |
Output is correct |
17 |
Correct |
3 ms |
3416 KB |
Output is correct |
18 |
Correct |
2 ms |
3420 KB |
Output is correct |
19 |
Correct |
3 ms |
3420 KB |
Output is correct |
20 |
Correct |
3 ms |
3420 KB |
Output is correct |
21 |
Correct |
5 ms |
3420 KB |
Output is correct |
22 |
Correct |
2 ms |
3416 KB |
Output is correct |
23 |
Correct |
2 ms |
3572 KB |
Output is correct |
24 |
Correct |
2 ms |
3420 KB |
Output is correct |
25 |
Correct |
2 ms |
3420 KB |
Output is correct |
26 |
Correct |
7 ms |
3420 KB |
Output is correct |
27 |
Correct |
3 ms |
3420 KB |
Output is correct |
28 |
Correct |
4 ms |
3420 KB |
Output is correct |
29 |
Correct |
4 ms |
3420 KB |
Output is correct |
30 |
Correct |
2 ms |
3580 KB |
Output is correct |
31 |
Correct |
2 ms |
3420 KB |
Output is correct |
32 |
Correct |
4 ms |
3420 KB |
Output is correct |
33 |
Correct |
4 ms |
3420 KB |
Output is correct |
34 |
Correct |
2 ms |
3420 KB |
Output is correct |
35 |
Correct |
2 ms |
3420 KB |
Output is correct |
36 |
Correct |
3 ms |
3420 KB |
Output is correct |
37 |
Correct |
2 ms |
3584 KB |
Output is correct |
38 |
Correct |
2 ms |
3420 KB |
Output is correct |
39 |
Correct |
3 ms |
3420 KB |
Output is correct |
40 |
Correct |
2 ms |
3420 KB |
Output is correct |
41 |
Correct |
3 ms |
3420 KB |
Output is correct |
42 |
Correct |
4 ms |
3416 KB |
Output is correct |
43 |
Correct |
3 ms |
3420 KB |
Output is correct |
44 |
Correct |
2 ms |
3576 KB |
Output is correct |
45 |
Correct |
3 ms |
3676 KB |
Output is correct |
46 |
Correct |
27 ms |
3912 KB |
Output is correct |
47 |
Correct |
27 ms |
3676 KB |
Output is correct |
48 |
Correct |
39 ms |
3672 KB |
Output is correct |
49 |
Correct |
261 ms |
4180 KB |
Output is correct |
50 |
Correct |
242 ms |
4076 KB |
Output is correct |
51 |
Correct |
251 ms |
4256 KB |
Output is correct |
52 |
Correct |
148 ms |
3968 KB |
Output is correct |
53 |
Correct |
126 ms |
3928 KB |
Output is correct |
54 |
Correct |
122 ms |
3996 KB |
Output is correct |
55 |
Correct |
138 ms |
4200 KB |
Output is correct |
56 |
Correct |
156 ms |
3992 KB |
Output is correct |
57 |
Correct |
28 ms |
3676 KB |
Output is correct |
58 |
Correct |
29 ms |
3928 KB |
Output is correct |
59 |
Correct |
37 ms |
3676 KB |
Output is correct |
60 |
Correct |
28 ms |
3676 KB |
Output is correct |
61 |
Correct |
121 ms |
3964 KB |
Output is correct |
62 |
Correct |
123 ms |
4008 KB |
Output is correct |
63 |
Correct |
129 ms |
4008 KB |
Output is correct |
64 |
Correct |
29 ms |
3812 KB |
Output is correct |
65 |
Correct |
28 ms |
3588 KB |
Output is correct |
66 |
Correct |
30 ms |
3676 KB |
Output is correct |
67 |
Correct |
26 ms |
3824 KB |
Output is correct |
68 |
Correct |
293 ms |
4064 KB |
Output is correct |
69 |
Correct |
121 ms |
3984 KB |
Output is correct |
70 |
Correct |
183 ms |
3852 KB |
Output is correct |
71 |
Correct |
142 ms |
3920 KB |
Output is correct |
72 |
Correct |
29 ms |
3672 KB |
Output is correct |
73 |
Correct |
26 ms |
3772 KB |
Output is correct |
74 |
Correct |
171 ms |
3904 KB |
Output is correct |
75 |
Correct |
84 ms |
3928 KB |
Output is correct |
76 |
Correct |
69 ms |
3936 KB |
Output is correct |
77 |
Correct |
64 ms |
3672 KB |
Output is correct |
78 |
Correct |
90 ms |
3836 KB |
Output is correct |
79 |
Correct |
77 ms |
3748 KB |
Output is correct |
80 |
Correct |
84 ms |
3736 KB |
Output is correct |
81 |
Correct |
115 ms |
3940 KB |
Output is correct |
82 |
Correct |
72 ms |
3932 KB |
Output is correct |
83 |
Correct |
72 ms |
3840 KB |
Output is correct |
84 |
Correct |
126 ms |
3672 KB |
Output is correct |
85 |
Correct |
89 ms |
3752 KB |
Output is correct |
86 |
Correct |
97 ms |
3776 KB |
Output is correct |
87 |
Correct |
271 ms |
4340 KB |
Output is correct |
88 |
Execution timed out |
2067 ms |
5436 KB |
Time limit exceeded |
89 |
Halted |
0 ms |
0 KB |
- |