#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
//#define p push
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
using namespace std;
const int mod=9901,mxn=4e5+5;
int tin[mxn+10],tout[mxn+10],l[mxn+10],r[mxn+10],sz[mxn+10],pos[mxn+10],fwk[mxn+10],dp[mxn+10];
int cnt=0,n,node,t=0,sum;
void upd(int p,int val){for(int i=p;i<=n;i+=(i&-i))fwk[i]+=val;}
int qry(int p){
sum=0;
for(int i=p;i>0;i-=(i&-i))sum+=fwk[i];
return sum;
}
int input(){
int cur;cin>>cur;
if(!cur){
cur=++cnt;
l[cur]=input();
r[cur]=input();
tin[cur]=tin[l[cur]];
tout[cur]=tout[r[cur]];
sz[cur]=sz[l[cur]]+sz[r[cur]];
}
else{
tin[cur]=tout[cur]=++t;
pos[t]=cur;
sz[cur]++;
}
return cur;
}
void solve(int cur,int keep){
if(cur<=n){
if(keep)upd(pos[tin[cur]],1);
return;
}
if(sz[l[cur]]<sz[r[cur]])swap(l[cur],r[cur]);//left keep
solve(r[cur],0);
solve(l[cur],1);
int invcnt=0;
for(int i=tin[r[cur]];i<=tout[r[cur]];i++)invcnt+=qry(pos[i]);
dp[cur]=dp[l[cur]]+dp[r[cur]]+min(invcnt,(sz[l[cur]]*sz[r[cur]])-invcnt);
if(keep)for(int i=tin[r[cur]];i<=tout[r[cur]];i++)upd(pos[i],1);
else for(int i=tin[l[cur]];i<=tout[l[cur]];i++)upd(pos[i],-1);
}
int32_t main(){
fastio
cin>>n;
cnt=n;
input();
solve(n+1,1);
cout<<dp[n+1];
}
Compilation message
rot.cpp:30:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
30 | #pragma GCC optimize ("03,unroll-lopps")
| ^
rot.cpp:35:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
35 | void upd(int p,int val){for(int i=p;i<=n;i+=(i&-i))fwk[i]+=val;}
| ^
rot.cpp:36:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
36 | int qry(int p){
| ^
rot.cpp:41:11: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
41 | int input(){
| ^
rot.cpp:58:28: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
58 | void solve(int cur,int keep){
| ^
rot.cpp:72:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
72 | int32_t main(){
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 ms |
12892 KB |
Output is correct |
3 |
Correct |
1 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 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 |
2 ms |
12888 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 |
12764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
12892 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
3 ms |
12892 KB |
Output is correct |
4 |
Correct |
3 ms |
12892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
13660 KB |
Output is correct |
2 |
Correct |
5 ms |
12888 KB |
Output is correct |
3 |
Correct |
12 ms |
12636 KB |
Output is correct |
4 |
Correct |
5 ms |
13404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
12636 KB |
Output is correct |
2 |
Correct |
13 ms |
14208 KB |
Output is correct |
3 |
Correct |
16 ms |
15192 KB |
Output is correct |
4 |
Correct |
15 ms |
15196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
18968 KB |
Output is correct |
2 |
Correct |
24 ms |
16220 KB |
Output is correct |
3 |
Correct |
25 ms |
14292 KB |
Output is correct |
4 |
Correct |
20 ms |
13320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
30 ms |
12892 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
69 ms |
16996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
52 ms |
15332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
15168 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |