#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ff first
#define ss second
#define all(a) (a.begin(), a.end())
const int mod = 1e9 + 7;
const int N = 1e5;
int n, c[N+10];
vector<int> g[N+10], group[N+10];
int shortest[N+10];
int par[N+10];
typedef tree<int, null_type, less_equal<int> ,rb_tree_tag, tree_order_statistics_node_update> ordered_set;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1;i <= n; i++){
cin >> c[i];
shortest[i] = -1;
}
int q = n-1;
shortest[1] = 0;
par[1] = -1;
group[1].push_back(1);
while(q--){
int a, b; cin >> a >> b;
int cost = 0;
ordered_set st;
for(int s : group[a]){
int it = st.order_of_key(c[s] + 1);
cost+= (st.size() - it);
st.insert(c[s]);
}
g[a].push_back(b);
for(int x : group[a]){
group[b].push_back(x);
}
group[b].push_back(b);
if(shortest[b] == -1 or shortest[b] > shortest[a]+1){
shortest[b] = shortest[a] + 1;
par[b] = a;
}
int parent = a;
while(parent != -1){
c[parent] = c[b];
parent = par[parent];
}
cout << cost << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
2 ms |
5724 KB |
Output is correct |
6 |
Correct |
2 ms |
5724 KB |
Output is correct |
7 |
Correct |
2 ms |
5724 KB |
Output is correct |
8 |
Correct |
14 ms |
6240 KB |
Output is correct |
9 |
Correct |
14 ms |
6236 KB |
Output is correct |
10 |
Correct |
14 ms |
6236 KB |
Output is correct |
11 |
Correct |
7 ms |
5980 KB |
Output is correct |
12 |
Correct |
7 ms |
5980 KB |
Output is correct |
13 |
Correct |
7 ms |
5976 KB |
Output is correct |
14 |
Correct |
7 ms |
5976 KB |
Output is correct |
15 |
Correct |
2 ms |
5720 KB |
Output is correct |
16 |
Correct |
2 ms |
5724 KB |
Output is correct |
17 |
Correct |
2 ms |
5724 KB |
Output is correct |
18 |
Correct |
2 ms |
5724 KB |
Output is correct |
19 |
Correct |
8 ms |
5980 KB |
Output is correct |
20 |
Correct |
7 ms |
5980 KB |
Output is correct |
21 |
Correct |
8 ms |
6036 KB |
Output is correct |
22 |
Correct |
2 ms |
5724 KB |
Output is correct |
23 |
Correct |
2 ms |
5724 KB |
Output is correct |
24 |
Correct |
2 ms |
5724 KB |
Output is correct |
25 |
Correct |
2 ms |
5724 KB |
Output is correct |
26 |
Correct |
14 ms |
6236 KB |
Output is correct |
27 |
Correct |
7 ms |
5980 KB |
Output is correct |
28 |
Correct |
7 ms |
5980 KB |
Output is correct |
29 |
Correct |
7 ms |
5980 KB |
Output is correct |
30 |
Correct |
2 ms |
5724 KB |
Output is correct |
31 |
Correct |
3 ms |
5724 KB |
Output is correct |
32 |
Correct |
8 ms |
6016 KB |
Output is correct |
33 |
Correct |
4 ms |
5960 KB |
Output is correct |
34 |
Correct |
3 ms |
5740 KB |
Output is correct |
35 |
Correct |
3 ms |
5724 KB |
Output is correct |
36 |
Correct |
4 ms |
5724 KB |
Output is correct |
37 |
Correct |
3 ms |
5724 KB |
Output is correct |
38 |
Correct |
3 ms |
5724 KB |
Output is correct |
39 |
Correct |
4 ms |
5724 KB |
Output is correct |
40 |
Correct |
4 ms |
5724 KB |
Output is correct |
41 |
Correct |
3 ms |
5724 KB |
Output is correct |
42 |
Correct |
4 ms |
5724 KB |
Output is correct |
43 |
Correct |
3 ms |
5720 KB |
Output is correct |
44 |
Correct |
3 ms |
5720 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
2 ms |
5724 KB |
Output is correct |
6 |
Correct |
2 ms |
5724 KB |
Output is correct |
7 |
Correct |
2 ms |
5724 KB |
Output is correct |
8 |
Correct |
14 ms |
6240 KB |
Output is correct |
9 |
Correct |
14 ms |
6236 KB |
Output is correct |
10 |
Correct |
14 ms |
6236 KB |
Output is correct |
11 |
Correct |
7 ms |
5980 KB |
Output is correct |
12 |
Correct |
7 ms |
5980 KB |
Output is correct |
13 |
Correct |
7 ms |
5976 KB |
Output is correct |
14 |
Correct |
7 ms |
5976 KB |
Output is correct |
15 |
Correct |
2 ms |
5720 KB |
Output is correct |
16 |
Correct |
2 ms |
5724 KB |
Output is correct |
17 |
Correct |
2 ms |
5724 KB |
Output is correct |
18 |
Correct |
2 ms |
5724 KB |
Output is correct |
19 |
Correct |
8 ms |
5980 KB |
Output is correct |
20 |
Correct |
7 ms |
5980 KB |
Output is correct |
21 |
Correct |
8 ms |
6036 KB |
Output is correct |
22 |
Correct |
2 ms |
5724 KB |
Output is correct |
23 |
Correct |
2 ms |
5724 KB |
Output is correct |
24 |
Correct |
2 ms |
5724 KB |
Output is correct |
25 |
Correct |
2 ms |
5724 KB |
Output is correct |
26 |
Correct |
14 ms |
6236 KB |
Output is correct |
27 |
Correct |
7 ms |
5980 KB |
Output is correct |
28 |
Correct |
7 ms |
5980 KB |
Output is correct |
29 |
Correct |
7 ms |
5980 KB |
Output is correct |
30 |
Correct |
2 ms |
5724 KB |
Output is correct |
31 |
Correct |
3 ms |
5724 KB |
Output is correct |
32 |
Correct |
8 ms |
6016 KB |
Output is correct |
33 |
Correct |
4 ms |
5960 KB |
Output is correct |
34 |
Correct |
3 ms |
5740 KB |
Output is correct |
35 |
Correct |
3 ms |
5724 KB |
Output is correct |
36 |
Correct |
4 ms |
5724 KB |
Output is correct |
37 |
Correct |
3 ms |
5724 KB |
Output is correct |
38 |
Correct |
3 ms |
5724 KB |
Output is correct |
39 |
Correct |
4 ms |
5724 KB |
Output is correct |
40 |
Correct |
4 ms |
5724 KB |
Output is correct |
41 |
Correct |
3 ms |
5724 KB |
Output is correct |
42 |
Correct |
4 ms |
5724 KB |
Output is correct |
43 |
Correct |
3 ms |
5720 KB |
Output is correct |
44 |
Correct |
3 ms |
5720 KB |
Output is correct |
45 |
Correct |
2 ms |
5724 KB |
Output is correct |
46 |
Correct |
5 ms |
5980 KB |
Output is correct |
47 |
Correct |
6 ms |
5980 KB |
Output is correct |
48 |
Correct |
5 ms |
5980 KB |
Output is correct |
49 |
Correct |
1134 ms |
46116 KB |
Output is correct |
50 |
Correct |
1169 ms |
46256 KB |
Output is correct |
51 |
Correct |
1129 ms |
46524 KB |
Output is correct |
52 |
Correct |
498 ms |
27060 KB |
Output is correct |
53 |
Correct |
516 ms |
27300 KB |
Output is correct |
54 |
Correct |
503 ms |
27168 KB |
Output is correct |
55 |
Correct |
511 ms |
27216 KB |
Output is correct |
56 |
Correct |
539 ms |
27072 KB |
Output is correct |
57 |
Correct |
6 ms |
5980 KB |
Output is correct |
58 |
Correct |
7 ms |
6232 KB |
Output is correct |
59 |
Correct |
6 ms |
6236 KB |
Output is correct |
60 |
Correct |
6 ms |
6244 KB |
Output is correct |
61 |
Correct |
541 ms |
27192 KB |
Output is correct |
62 |
Correct |
549 ms |
27368 KB |
Output is correct |
63 |
Correct |
531 ms |
27216 KB |
Output is correct |
64 |
Correct |
5 ms |
5980 KB |
Output is correct |
65 |
Correct |
6 ms |
6028 KB |
Output is correct |
66 |
Correct |
6 ms |
5980 KB |
Output is correct |
67 |
Correct |
5 ms |
5984 KB |
Output is correct |
68 |
Correct |
1131 ms |
46372 KB |
Output is correct |
69 |
Correct |
520 ms |
27028 KB |
Output is correct |
70 |
Correct |
502 ms |
26980 KB |
Output is correct |
71 |
Correct |
504 ms |
27020 KB |
Output is correct |
72 |
Correct |
5 ms |
6236 KB |
Output is correct |
73 |
Correct |
6 ms |
6188 KB |
Output is correct |
74 |
Correct |
529 ms |
27072 KB |
Output is correct |
75 |
Correct |
225 ms |
16540 KB |
Output is correct |
76 |
Correct |
143 ms |
13684 KB |
Output is correct |
77 |
Correct |
111 ms |
11148 KB |
Output is correct |
78 |
Correct |
232 ms |
16384 KB |
Output is correct |
79 |
Correct |
146 ms |
13624 KB |
Output is correct |
80 |
Correct |
104 ms |
11160 KB |
Output is correct |
81 |
Correct |
244 ms |
16596 KB |
Output is correct |
82 |
Correct |
155 ms |
13648 KB |
Output is correct |
83 |
Correct |
115 ms |
11288 KB |
Output is correct |
84 |
Correct |
251 ms |
16412 KB |
Output is correct |
85 |
Correct |
157 ms |
13652 KB |
Output is correct |
86 |
Correct |
111 ms |
11116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Correct |
2 ms |
5724 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
2 ms |
5724 KB |
Output is correct |
6 |
Correct |
2 ms |
5724 KB |
Output is correct |
7 |
Correct |
2 ms |
5724 KB |
Output is correct |
8 |
Correct |
14 ms |
6240 KB |
Output is correct |
9 |
Correct |
14 ms |
6236 KB |
Output is correct |
10 |
Correct |
14 ms |
6236 KB |
Output is correct |
11 |
Correct |
7 ms |
5980 KB |
Output is correct |
12 |
Correct |
7 ms |
5980 KB |
Output is correct |
13 |
Correct |
7 ms |
5976 KB |
Output is correct |
14 |
Correct |
7 ms |
5976 KB |
Output is correct |
15 |
Correct |
2 ms |
5720 KB |
Output is correct |
16 |
Correct |
2 ms |
5724 KB |
Output is correct |
17 |
Correct |
2 ms |
5724 KB |
Output is correct |
18 |
Correct |
2 ms |
5724 KB |
Output is correct |
19 |
Correct |
8 ms |
5980 KB |
Output is correct |
20 |
Correct |
7 ms |
5980 KB |
Output is correct |
21 |
Correct |
8 ms |
6036 KB |
Output is correct |
22 |
Correct |
2 ms |
5724 KB |
Output is correct |
23 |
Correct |
2 ms |
5724 KB |
Output is correct |
24 |
Correct |
2 ms |
5724 KB |
Output is correct |
25 |
Correct |
2 ms |
5724 KB |
Output is correct |
26 |
Correct |
14 ms |
6236 KB |
Output is correct |
27 |
Correct |
7 ms |
5980 KB |
Output is correct |
28 |
Correct |
7 ms |
5980 KB |
Output is correct |
29 |
Correct |
7 ms |
5980 KB |
Output is correct |
30 |
Correct |
2 ms |
5724 KB |
Output is correct |
31 |
Correct |
3 ms |
5724 KB |
Output is correct |
32 |
Correct |
8 ms |
6016 KB |
Output is correct |
33 |
Correct |
4 ms |
5960 KB |
Output is correct |
34 |
Correct |
3 ms |
5740 KB |
Output is correct |
35 |
Correct |
3 ms |
5724 KB |
Output is correct |
36 |
Correct |
4 ms |
5724 KB |
Output is correct |
37 |
Correct |
3 ms |
5724 KB |
Output is correct |
38 |
Correct |
3 ms |
5724 KB |
Output is correct |
39 |
Correct |
4 ms |
5724 KB |
Output is correct |
40 |
Correct |
4 ms |
5724 KB |
Output is correct |
41 |
Correct |
3 ms |
5724 KB |
Output is correct |
42 |
Correct |
4 ms |
5724 KB |
Output is correct |
43 |
Correct |
3 ms |
5720 KB |
Output is correct |
44 |
Correct |
3 ms |
5720 KB |
Output is correct |
45 |
Correct |
2 ms |
5724 KB |
Output is correct |
46 |
Correct |
5 ms |
5980 KB |
Output is correct |
47 |
Correct |
6 ms |
5980 KB |
Output is correct |
48 |
Correct |
5 ms |
5980 KB |
Output is correct |
49 |
Correct |
1134 ms |
46116 KB |
Output is correct |
50 |
Correct |
1169 ms |
46256 KB |
Output is correct |
51 |
Correct |
1129 ms |
46524 KB |
Output is correct |
52 |
Correct |
498 ms |
27060 KB |
Output is correct |
53 |
Correct |
516 ms |
27300 KB |
Output is correct |
54 |
Correct |
503 ms |
27168 KB |
Output is correct |
55 |
Correct |
511 ms |
27216 KB |
Output is correct |
56 |
Correct |
539 ms |
27072 KB |
Output is correct |
57 |
Correct |
6 ms |
5980 KB |
Output is correct |
58 |
Correct |
7 ms |
6232 KB |
Output is correct |
59 |
Correct |
6 ms |
6236 KB |
Output is correct |
60 |
Correct |
6 ms |
6244 KB |
Output is correct |
61 |
Correct |
541 ms |
27192 KB |
Output is correct |
62 |
Correct |
549 ms |
27368 KB |
Output is correct |
63 |
Correct |
531 ms |
27216 KB |
Output is correct |
64 |
Correct |
5 ms |
5980 KB |
Output is correct |
65 |
Correct |
6 ms |
6028 KB |
Output is correct |
66 |
Correct |
6 ms |
5980 KB |
Output is correct |
67 |
Correct |
5 ms |
5984 KB |
Output is correct |
68 |
Correct |
1131 ms |
46372 KB |
Output is correct |
69 |
Correct |
520 ms |
27028 KB |
Output is correct |
70 |
Correct |
502 ms |
26980 KB |
Output is correct |
71 |
Correct |
504 ms |
27020 KB |
Output is correct |
72 |
Correct |
5 ms |
6236 KB |
Output is correct |
73 |
Correct |
6 ms |
6188 KB |
Output is correct |
74 |
Correct |
529 ms |
27072 KB |
Output is correct |
75 |
Correct |
225 ms |
16540 KB |
Output is correct |
76 |
Correct |
143 ms |
13684 KB |
Output is correct |
77 |
Correct |
111 ms |
11148 KB |
Output is correct |
78 |
Correct |
232 ms |
16384 KB |
Output is correct |
79 |
Correct |
146 ms |
13624 KB |
Output is correct |
80 |
Correct |
104 ms |
11160 KB |
Output is correct |
81 |
Correct |
244 ms |
16596 KB |
Output is correct |
82 |
Correct |
155 ms |
13648 KB |
Output is correct |
83 |
Correct |
115 ms |
11288 KB |
Output is correct |
84 |
Correct |
251 ms |
16412 KB |
Output is correct |
85 |
Correct |
157 ms |
13652 KB |
Output is correct |
86 |
Correct |
111 ms |
11116 KB |
Output is correct |
87 |
Correct |
12 ms |
6748 KB |
Output is correct |
88 |
Correct |
36 ms |
9136 KB |
Output is correct |
89 |
Correct |
144 ms |
18408 KB |
Output is correct |
90 |
Correct |
141 ms |
17812 KB |
Output is correct |
91 |
Correct |
187 ms |
18704 KB |
Output is correct |
92 |
Execution timed out |
2104 ms |
73172 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |