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>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ll long long
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define cd complex<double>
const ll mod = 1e9 + 7;
const ll INF = 1e18L + 5;
const double PI = acos(-1);
const int block = 320;
const int N = 1e6;
int tc, tt = 1;
int n, u, v;
int tdfs = 0, cnt = 0;
int a[N + 5], sz[N + 5];
vi g[N + 5];
set<int> s[N + 5];
ll dp[N + 5];
void build_tree(int u) {
for(int i=1; i<=2; i++) {
int x;
cin>>x;
if(x == 0) {
tdfs++;
g[u].pb(tdfs);
build_tree(tdfs);
}
else {
tdfs++;
g[u].pb(tdfs);
a[tdfs] = x;
cnt++;
}
if(cnt == n) return;
}
}
struct fenwick_tree {
int bit[N + 5];
void update(int pos, int v) {
while(pos <= n) {
bit[pos] += v;
pos += (pos & (-pos));
}
}
int get(int pos) {
int ans = 0;
while(pos) {
ans += bit[pos];
pos -= (pos & (-pos));
}
return ans;
}
} ft;
void dfs_sz(int u, int par) {
sz[u] = 1;
for(auto v: g[u])
if(v != par) {
dfs_sz(v, u);
sz[u] += sz[v];
}
}
void dfs(int u, int par) {
int bc = 0;
for(auto v: g[u])
if(v != par && sz[v] > sz[bc]) bc = v;
for(auto v: g[u])
if(v != par && v != bc) {
dfs(v, u);
dp[u] += dp[v];
for(auto x: s[v])
ft.update(x, -1);
}
if(bc) {
dfs(bc, u);
dp[u] += dp[bc];
swap(s[bc], s[u]);
}
if(a[u] != -1) {
ft.update(a[u], 1);
s[u].insert(a[u]);
}
if((int)g[u].size() == 0) return;
ll c1 = 0, c2 = 0;
for(auto v: g[u])
if(v != par && v != bc) {
for(auto x: s[v]) {
c1 += ft.get(x - 1);
c2 += ft.get(n) - ft.get(x);
s[u].insert(x);
}
}
for(auto v: g[u])
if(v != par && v != bc)
for(auto x: s[v])
ft.update(x, 1);
dp[u] += min(c1, c2);
}
void solve() {
memset(a, -1, sizeof(a));
cin>>n;
build_tree(0);
n = tdfs;
dfs_sz(1, -1);
dfs(1, -1);
cout<<dp[1];
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen(".INP", "r", stdin);
// freopen(".OUT", "w", stdout);
for(tc=1; tc<=tt; tc++) solve();
return 0;
}
# | 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... |