This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll int
#define ull unsigned long long
#define ld long double
#define pb push_back
#define pf push_front
#define vi vector<ll>
#define vii vector<vi>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define fi first
#define se second
using namespace std;
const ll mod = 1e9+7;
const ll inf = 2e18;
const ll base = 31;
const ll blocksz = 320;
const ll N = 1e5+8;
ll g[N][2];
ll a[N],tdfs,cnt,n;
long long dp[N], inv1, inv2;
void build_tree(ll u) {
for(ll i = 0; i < 2; i++) {
ll x;
cin >> x;
if(x == 0) {
tdfs++;
g[u][i] = tdfs;
build_tree(tdfs);
}
else {
tdfs++;
g[u][i] = tdfs;
a[tdfs] = x;
cnt++;
}
if(cnt == n) return;
}
}
struct Binary_Indexed_Tree{
ll BIT[N+5];
void update(ll i, ll x){
while(i <= n){
BIT[i] += x;
i += (i&-i);
}
}
ll get(ll i){
ll ans = 0;
while(i){
ans += BIT[i];
i -= (i&-i);
}
return ans;
}
} bit;
ll sz[N];
void dfs_size(ll u, ll p){
sz[u] = 1;
for(ll i = 0; i < 2; i++){
ll v = g[u][i];
if(v != p && v != -1) {
dfs_size(v,u);
sz[u] += sz[v];
}
}
}
void sack_add(ll u, ll p){
if(a[u] != -1) bit.update(a[u],1);
for(ll i = 0; i < 2; i++){
ll v = g[u][i];
if(v != p && v != -1) sack_add(v,u);
}
}
void sack_rmv(ll u, ll p){
if(a[u] != -1) bit.update(a[u],-1);
for(ll i = 0; i < 2; i++){
ll v = g[u][i];
if(v != p && v != -1) sack_rmv(v,u);
}
}
void cal_inv(ll u, ll p){
if(a[u] != -1){
inv1 = bit.get(a[u]-1);
inv2 = bit.get(n)-bit.get(a[u]);
}
for(ll i = 0; i < 2; i++){
ll v = g[u][i];
if(v != p && v != -1) cal_inv(v,u);
}
}
void sack(ll u, ll p){
ll big = 0;
for(ll i = 0; i < 2; i++){
ll v = g[u][i];
if(v != p && v != -1 && sz[v] > sz[big]) big = v;
}
for(ll i = 0; i < 2; i++){
ll v = g[u][i];
if(v != p && v != -1 && v != big){
sack(v,u);
sack_rmv(v,u);
dp[u] += dp[v];
}
}
if(big){
sack(big,u);
dp[u] += dp[big];
}
if(a[u] != -1) bit.update(a[u],1);
inv1 = inv2 = 0;
for(ll i = 0; i < 2; i++){
ll v = g[u][i];
if(v != p && v != -1 && v != big){
cal_inv(v,u);
}
}
for(ll i = 0; i < 2; i++){
ll v = g[u][i];
if(v != p && v != -1 && v != big){
sack_add(v,u);
}
}
dp[u] += min(inv1,inv2);
}
void solve(){
memset(a,-1,sizeof(a));
memset(g,-1,sizeof(g));
cin >> n;
build_tree(0);
n = tdfs;
dfs_size(1,1);
sack(1,1);
cout << dp[1];
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen("test.inp", "r")){
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
ll T = 1;
// cin >> T;
for (ll i = 1; i <= T; i++){
solve();
// cout << '\n';
}
}
Compilation message (stderr)
rot.cpp:16:16: warning: overflow in conversion from 'double' to 'int' changes value from '2.0e+18' to '2147483647' [-Woverflow]
16 | const ll inf = 2e18;
| ^~~~
rot.cpp: In function 'int main()':
rot.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
rot.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | freopen("test.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |