//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=2e5+7;
const int M=1e9+7;
const int NN=2e5+7;
const int inf=1e9;
int c[N], tin[N], tout[N], eul[N], sz[N], big[N], t[N*2], l[N], r[N], szz;
vector <pair<int*, int>> rb;
int n, timer, cur, 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]=1;
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 dfs(int v=1){
tin[v] = ++timer;
eul[timer] = v;
sz[v] = 1;
if(!c[v]){
dfs(l[v]);
dfs(r[v]);
sz[v] += sz[l[v]];
sz[v] += sz[r[v]];
if(sz[r[v]] > sz[l[v]])big[v] = r[v];
else big[v] = l[v];
}
tout[v] = timer;
}
void go(int v=1, int cl=0){
if(c[v]){
if(!cl)tupd(c[v]);
return;
}
int temp = rb.size(), ok=0, cntkeep=0, cntswap=0, x = l[v];
if(big[v] == l[v]){
x = r[v];
ok = 1;
}
go(x, 1);
go(big[v], 0);
for(int i=tin[x]; i <= tout[x]; i++){
int f = eul[i];
if(c[f]){
if(ok){
cntkeep += get(c[f], n);
cntswap += get(1, c[f]);
}
else {
cntkeep += get(1, c[f]);
cntswap += get(c[f], n);
}
}
}
ans += min(cntkeep, cntswap);
if(cl){
while(temp < rb.size()){
*(rb.back().F) = rb.back().S;
rb.pop_back();
}
}
else{
for(int i=tin[x]; i <= tout[x]; i++){
int f = eul[i];
if(c[f])tupd(c[f]);
}
}
}
void rec(){
cin >> c[szz];
if(!c[szz]){
int cur = szz;
szz++;
l[cur] = szz;
rec();
szz++;
r[cur] = szz;
rec();
}
}
void AlemAmenov(){
cin >> n;
szz=1;
rec();
dfs();
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:86: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]
86 | while(temp < rb.size()){
| ~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6612 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6748 KB |
Output is correct |
2 |
Correct |
2 ms |
7004 KB |
Output is correct |
3 |
Correct |
2 ms |
7004 KB |
Output is correct |
4 |
Correct |
2 ms |
7004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8156 KB |
Output is correct |
2 |
Correct |
8 ms |
7644 KB |
Output is correct |
3 |
Correct |
4 ms |
8156 KB |
Output is correct |
4 |
Correct |
4 ms |
8156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12756 KB |
Output is correct |
2 |
Correct |
31 ms |
11212 KB |
Output is correct |
3 |
Correct |
88 ms |
15312 KB |
Output is correct |
4 |
Correct |
19 ms |
11984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
151 ms |
25308 KB |
Output is correct |
2 |
Correct |
80 ms |
26760 KB |
Output is correct |
3 |
Correct |
92 ms |
29372 KB |
Output is correct |
4 |
Correct |
87 ms |
27664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
50360 KB |
Output is correct |
2 |
Correct |
111 ms |
47372 KB |
Output is correct |
3 |
Correct |
160 ms |
44988 KB |
Output is correct |
4 |
Correct |
113 ms |
42088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
14 ms |
7512 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
14 ms |
7516 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
17 ms |
8584 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
20 ms |
8028 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |