#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 = 5e6;
int tc, tt = 1;
int n, u, v;
int tdfs = 0, cnt = 0;
ll c1 = 0, c2 = 0;
int a[N + 5], sz[N + 5];
vi g[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 add(int u, int par, int val) {
if(a[u] != -1) ft.update(a[u], val);
for(auto v: g[u])
if(v != par) add(v, u, val);
}
void cal(int u, int par) {
if(a[u] != -1) {
c1 += ft.get(a[u] - 1);
c2 += ft.get(n) - ft.get(a[u]);
}
for(auto v: g[u])
if(v != par) cal(v, u);
}
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];
add(v, u, -1);
}
if(bc) {
dfs(bc, u);
dp[u] += dp[bc];
}
if(a[u] != -1) ft.update(a[u], 1);
if((int)g[u].size() == 0) return;
c1 = c2 = 0;
for(auto v: g[u])
if(v != par && v != bc)
cal(v, u);
for(auto v: g[u])
if(v != par && v != bc)
add(v, u, 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
32 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
32 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
32 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
38 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
32 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
31 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
32 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
37 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
30 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
35 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
34 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |