#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 f first
#define s second
#define int long long
const int N = 2e5 + 69;
int n, t = 1, id, ans;
typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
ordered_set;
ordered_set st[N];
int napravi() {
int x;
cin >> x;
if (x == 0) {
int l = napravi();
int r = napravi();
if (st[l].size() < st[r].size())
st[l].swap(st[r]);
int k1 = 0, k2 = 0;
for (auto u : st[r]) {
int x = st[l].order_of_key(u + 1);
k1 += (int)st[l].size() - x;
}
//b a
for (auto u : st[r]) {
int x = st[l].order_of_key(u + 1);
k2 += x;
}
for (auto u : st[r])
st[l].insert(u);
st[r].clear();
ans += min(k1, k2);
return l;
}
else {
st[x].insert(x);
return x;
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("snowcow.in", "r", stdin);
//freopen("snowcow.out", "w", stdout);
int tt = 1;
//cin >> tt;
while (tt--) {
cin >> n;
napravi();
cout << ans;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19036 KB |
Output is correct |
2 |
Correct |
11 ms |
19036 KB |
Output is correct |
3 |
Correct |
11 ms |
19036 KB |
Output is correct |
4 |
Correct |
11 ms |
19036 KB |
Output is correct |
5 |
Correct |
12 ms |
19036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19248 KB |
Output is correct |
2 |
Correct |
10 ms |
19036 KB |
Output is correct |
3 |
Correct |
10 ms |
19036 KB |
Output is correct |
4 |
Correct |
13 ms |
19036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19292 KB |
Output is correct |
2 |
Correct |
11 ms |
19292 KB |
Output is correct |
3 |
Correct |
12 ms |
19292 KB |
Output is correct |
4 |
Correct |
13 ms |
19324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
20160 KB |
Output is correct |
2 |
Correct |
15 ms |
19544 KB |
Output is correct |
3 |
Correct |
14 ms |
19928 KB |
Output is correct |
4 |
Correct |
13 ms |
19944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
21592 KB |
Output is correct |
2 |
Correct |
27 ms |
20032 KB |
Output is correct |
3 |
Correct |
77 ms |
21936 KB |
Output is correct |
4 |
Correct |
22 ms |
21340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
23636 KB |
Output is correct |
2 |
Correct |
60 ms |
26376 KB |
Output is correct |
3 |
Correct |
79 ms |
29008 KB |
Output is correct |
4 |
Correct |
66 ms |
29264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
36688 KB |
Output is correct |
2 |
Correct |
87 ms |
34340 KB |
Output is correct |
3 |
Correct |
124 ms |
28496 KB |
Output is correct |
4 |
Correct |
94 ms |
26780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
27288 KB |
Output is correct |
2 |
Correct |
150 ms |
30800 KB |
Output is correct |
3 |
Correct |
123 ms |
40272 KB |
Output is correct |
4 |
Correct |
132 ms |
40368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
325 ms |
40016 KB |
Output is correct |
2 |
Correct |
213 ms |
34808 KB |
Output is correct |
3 |
Correct |
255 ms |
37460 KB |
Output is correct |
4 |
Correct |
251 ms |
35924 KB |
Output is correct |
5 |
Correct |
427 ms |
35752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
273 ms |
36356 KB |
Output is correct |
2 |
Correct |
238 ms |
52336 KB |
Output is correct |
3 |
Correct |
315 ms |
42256 KB |
Output is correct |
4 |
Correct |
226 ms |
54052 KB |
Output is correct |
5 |
Correct |
545 ms |
38924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
335 ms |
41368 KB |
Output is correct |
2 |
Correct |
276 ms |
37280 KB |
Output is correct |
3 |
Correct |
409 ms |
35116 KB |
Output is correct |
4 |
Correct |
322 ms |
41556 KB |
Output is correct |
5 |
Correct |
254 ms |
49176 KB |
Output is correct |
6 |
Correct |
496 ms |
39508 KB |
Output is correct |