//gm --- akezhon
#include <bits/stdc++.h>
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define int long long
#define pb push_back
#define F first
#define S second
#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define tm (tl+tr)/2
#define TL v+v, tl, tm
#define TR v+v+1, tm+1, tr
#define DA l <= tl && tr <= r
#define NET r < tl || tr < l
#define double long double
using namespace std;
const int N=6e5+7;
const int inf=1e9;
int c[N], sz[N], t[N*4], l[N], r[N], szz, a, b;
int n, timer, ans;
void tupd(int pos, int val, int v=1, int tl=1, int tr=n){
if(tl == tr){
t[v] = val;
return;
}
if(pos <= tm)tupd(pos, val, TL);
else tupd(pos, val, TR);
t[v] = t[v+v] + t[v+v+1];
}
int get(int l, int r, int v=1, int tl=1, int tr=n){
if(NET)return 0;
if(DA)return t[v];
return get(l, r, TL) + get(l, r, TR);
}
void calc(int v){
if(c[v]){
a += get(1, c[v]-1);
b += get(c[v]+1, n);
}else{
calc(l[v]);
calc(r[v]);
}
}
void add(int v, int x){
if(c[v]){
tupd(c[v], x);
}else{
add(l[v], x);
add(r[v], x);
}
}
void go(int v=1, int cl=0){
if(c[v]){
if(cl==0)tupd(c[v], 1);
return;
}
if(sz[l[v]] > sz[r[v]]){
swap(l[v], r[v]);
}
go(l[v], 1);
go(r[v], 0);
a=0, b=0;
calc(l[v]);
ans += min(a, b);
if(cl)add(r[v], 0);
else add(l[v], 1);
}
void rec(){
cin >> c[szz];
if(c[szz]){
sz[szz] = 1;
}
else{
int v = szz;
l[v] = ++szz;
rec();
r[v] = ++szz;
rec();
sz[v] = sz[l[v]]+sz[r[v]];
}
}
void AlemAmenov(){
cin >> n;
szz=1;
rec();
go();
cout << ans;
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// freopen("promote.in", "r", stdin);
// freopen("promote.out", "w", stdout);
int RealName=1;
// cin >> RealName;
// int C=0;
while(RealName--){
// cout << "Case " << ++C << ":\n";
AlemAmenov();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8792 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8588 KB |
Output is correct |
5 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
8792 KB |
Output is correct |
2 |
Correct |
7 ms |
8540 KB |
Output is correct |
3 |
Correct |
3 ms |
8660 KB |
Output is correct |
4 |
Correct |
3 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
11368 KB |
Output is correct |
2 |
Correct |
25 ms |
12892 KB |
Output is correct |
3 |
Correct |
75 ms |
12892 KB |
Output is correct |
4 |
Correct |
15 ms |
13144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
130 ms |
14936 KB |
Output is correct |
2 |
Correct |
59 ms |
15708 KB |
Output is correct |
3 |
Correct |
69 ms |
16544 KB |
Output is correct |
4 |
Correct |
64 ms |
16212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
19548 KB |
Output is correct |
2 |
Correct |
83 ms |
17880 KB |
Output is correct |
3 |
Correct |
130 ms |
17964 KB |
Output is correct |
4 |
Correct |
83 ms |
17244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
137 ms |
19168 KB |
Output is correct |
2 |
Correct |
118 ms |
19992 KB |
Output is correct |
3 |
Correct |
102 ms |
21840 KB |
Output is correct |
4 |
Correct |
107 ms |
21844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
478 ms |
26348 KB |
Output is correct |
2 |
Correct |
246 ms |
26192 KB |
Output is correct |
3 |
Correct |
219 ms |
25436 KB |
Output is correct |
4 |
Correct |
285 ms |
24916 KB |
Output is correct |
5 |
Correct |
557 ms |
24404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
240 ms |
24916 KB |
Output is correct |
2 |
Correct |
243 ms |
29260 KB |
Output is correct |
3 |
Correct |
295 ms |
27984 KB |
Output is correct |
4 |
Correct |
194 ms |
28940 KB |
Output is correct |
5 |
Correct |
683 ms |
25180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
273 ms |
24656 KB |
Output is correct |
2 |
Correct |
224 ms |
25512 KB |
Output is correct |
3 |
Correct |
512 ms |
24148 KB |
Output is correct |
4 |
Correct |
322 ms |
24784 KB |
Output is correct |
5 |
Correct |
175 ms |
28868 KB |
Output is correct |
6 |
Correct |
731 ms |
24404 KB |
Output is correct |