//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], tin[N], tout[N], eul[N], sz[N], t[N*4], l[N], r[N], szz;
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 go(int v=1, int cl=0){
if(c[v]){
if(cl==0)tupd(c[v], 1);
return;
}
int big, small;
if(sz[l[v]] < sz[r[v]]){
big = r[v];
small = l[v];
}
else{
big = l[v];
small = r[v];
}
go(small, 1);
go(big, 0);
int a=0, b=0;
for(int i=tin[small]; i <= tout[small]; i++){
a += get(eul[i], n);
b += get(1, eul[i]);
}
ans += min(a, b);
if(cl==0) {
for(int i=tin[small]; i <= tout[small]; i++){
tupd(eul[i], 1);
}
}
else{
for(int i=tin[big]; i <= tout[big]; i++){
tupd(eul[i], 0);
}
}
}
void rec(){
cin >> c[szz];
if(c[szz]){
tin[szz] = tout[szz] = ++timer;
eul[timer] = c[szz];
sz[szz] = 1;
}
else{
int cur = szz;
szz++;
l[cur] = szz;
rec();
szz++;
r[cur] = szz;
rec();
tin[cur] = tin[l[cur]];
tout[cur] = timer;
sz[cur] = sz[l[cur]] + sz[r[cur]];
}
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14812 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
14684 KB |
Output is correct |
2 |
Correct |
3 ms |
14684 KB |
Output is correct |
3 |
Correct |
3 ms |
14680 KB |
Output is correct |
4 |
Correct |
2 ms |
14680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
15452 KB |
Output is correct |
2 |
Correct |
7 ms |
14896 KB |
Output is correct |
3 |
Correct |
4 ms |
15452 KB |
Output is correct |
4 |
Correct |
3 ms |
15452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
16984 KB |
Output is correct |
2 |
Correct |
27 ms |
16988 KB |
Output is correct |
3 |
Correct |
70 ms |
21084 KB |
Output is correct |
4 |
Correct |
15 ms |
18300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
23204 KB |
Output is correct |
2 |
Correct |
54 ms |
26716 KB |
Output is correct |
3 |
Correct |
69 ms |
29520 KB |
Output is correct |
4 |
Correct |
61 ms |
28932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
40916 KB |
Output is correct |
2 |
Correct |
80 ms |
34384 KB |
Output is correct |
3 |
Correct |
119 ms |
30236 KB |
Output is correct |
4 |
Correct |
75 ms |
28800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
31924 KB |
Output is correct |
2 |
Correct |
113 ms |
35152 KB |
Output is correct |
3 |
Correct |
101 ms |
42832 KB |
Output is correct |
4 |
Correct |
102 ms |
42348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
438 ms |
44224 KB |
Output is correct |
2 |
Correct |
212 ms |
40532 KB |
Output is correct |
3 |
Correct |
208 ms |
41764 KB |
Output is correct |
4 |
Correct |
261 ms |
39764 KB |
Output is correct |
5 |
Correct |
490 ms |
36512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
230 ms |
41016 KB |
Output is correct |
2 |
Correct |
229 ms |
53068 KB |
Output is correct |
3 |
Correct |
266 ms |
47956 KB |
Output is correct |
4 |
Correct |
179 ms |
56612 KB |
Output is correct |
5 |
Correct |
596 ms |
36540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
258 ms |
38748 KB |
Output is correct |
2 |
Correct |
210 ms |
42324 KB |
Output is correct |
3 |
Correct |
470 ms |
36804 KB |
Output is correct |
4 |
Correct |
293 ms |
38736 KB |
Output is correct |
5 |
Correct |
191 ms |
56328 KB |
Output is correct |
6 |
Correct |
671 ms |
36528 KB |
Output is correct |