//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 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=5e6+7;
const int inf=1e9;
int c[N], tin[N], tout[N], eul[N], sz[N], t[N*4], l[N], r[N], szz;
vector <pair<int*, int>> rb;
int n, timer, ans;
void roll(int &x){
rb.push_back({&x, x});
}
void tupd(int pos, int v=1, int tl=1, int tr=n){
roll(t[v]);
if(tl == tr){
t[v]++;
return;
}
if(pos <= tm)tupd(pos, TL);
else tupd(pos, 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]);
return;
}
int temp = rb.size();
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 cntkeep=0, cntswap=0;
for(int i=tin[small]; i <= tout[small]; i++){
if(big == r[v]){
cntkeep += get(eul[i], n);
cntswap += get(1, eul[i]);
}
else {
cntkeep += get(1, eul[i]);
cntswap += get(eul[i], n);
}
}
ans += min(cntkeep, cntswap);
if(cl==0) {
for(int i=tin[small]; i <= tout[small]; i++){
tupd(eul[i]);
}
}
else{
while(temp < rb.size()){
*(rb.back().F) = rb.back().S;
rb.pop_back();
}
}
}
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;
}
Compilation message
rot.cpp: In function 'void go(int, int)':
rot.cpp:75:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int*, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | while(temp < rb.size()){
| ~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12756 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
1 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
12892 KB |
Output is correct |
2 |
Correct |
3 ms |
13148 KB |
Output is correct |
3 |
Correct |
3 ms |
13148 KB |
Output is correct |
4 |
Correct |
2 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
14300 KB |
Output is correct |
2 |
Correct |
8 ms |
13828 KB |
Output is correct |
3 |
Correct |
5 ms |
14372 KB |
Output is correct |
4 |
Correct |
5 ms |
14296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
18640 KB |
Output is correct |
2 |
Correct |
29 ms |
18900 KB |
Output is correct |
3 |
Correct |
87 ms |
23896 KB |
Output is correct |
4 |
Correct |
21 ms |
19408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
150 ms |
33724 KB |
Output is correct |
2 |
Correct |
77 ms |
35256 KB |
Output is correct |
3 |
Correct |
86 ms |
36128 KB |
Output is correct |
4 |
Correct |
85 ms |
36280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
61620 KB |
Output is correct |
2 |
Correct |
112 ms |
56240 KB |
Output is correct |
3 |
Correct |
157 ms |
54784 KB |
Output is correct |
4 |
Correct |
114 ms |
53756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
177 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
520 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
269 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
342 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |