#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[2 * 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
37964 KB |
Output is correct |
2 |
Correct |
19 ms |
37980 KB |
Output is correct |
3 |
Correct |
20 ms |
38028 KB |
Output is correct |
4 |
Correct |
19 ms |
37824 KB |
Output is correct |
5 |
Correct |
20 ms |
37980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
37976 KB |
Output is correct |
2 |
Correct |
22 ms |
37980 KB |
Output is correct |
3 |
Correct |
26 ms |
37980 KB |
Output is correct |
4 |
Correct |
20 ms |
37980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
37980 KB |
Output is correct |
2 |
Correct |
22 ms |
38132 KB |
Output is correct |
3 |
Correct |
21 ms |
37980 KB |
Output is correct |
4 |
Correct |
28 ms |
37980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
326 ms |
39268 KB |
Output is correct |
2 |
Correct |
31 ms |
38480 KB |
Output is correct |
3 |
Correct |
394 ms |
38920 KB |
Output is correct |
4 |
Correct |
472 ms |
38992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1035 ms |
40880 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
207 ms |
43056 KB |
Output is correct |
2 |
Execution timed out |
1077 ms |
42600 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1061 ms |
54056 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1055 ms |
39764 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1068 ms |
56568 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1006 ms |
41924 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1053 ms |
41244 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |