#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]);
int m1 = 0, m2 = 0;
for(auto e: T[u2]){
int x = T[u1].order_of_key(e);
m1 += x;
m2 += sz(T[u1])-x;
}
ans += (ll)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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
39512 KB |
Output is correct |
2 |
Correct |
26 ms |
39504 KB |
Output is correct |
3 |
Correct |
28 ms |
39516 KB |
Output is correct |
4 |
Correct |
27 ms |
39536 KB |
Output is correct |
5 |
Correct |
26 ms |
39516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
39516 KB |
Output is correct |
2 |
Correct |
30 ms |
39540 KB |
Output is correct |
3 |
Correct |
27 ms |
39768 KB |
Output is correct |
4 |
Correct |
27 ms |
39772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
39512 KB |
Output is correct |
2 |
Correct |
26 ms |
39512 KB |
Output is correct |
3 |
Correct |
28 ms |
39640 KB |
Output is correct |
4 |
Correct |
28 ms |
39516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
40024 KB |
Output is correct |
2 |
Correct |
31 ms |
39764 KB |
Output is correct |
3 |
Correct |
29 ms |
40024 KB |
Output is correct |
4 |
Correct |
28 ms |
40284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
41304 KB |
Output is correct |
2 |
Correct |
39 ms |
40356 KB |
Output is correct |
3 |
Correct |
75 ms |
41812 KB |
Output is correct |
4 |
Correct |
37 ms |
41304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
43088 KB |
Output is correct |
2 |
Correct |
65 ms |
45384 KB |
Output is correct |
3 |
Correct |
75 ms |
47484 KB |
Output is correct |
4 |
Correct |
71 ms |
47496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
53828 KB |
Output is correct |
2 |
Correct |
85 ms |
51496 KB |
Output is correct |
3 |
Correct |
117 ms |
46676 KB |
Output is correct |
4 |
Correct |
82 ms |
45396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
122 ms |
46676 KB |
Output is correct |
2 |
Correct |
122 ms |
48468 KB |
Output is correct |
3 |
Correct |
105 ms |
55932 KB |
Output is correct |
4 |
Correct |
110 ms |
55988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
231 ms |
56912 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
189 ms |
53076 KB |
Output is correct |
2 |
Incorrect |
192 ms |
65388 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
223 ms |
56876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |