// ↘ ⬇ ⬇ ⬇ ⬇ ⬇ ↙
// ➡ 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[4000];
struct Fenwick_Tree {
int n;
vector<int> t;
Fenwick_Tree(int n) {
this->n = n;
t.assign(n, 0);
}
void upd(int i) {
for (; i < n; i = i | (i + 1)) {
t[i]++;
}
}
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);
for (int i = 1; i < n; ++i) {
int a, b;
cin >> a >> b;
a--, --b;
path.clear();
dfs(0, -1, a);
reverse(all(path));
Fenwick_Tree t(4000);
t.upd(c[path[0]]);
c[path[0]] = c[b];
g[a].pb(b);
g[b].pb(a);
int res = 0;
for (int j = 1; j < size(path); ++j) {
t.upd(c[path[j]]);
res += j + 1 - t.get(c[path[j]]);
c[path[j]] = c[b];
}
cout << res << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
548 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
3 ms |
560 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
4 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
560 KB |
Output is correct |
19 |
Correct |
3 ms |
548 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
3 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
556 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
3 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
628 KB |
Output is correct |
36 |
Correct |
2 ms |
604 KB |
Output is correct |
37 |
Correct |
3 ms |
600 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
39 |
Correct |
3 ms |
396 KB |
Output is correct |
40 |
Correct |
3 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
2 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
548 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
3 ms |
560 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
4 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
560 KB |
Output is correct |
19 |
Correct |
3 ms |
548 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
3 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
556 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
3 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
628 KB |
Output is correct |
36 |
Correct |
2 ms |
604 KB |
Output is correct |
37 |
Correct |
3 ms |
600 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
39 |
Correct |
3 ms |
396 KB |
Output is correct |
40 |
Correct |
3 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
2 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
604 KB |
Output is correct |
45 |
Correct |
4 ms |
604 KB |
Output is correct |
46 |
Correct |
39 ms |
604 KB |
Output is correct |
47 |
Correct |
33 ms |
860 KB |
Output is correct |
48 |
Correct |
31 ms |
860 KB |
Output is correct |
49 |
Correct |
155 ms |
1124 KB |
Output is correct |
50 |
Correct |
157 ms |
856 KB |
Output is correct |
51 |
Correct |
159 ms |
1228 KB |
Output is correct |
52 |
Correct |
86 ms |
856 KB |
Output is correct |
53 |
Correct |
82 ms |
860 KB |
Output is correct |
54 |
Correct |
81 ms |
1112 KB |
Output is correct |
55 |
Correct |
85 ms |
860 KB |
Output is correct |
56 |
Correct |
83 ms |
920 KB |
Output is correct |
57 |
Correct |
31 ms |
600 KB |
Output is correct |
58 |
Correct |
34 ms |
856 KB |
Output is correct |
59 |
Correct |
31 ms |
600 KB |
Output is correct |
60 |
Correct |
31 ms |
860 KB |
Output is correct |
61 |
Correct |
85 ms |
956 KB |
Output is correct |
62 |
Correct |
85 ms |
1192 KB |
Output is correct |
63 |
Correct |
85 ms |
964 KB |
Output is correct |
64 |
Correct |
31 ms |
604 KB |
Output is correct |
65 |
Correct |
30 ms |
796 KB |
Output is correct |
66 |
Correct |
30 ms |
600 KB |
Output is correct |
67 |
Correct |
30 ms |
604 KB |
Output is correct |
68 |
Correct |
154 ms |
848 KB |
Output is correct |
69 |
Correct |
81 ms |
940 KB |
Output is correct |
70 |
Correct |
79 ms |
896 KB |
Output is correct |
71 |
Correct |
80 ms |
848 KB |
Output is correct |
72 |
Correct |
36 ms |
848 KB |
Output is correct |
73 |
Correct |
31 ms |
604 KB |
Output is correct |
74 |
Correct |
103 ms |
836 KB |
Output is correct |
75 |
Correct |
63 ms |
860 KB |
Output is correct |
76 |
Correct |
56 ms |
860 KB |
Output is correct |
77 |
Correct |
56 ms |
872 KB |
Output is correct |
78 |
Correct |
62 ms |
848 KB |
Output is correct |
79 |
Correct |
57 ms |
756 KB |
Output is correct |
80 |
Correct |
61 ms |
736 KB |
Output is correct |
81 |
Correct |
70 ms |
896 KB |
Output is correct |
82 |
Correct |
69 ms |
884 KB |
Output is correct |
83 |
Correct |
63 ms |
872 KB |
Output is correct |
84 |
Correct |
66 ms |
768 KB |
Output is correct |
85 |
Correct |
64 ms |
748 KB |
Output is correct |
86 |
Correct |
59 ms |
836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
548 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
604 KB |
Output is correct |
6 |
Correct |
2 ms |
600 KB |
Output is correct |
7 |
Correct |
3 ms |
560 KB |
Output is correct |
8 |
Correct |
3 ms |
604 KB |
Output is correct |
9 |
Correct |
4 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
560 KB |
Output is correct |
19 |
Correct |
3 ms |
548 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
3 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
556 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
3 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
604 KB |
Output is correct |
30 |
Correct |
2 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
604 KB |
Output is correct |
32 |
Correct |
3 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
2 ms |
628 KB |
Output is correct |
36 |
Correct |
2 ms |
604 KB |
Output is correct |
37 |
Correct |
3 ms |
600 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
39 |
Correct |
3 ms |
396 KB |
Output is correct |
40 |
Correct |
3 ms |
604 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
2 ms |
604 KB |
Output is correct |
43 |
Correct |
2 ms |
604 KB |
Output is correct |
44 |
Correct |
2 ms |
604 KB |
Output is correct |
45 |
Correct |
4 ms |
604 KB |
Output is correct |
46 |
Correct |
39 ms |
604 KB |
Output is correct |
47 |
Correct |
33 ms |
860 KB |
Output is correct |
48 |
Correct |
31 ms |
860 KB |
Output is correct |
49 |
Correct |
155 ms |
1124 KB |
Output is correct |
50 |
Correct |
157 ms |
856 KB |
Output is correct |
51 |
Correct |
159 ms |
1228 KB |
Output is correct |
52 |
Correct |
86 ms |
856 KB |
Output is correct |
53 |
Correct |
82 ms |
860 KB |
Output is correct |
54 |
Correct |
81 ms |
1112 KB |
Output is correct |
55 |
Correct |
85 ms |
860 KB |
Output is correct |
56 |
Correct |
83 ms |
920 KB |
Output is correct |
57 |
Correct |
31 ms |
600 KB |
Output is correct |
58 |
Correct |
34 ms |
856 KB |
Output is correct |
59 |
Correct |
31 ms |
600 KB |
Output is correct |
60 |
Correct |
31 ms |
860 KB |
Output is correct |
61 |
Correct |
85 ms |
956 KB |
Output is correct |
62 |
Correct |
85 ms |
1192 KB |
Output is correct |
63 |
Correct |
85 ms |
964 KB |
Output is correct |
64 |
Correct |
31 ms |
604 KB |
Output is correct |
65 |
Correct |
30 ms |
796 KB |
Output is correct |
66 |
Correct |
30 ms |
600 KB |
Output is correct |
67 |
Correct |
30 ms |
604 KB |
Output is correct |
68 |
Correct |
154 ms |
848 KB |
Output is correct |
69 |
Correct |
81 ms |
940 KB |
Output is correct |
70 |
Correct |
79 ms |
896 KB |
Output is correct |
71 |
Correct |
80 ms |
848 KB |
Output is correct |
72 |
Correct |
36 ms |
848 KB |
Output is correct |
73 |
Correct |
31 ms |
604 KB |
Output is correct |
74 |
Correct |
103 ms |
836 KB |
Output is correct |
75 |
Correct |
63 ms |
860 KB |
Output is correct |
76 |
Correct |
56 ms |
860 KB |
Output is correct |
77 |
Correct |
56 ms |
872 KB |
Output is correct |
78 |
Correct |
62 ms |
848 KB |
Output is correct |
79 |
Correct |
57 ms |
756 KB |
Output is correct |
80 |
Correct |
61 ms |
736 KB |
Output is correct |
81 |
Correct |
70 ms |
896 KB |
Output is correct |
82 |
Correct |
69 ms |
884 KB |
Output is correct |
83 |
Correct |
63 ms |
872 KB |
Output is correct |
84 |
Correct |
66 ms |
768 KB |
Output is correct |
85 |
Correct |
64 ms |
748 KB |
Output is correct |
86 |
Correct |
59 ms |
836 KB |
Output is correct |
87 |
Runtime error |
4 ms |
1748 KB |
Execution killed with signal 11 |
88 |
Halted |
0 ms |
0 KB |
- |