#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, 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]);
swap(l, 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 |
10 ms |
19036 KB |
Output is correct |
2 |
Correct |
10 ms |
19036 KB |
Output is correct |
3 |
Correct |
10 ms |
19036 KB |
Output is correct |
4 |
Correct |
10 ms |
19036 KB |
Output is correct |
5 |
Correct |
10 ms |
19036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19036 KB |
Output is correct |
2 |
Correct |
11 ms |
19108 KB |
Output is correct |
3 |
Correct |
10 ms |
19292 KB |
Output is correct |
4 |
Correct |
13 ms |
19292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
19548 KB |
Output is correct |
2 |
Correct |
16 ms |
19636 KB |
Output is correct |
3 |
Correct |
12 ms |
19548 KB |
Output is correct |
4 |
Correct |
22 ms |
23900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
118 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
114 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
176 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
109 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
115 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
173 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
111 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
111 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |