#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define oo 1e9
const int MAX = 1e5 + 5;
int n;
int tree[MAX];
void init(){
for(int i = 0; i < MAX; i++) tree[i] = 0;
}
void update(int pos, int val){
for(int i = pos; i < MAX; i += (i & -i)) tree[i] += val;
}
int ask(int l, int r){
if(l != 1) return ask(1, r) - ask(1, l - 1);
int res = 0;
for(int i = r; i >= 1; i -= i & -i) res += tree[i];
return res;
}
vector<int> g[MAX];
int c[MAX];
int par[MAX];
void solve(){
cin >> n;
vector<pii> v;
for(int i = 1; i <= n; i++){
cin >> c[i];
v.push_back({c[i], i});
}
sort(v.begin(), v.end());
int t = 0;
for(int i = 0; i < v.size(); i++){
if(i == 0 || v[i].first != v[i - 1].first) t++;
c[v[i].second] = t;
}
par[1] = 1;
for(int i = 1; i < n; i++){
int u, v; cin >> u >> v;
par[v] = u;
int ans = 0;
while(1){
ans += ask(1, c[u] - 1);
update(c[u], 1);
c[u] = c[v];
if(u == 1) break;
u = par[u];
}
init();
cout << ans << '\n';
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
while(t--){
solve();
}
}
Compilation message
construction.cpp: In function 'void solve()':
construction.cpp:43:22: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
43 | for(int i = 0; i < v.size(); i++){
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
7 ms |
4952 KB |
Output is correct |
5 |
Correct |
9 ms |
4956 KB |
Output is correct |
6 |
Correct |
9 ms |
5012 KB |
Output is correct |
7 |
Correct |
10 ms |
4956 KB |
Output is correct |
8 |
Correct |
11 ms |
4952 KB |
Output is correct |
9 |
Correct |
10 ms |
4956 KB |
Output is correct |
10 |
Correct |
11 ms |
5008 KB |
Output is correct |
11 |
Correct |
9 ms |
4956 KB |
Output is correct |
12 |
Correct |
9 ms |
4956 KB |
Output is correct |
13 |
Correct |
9 ms |
5000 KB |
Output is correct |
14 |
Correct |
10 ms |
5016 KB |
Output is correct |
15 |
Correct |
9 ms |
4956 KB |
Output is correct |
16 |
Correct |
8 ms |
4956 KB |
Output is correct |
17 |
Correct |
8 ms |
4956 KB |
Output is correct |
18 |
Correct |
9 ms |
4956 KB |
Output is correct |
19 |
Correct |
9 ms |
5008 KB |
Output is correct |
20 |
Correct |
9 ms |
5008 KB |
Output is correct |
21 |
Correct |
10 ms |
4956 KB |
Output is correct |
22 |
Correct |
8 ms |
4956 KB |
Output is correct |
23 |
Correct |
9 ms |
4956 KB |
Output is correct |
24 |
Correct |
8 ms |
4956 KB |
Output is correct |
25 |
Correct |
8 ms |
5008 KB |
Output is correct |
26 |
Correct |
11 ms |
4828 KB |
Output is correct |
27 |
Correct |
9 ms |
4956 KB |
Output is correct |
28 |
Correct |
10 ms |
4952 KB |
Output is correct |
29 |
Correct |
11 ms |
5004 KB |
Output is correct |
30 |
Correct |
9 ms |
4956 KB |
Output is correct |
31 |
Correct |
8 ms |
4956 KB |
Output is correct |
32 |
Correct |
10 ms |
4956 KB |
Output is correct |
33 |
Correct |
9 ms |
4956 KB |
Output is correct |
34 |
Correct |
9 ms |
4956 KB |
Output is correct |
35 |
Correct |
9 ms |
4952 KB |
Output is correct |
36 |
Correct |
9 ms |
4956 KB |
Output is correct |
37 |
Correct |
9 ms |
4828 KB |
Output is correct |
38 |
Correct |
8 ms |
4956 KB |
Output is correct |
39 |
Correct |
9 ms |
5012 KB |
Output is correct |
40 |
Correct |
9 ms |
4956 KB |
Output is correct |
41 |
Correct |
8 ms |
5008 KB |
Output is correct |
42 |
Correct |
9 ms |
4956 KB |
Output is correct |
43 |
Correct |
9 ms |
4956 KB |
Output is correct |
44 |
Correct |
9 ms |
5004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
7 ms |
4952 KB |
Output is correct |
5 |
Correct |
9 ms |
4956 KB |
Output is correct |
6 |
Correct |
9 ms |
5012 KB |
Output is correct |
7 |
Correct |
10 ms |
4956 KB |
Output is correct |
8 |
Correct |
11 ms |
4952 KB |
Output is correct |
9 |
Correct |
10 ms |
4956 KB |
Output is correct |
10 |
Correct |
11 ms |
5008 KB |
Output is correct |
11 |
Correct |
9 ms |
4956 KB |
Output is correct |
12 |
Correct |
9 ms |
4956 KB |
Output is correct |
13 |
Correct |
9 ms |
5000 KB |
Output is correct |
14 |
Correct |
10 ms |
5016 KB |
Output is correct |
15 |
Correct |
9 ms |
4956 KB |
Output is correct |
16 |
Correct |
8 ms |
4956 KB |
Output is correct |
17 |
Correct |
8 ms |
4956 KB |
Output is correct |
18 |
Correct |
9 ms |
4956 KB |
Output is correct |
19 |
Correct |
9 ms |
5008 KB |
Output is correct |
20 |
Correct |
9 ms |
5008 KB |
Output is correct |
21 |
Correct |
10 ms |
4956 KB |
Output is correct |
22 |
Correct |
8 ms |
4956 KB |
Output is correct |
23 |
Correct |
9 ms |
4956 KB |
Output is correct |
24 |
Correct |
8 ms |
4956 KB |
Output is correct |
25 |
Correct |
8 ms |
5008 KB |
Output is correct |
26 |
Correct |
11 ms |
4828 KB |
Output is correct |
27 |
Correct |
9 ms |
4956 KB |
Output is correct |
28 |
Correct |
10 ms |
4952 KB |
Output is correct |
29 |
Correct |
11 ms |
5004 KB |
Output is correct |
30 |
Correct |
9 ms |
4956 KB |
Output is correct |
31 |
Correct |
8 ms |
4956 KB |
Output is correct |
32 |
Correct |
10 ms |
4956 KB |
Output is correct |
33 |
Correct |
9 ms |
4956 KB |
Output is correct |
34 |
Correct |
9 ms |
4956 KB |
Output is correct |
35 |
Correct |
9 ms |
4952 KB |
Output is correct |
36 |
Correct |
9 ms |
4956 KB |
Output is correct |
37 |
Correct |
9 ms |
4828 KB |
Output is correct |
38 |
Correct |
8 ms |
4956 KB |
Output is correct |
39 |
Correct |
9 ms |
5012 KB |
Output is correct |
40 |
Correct |
9 ms |
4956 KB |
Output is correct |
41 |
Correct |
8 ms |
5008 KB |
Output is correct |
42 |
Correct |
9 ms |
4956 KB |
Output is correct |
43 |
Correct |
9 ms |
4956 KB |
Output is correct |
44 |
Correct |
9 ms |
5004 KB |
Output is correct |
45 |
Correct |
16 ms |
4956 KB |
Output is correct |
46 |
Correct |
59 ms |
5212 KB |
Output is correct |
47 |
Correct |
61 ms |
5212 KB |
Output is correct |
48 |
Correct |
63 ms |
5208 KB |
Output is correct |
49 |
Correct |
206 ms |
5212 KB |
Output is correct |
50 |
Correct |
203 ms |
5212 KB |
Output is correct |
51 |
Correct |
201 ms |
5192 KB |
Output is correct |
52 |
Correct |
142 ms |
4952 KB |
Output is correct |
53 |
Correct |
142 ms |
5208 KB |
Output is correct |
54 |
Correct |
145 ms |
5208 KB |
Output is correct |
55 |
Correct |
143 ms |
5212 KB |
Output is correct |
56 |
Correct |
138 ms |
5208 KB |
Output is correct |
57 |
Correct |
67 ms |
4956 KB |
Output is correct |
58 |
Correct |
60 ms |
5212 KB |
Output is correct |
59 |
Correct |
60 ms |
5208 KB |
Output is correct |
60 |
Correct |
60 ms |
5208 KB |
Output is correct |
61 |
Correct |
144 ms |
5208 KB |
Output is correct |
62 |
Correct |
167 ms |
5212 KB |
Output is correct |
63 |
Correct |
144 ms |
5212 KB |
Output is correct |
64 |
Correct |
62 ms |
4956 KB |
Output is correct |
65 |
Correct |
60 ms |
4956 KB |
Output is correct |
66 |
Correct |
66 ms |
4952 KB |
Output is correct |
67 |
Correct |
59 ms |
4952 KB |
Output is correct |
68 |
Correct |
218 ms |
5172 KB |
Output is correct |
69 |
Correct |
142 ms |
5192 KB |
Output is correct |
70 |
Correct |
151 ms |
5208 KB |
Output is correct |
71 |
Correct |
146 ms |
4956 KB |
Output is correct |
72 |
Correct |
58 ms |
4952 KB |
Output is correct |
73 |
Correct |
61 ms |
4952 KB |
Output is correct |
74 |
Correct |
165 ms |
4952 KB |
Output is correct |
75 |
Correct |
108 ms |
4956 KB |
Output is correct |
76 |
Correct |
93 ms |
5184 KB |
Output is correct |
77 |
Correct |
89 ms |
4956 KB |
Output is correct |
78 |
Correct |
110 ms |
5164 KB |
Output is correct |
79 |
Correct |
104 ms |
4952 KB |
Output is correct |
80 |
Correct |
87 ms |
5192 KB |
Output is correct |
81 |
Correct |
109 ms |
4952 KB |
Output is correct |
82 |
Correct |
95 ms |
4956 KB |
Output is correct |
83 |
Correct |
89 ms |
4956 KB |
Output is correct |
84 |
Correct |
113 ms |
4956 KB |
Output is correct |
85 |
Correct |
95 ms |
4952 KB |
Output is correct |
86 |
Correct |
88 ms |
4956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4956 KB |
Output is correct |
2 |
Correct |
1 ms |
4956 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
7 ms |
4952 KB |
Output is correct |
5 |
Correct |
9 ms |
4956 KB |
Output is correct |
6 |
Correct |
9 ms |
5012 KB |
Output is correct |
7 |
Correct |
10 ms |
4956 KB |
Output is correct |
8 |
Correct |
11 ms |
4952 KB |
Output is correct |
9 |
Correct |
10 ms |
4956 KB |
Output is correct |
10 |
Correct |
11 ms |
5008 KB |
Output is correct |
11 |
Correct |
9 ms |
4956 KB |
Output is correct |
12 |
Correct |
9 ms |
4956 KB |
Output is correct |
13 |
Correct |
9 ms |
5000 KB |
Output is correct |
14 |
Correct |
10 ms |
5016 KB |
Output is correct |
15 |
Correct |
9 ms |
4956 KB |
Output is correct |
16 |
Correct |
8 ms |
4956 KB |
Output is correct |
17 |
Correct |
8 ms |
4956 KB |
Output is correct |
18 |
Correct |
9 ms |
4956 KB |
Output is correct |
19 |
Correct |
9 ms |
5008 KB |
Output is correct |
20 |
Correct |
9 ms |
5008 KB |
Output is correct |
21 |
Correct |
10 ms |
4956 KB |
Output is correct |
22 |
Correct |
8 ms |
4956 KB |
Output is correct |
23 |
Correct |
9 ms |
4956 KB |
Output is correct |
24 |
Correct |
8 ms |
4956 KB |
Output is correct |
25 |
Correct |
8 ms |
5008 KB |
Output is correct |
26 |
Correct |
11 ms |
4828 KB |
Output is correct |
27 |
Correct |
9 ms |
4956 KB |
Output is correct |
28 |
Correct |
10 ms |
4952 KB |
Output is correct |
29 |
Correct |
11 ms |
5004 KB |
Output is correct |
30 |
Correct |
9 ms |
4956 KB |
Output is correct |
31 |
Correct |
8 ms |
4956 KB |
Output is correct |
32 |
Correct |
10 ms |
4956 KB |
Output is correct |
33 |
Correct |
9 ms |
4956 KB |
Output is correct |
34 |
Correct |
9 ms |
4956 KB |
Output is correct |
35 |
Correct |
9 ms |
4952 KB |
Output is correct |
36 |
Correct |
9 ms |
4956 KB |
Output is correct |
37 |
Correct |
9 ms |
4828 KB |
Output is correct |
38 |
Correct |
8 ms |
4956 KB |
Output is correct |
39 |
Correct |
9 ms |
5012 KB |
Output is correct |
40 |
Correct |
9 ms |
4956 KB |
Output is correct |
41 |
Correct |
8 ms |
5008 KB |
Output is correct |
42 |
Correct |
9 ms |
4956 KB |
Output is correct |
43 |
Correct |
9 ms |
4956 KB |
Output is correct |
44 |
Correct |
9 ms |
5004 KB |
Output is correct |
45 |
Correct |
16 ms |
4956 KB |
Output is correct |
46 |
Correct |
59 ms |
5212 KB |
Output is correct |
47 |
Correct |
61 ms |
5212 KB |
Output is correct |
48 |
Correct |
63 ms |
5208 KB |
Output is correct |
49 |
Correct |
206 ms |
5212 KB |
Output is correct |
50 |
Correct |
203 ms |
5212 KB |
Output is correct |
51 |
Correct |
201 ms |
5192 KB |
Output is correct |
52 |
Correct |
142 ms |
4952 KB |
Output is correct |
53 |
Correct |
142 ms |
5208 KB |
Output is correct |
54 |
Correct |
145 ms |
5208 KB |
Output is correct |
55 |
Correct |
143 ms |
5212 KB |
Output is correct |
56 |
Correct |
138 ms |
5208 KB |
Output is correct |
57 |
Correct |
67 ms |
4956 KB |
Output is correct |
58 |
Correct |
60 ms |
5212 KB |
Output is correct |
59 |
Correct |
60 ms |
5208 KB |
Output is correct |
60 |
Correct |
60 ms |
5208 KB |
Output is correct |
61 |
Correct |
144 ms |
5208 KB |
Output is correct |
62 |
Correct |
167 ms |
5212 KB |
Output is correct |
63 |
Correct |
144 ms |
5212 KB |
Output is correct |
64 |
Correct |
62 ms |
4956 KB |
Output is correct |
65 |
Correct |
60 ms |
4956 KB |
Output is correct |
66 |
Correct |
66 ms |
4952 KB |
Output is correct |
67 |
Correct |
59 ms |
4952 KB |
Output is correct |
68 |
Correct |
218 ms |
5172 KB |
Output is correct |
69 |
Correct |
142 ms |
5192 KB |
Output is correct |
70 |
Correct |
151 ms |
5208 KB |
Output is correct |
71 |
Correct |
146 ms |
4956 KB |
Output is correct |
72 |
Correct |
58 ms |
4952 KB |
Output is correct |
73 |
Correct |
61 ms |
4952 KB |
Output is correct |
74 |
Correct |
165 ms |
4952 KB |
Output is correct |
75 |
Correct |
108 ms |
4956 KB |
Output is correct |
76 |
Correct |
93 ms |
5184 KB |
Output is correct |
77 |
Correct |
89 ms |
4956 KB |
Output is correct |
78 |
Correct |
110 ms |
5164 KB |
Output is correct |
79 |
Correct |
104 ms |
4952 KB |
Output is correct |
80 |
Correct |
87 ms |
5192 KB |
Output is correct |
81 |
Correct |
109 ms |
4952 KB |
Output is correct |
82 |
Correct |
95 ms |
4956 KB |
Output is correct |
83 |
Correct |
89 ms |
4956 KB |
Output is correct |
84 |
Correct |
113 ms |
4956 KB |
Output is correct |
85 |
Correct |
95 ms |
4952 KB |
Output is correct |
86 |
Correct |
88 ms |
4956 KB |
Output is correct |
87 |
Correct |
151 ms |
5340 KB |
Output is correct |
88 |
Correct |
431 ms |
6260 KB |
Output is correct |
89 |
Correct |
1496 ms |
9056 KB |
Output is correct |
90 |
Correct |
1427 ms |
9116 KB |
Output is correct |
91 |
Correct |
1441 ms |
9108 KB |
Output is correct |
92 |
Execution timed out |
2024 ms |
8132 KB |
Time limit exceeded |
93 |
Halted |
0 ms |
0 KB |
- |