#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=2e5+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(){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4696 KB |
Output is correct |
3 |
Correct |
1 ms |
4700 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5724 KB |
Output is correct |
2 |
Correct |
5 ms |
4832 KB |
Output is correct |
3 |
Correct |
11 ms |
5164 KB |
Output is correct |
4 |
Correct |
4 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
5724 KB |
Output is correct |
2 |
Correct |
12 ms |
7004 KB |
Output is correct |
3 |
Correct |
16 ms |
8264 KB |
Output is correct |
4 |
Correct |
15 ms |
8284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
12892 KB |
Output is correct |
2 |
Correct |
21 ms |
10076 KB |
Output is correct |
3 |
Correct |
25 ms |
8276 KB |
Output is correct |
4 |
Correct |
20 ms |
7256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
16 ms |
13660 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
10 ms |
12636 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
8 ms |
13656 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
8 ms |
13276 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |