#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define pb push_back
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T>
using index_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int maxn = 500001;
index_set<int> T[maxn];
ll ans = 0LL;
int cur = 1;
void dfs(int s){
int x; cin >> x;
if(x == 0){
int u1 = ++cur;
dfs(u1);
int u2 = ++cur;
dfs(u2);
if(sz(T[u1]) < sz(T[u2])) T[u1].swap(T[u2]);
ll m1 = 0LL, m2 = 0LL;
for(auto e: T[u2]){
int x = T[u1].order_of_key(e);
m1 += x;
m2 += sz(T[u1])-x;
}
ans += min(m1, m2);
for(auto e: T[u2]){
T[u1].insert(e);
}
T[u2].clear();
T[s].swap(T[u1]);
}
else{
T[s].insert(x);
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n; cin >> n;
dfs(1);
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
39504 KB |
Output is correct |
2 |
Correct |
25 ms |
39508 KB |
Output is correct |
3 |
Correct |
25 ms |
39516 KB |
Output is correct |
4 |
Correct |
25 ms |
39516 KB |
Output is correct |
5 |
Correct |
25 ms |
39428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
39512 KB |
Output is correct |
2 |
Correct |
25 ms |
39504 KB |
Output is correct |
3 |
Correct |
25 ms |
39444 KB |
Output is correct |
4 |
Correct |
26 ms |
39512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
39516 KB |
Output is correct |
2 |
Correct |
29 ms |
39516 KB |
Output is correct |
3 |
Correct |
26 ms |
39500 KB |
Output is correct |
4 |
Correct |
25 ms |
39516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
40144 KB |
Output is correct |
2 |
Correct |
34 ms |
39760 KB |
Output is correct |
3 |
Correct |
27 ms |
40016 KB |
Output is correct |
4 |
Correct |
27 ms |
40120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
41508 KB |
Output is correct |
2 |
Correct |
43 ms |
40276 KB |
Output is correct |
3 |
Correct |
69 ms |
41812 KB |
Output is correct |
4 |
Correct |
34 ms |
41300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
43092 KB |
Output is correct |
2 |
Correct |
66 ms |
45392 KB |
Output is correct |
3 |
Correct |
71 ms |
47380 KB |
Output is correct |
4 |
Correct |
65 ms |
47444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
53840 KB |
Output is correct |
2 |
Correct |
80 ms |
51284 KB |
Output is correct |
3 |
Correct |
114 ms |
46672 KB |
Output is correct |
4 |
Correct |
82 ms |
45256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
46484 KB |
Output is correct |
2 |
Correct |
129 ms |
47696 KB |
Output is correct |
3 |
Correct |
100 ms |
55124 KB |
Output is correct |
4 |
Correct |
102 ms |
55124 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
221 ms |
55392 KB |
Output is correct |
2 |
Correct |
194 ms |
51792 KB |
Output is correct |
3 |
Correct |
200 ms |
53844 KB |
Output is correct |
4 |
Correct |
191 ms |
52664 KB |
Output is correct |
5 |
Correct |
311 ms |
52304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
198 ms |
51404 KB |
Output is correct |
2 |
Correct |
185 ms |
63896 KB |
Output is correct |
3 |
Correct |
220 ms |
57680 KB |
Output is correct |
4 |
Correct |
154 ms |
65536 KB |
Output is correct |
5 |
Correct |
389 ms |
54748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
225 ms |
55120 KB |
Output is correct |
2 |
Correct |
200 ms |
53676 KB |
Output is correct |
3 |
Correct |
293 ms |
51800 KB |
Output is correct |
4 |
Correct |
232 ms |
56772 KB |
Output is correct |
5 |
Correct |
179 ms |
63060 KB |
Output is correct |
6 |
Correct |
298 ms |
55120 KB |
Output is correct |