#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 |
19032 KB |
Output is correct |
2 |
Correct |
10 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 |
19036 KB |
Output is correct |
2 |
Correct |
13 ms |
19036 KB |
Output is correct |
3 |
Correct |
13 ms |
19192 KB |
Output is correct |
4 |
Correct |
11 ms |
19032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
19340 KB |
Output is correct |
2 |
Correct |
12 ms |
19296 KB |
Output is correct |
3 |
Correct |
11 ms |
19292 KB |
Output is correct |
4 |
Correct |
22 ms |
19384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
314 ms |
20052 KB |
Output is correct |
2 |
Correct |
23 ms |
19744 KB |
Output is correct |
3 |
Correct |
348 ms |
20136 KB |
Output is correct |
4 |
Correct |
470 ms |
20756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1035 ms |
21840 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
24160 KB |
Output is correct |
2 |
Execution timed out |
1038 ms |
23540 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1076 ms |
34916 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1032 ms |
20880 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1049 ms |
36300 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1014 ms |
22748 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1043 ms |
22100 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |