#include <bits/stdc++.h>
using namespace std;
// #pragma comment(linker, "/stack:2000000000")
// #pragma GCC optimize("Ofast,unroll-loops,fast-math,O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
#define precise(a) cout<<fixed<<setprecision(a)
#define sz size()
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
// t1
const ll mod = ll(1e9)+7; //(b + (a%b)) % b (to mod -1%(10^9+7) correctly in c++ its -1 but its suppose to be 10^9+6
const ll MOD = 998244353; // (a%mod)*(binpow(b,mod-2,mod) = (a/b)%mod
const ll N = ll(4e5)+10;
const ll inf = 1e18;
const ld eps = 1e-15L;
const ll B = 400;
// const ld pie = acos(-1.0);
ll lcm(ll a, ll b){ return (a / __gcd(a,b))*b; }
ll binpow(ll a, ll b, ll m){ ll res=1;a%=m; while(b>0){ if(b&1)res=(res*a)%m; a=(a*a)%m; b/=2; } return res%m;}
void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
int n, lc[N], rc[N], node, siz[N], fenwin[N];
ll ans[N];
vector<int> val[N];
int read() {
++node;
int root = node;
siz[root] = 1;
int v; cin>>v;
if(v == 0) {
lc[root] = read();
rc[root] = read();
siz[root] = siz[lc[root]] + siz[rc[root]];
return root;
}
else {
val[root].pb(v);
lc[root] = rc[root] = -1;
return root;
}
}
void update(int p, int x) {
while(p <= n) {
fenwin[p] += x;
p += (p&-p);
}
}
int got(int x) {
int res = 0;
while(x >= 1) {
res += fenwin[x];
x -= (x & -x);
}
return res;
}
int get(int l, int r) {
return got(r) - got(l - 1);
}
void dfs(int v, bool clear) {
if(lc[v] == -1) {
if(!clear) {
for(auto x : val[v]) update(x, 1);
}
return;
}
if(siz[lc[v]] < siz[rc[v]]) {
dfs(lc[v], 1);
dfs(rc[v], 0);
// light ... heavy
ll ex = 0;
for(auto x : val[lc[v]]) ex += get(1, x-1);
ll ans1 = ans[lc[v]] + ans[rc[v]] + ex;
ex = 0;
// heavy light
for(auto x : val[lc[v]]) ex += get(x+1, n);
ll ans2 = ans[rc[v]] + ans[lc[v]] + ex;
if(clear) {
for(auto x : val[rc[v]]) update(x, -1);
}
if(ans1 < ans2) {
ans[v] = ans1;
}
else {
ans[v] = ans2;
}
val[v].swap(val[rc[v]]);
for(auto x : val[lc[v]]) val[v].pb(x);
if(!clear) for(auto x : val[lc[v]]) update( x, 1);
}
else {
dfs(rc[v], 1);
dfs(lc[v], 0);
// light ... heavy
ll ex = 0;
for(auto x : val[rc[v]]) ex += get(1, x-1);
ll ans1 = ans[rc[v]] + ans[lc[v]] + ex;
ex = 0;
// heavy light
for(auto x : val[rc[v]]) ex += get(x+1, n);
ll ans2 = ans[lc[v]] + ans[rc[v]] + ex;
// if(v == 17) {
// cout<<"HEREol\n";
// cout<<ans1<<" "<<ans2<<"\n";
// }
if(clear) {
for(auto x : val[lc[v]]) update(x, -1);
}
if(ans1 < ans2) {
ans[v] = ans1;
}
else {
ans[v] = ans2;
}
val[v].swap(val[lc[v]]);
for(auto x : val[rc[v]]) val[v].pb(x);
if(!clear) for(auto x : val[rc[v]]) update(x, 1);
}
// cout<<v<<" "<<ans[v]<<"\n";
// for(auto x : val[v]) cout<<x<<" ";
// cout<<"\n";
// if(v == 5) {
// cout<<"HERE\n";
// cout<<lc[v]<<" "<<rc[v]<<" "<<ans[lc[v]]<<" "<<ans[rc[v]]<<"\n";
// for(auto x : val[lc[v]]) cout<<x<<" ";
// cout<<"\n";
// for(auto x : val[rc[v]]) cout<<x<<" ";
// cout<<"\n";
// }
}
void Baizho()
{
cin>>n;
read();
dfs(1, 0);
cout<<ans[1];
}
int main() {
// Freopen("disrupt");
ios_base::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
// precalc();
int ttt = 1;
// cin>>ttt;
for(int i=1; i<=ttt; i++) {Baizho(); }
}
Compilation message
rot.cpp: In function 'void Freopen(std::string)':
rot.cpp:33:34: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rot.cpp:33:76: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | void Freopen(string Key){ freopen((Key+".in").c_str(), "r", stdin); freopen((Key+".out").c_str(), "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9756 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9740 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
10324 KB |
Output is correct |
2 |
Correct |
6 ms |
10068 KB |
Output is correct |
3 |
Correct |
6 ms |
10324 KB |
Output is correct |
4 |
Correct |
6 ms |
10452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
11732 KB |
Output is correct |
2 |
Correct |
11 ms |
10932 KB |
Output is correct |
3 |
Correct |
23 ms |
13052 KB |
Output is correct |
4 |
Correct |
9 ms |
11724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
15056 KB |
Output is correct |
2 |
Correct |
24 ms |
16220 KB |
Output is correct |
3 |
Correct |
28 ms |
18084 KB |
Output is correct |
4 |
Correct |
34 ms |
18204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
24548 KB |
Output is correct |
2 |
Correct |
34 ms |
21708 KB |
Output is correct |
3 |
Correct |
43 ms |
19956 KB |
Output is correct |
4 |
Correct |
40 ms |
17748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
20120 KB |
Output is correct |
2 |
Correct |
49 ms |
21912 KB |
Output is correct |
3 |
Correct |
47 ms |
26184 KB |
Output is correct |
4 |
Correct |
58 ms |
25804 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
117 ms |
32376 KB |
Output is correct |
2 |
Correct |
92 ms |
28744 KB |
Output is correct |
3 |
Correct |
76 ms |
27628 KB |
Output is correct |
4 |
Correct |
90 ms |
27332 KB |
Output is correct |
5 |
Correct |
151 ms |
29220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
27636 KB |
Output is correct |
2 |
Correct |
86 ms |
35888 KB |
Output is correct |
3 |
Correct |
99 ms |
33916 KB |
Output is correct |
4 |
Correct |
72 ms |
36720 KB |
Output is correct |
5 |
Correct |
169 ms |
32516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
28168 KB |
Output is correct |
2 |
Correct |
86 ms |
29404 KB |
Output is correct |
3 |
Correct |
132 ms |
30076 KB |
Output is correct |
4 |
Correct |
93 ms |
28932 KB |
Output is correct |
5 |
Correct |
83 ms |
36668 KB |
Output is correct |
6 |
Correct |
162 ms |
33416 KB |
Output is correct |