#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 = 2e5;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15192 KB |
Output is correct |
2 |
Correct |
6 ms |
15304 KB |
Output is correct |
3 |
Correct |
7 ms |
15196 KB |
Output is correct |
4 |
Correct |
7 ms |
15196 KB |
Output is correct |
5 |
Correct |
7 ms |
15196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15196 KB |
Output is correct |
2 |
Correct |
7 ms |
15196 KB |
Output is correct |
3 |
Correct |
7 ms |
15196 KB |
Output is correct |
4 |
Correct |
6 ms |
15160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15452 KB |
Output is correct |
2 |
Correct |
8 ms |
15452 KB |
Output is correct |
3 |
Correct |
8 ms |
15496 KB |
Output is correct |
4 |
Correct |
9 ms |
15452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16220 KB |
Output is correct |
2 |
Correct |
12 ms |
16476 KB |
Output is correct |
3 |
Correct |
9 ms |
16472 KB |
Output is correct |
4 |
Correct |
9 ms |
16348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
18428 KB |
Output is correct |
2 |
Correct |
22 ms |
19292 KB |
Output is correct |
3 |
Correct |
68 ms |
28132 KB |
Output is correct |
4 |
Correct |
19 ms |
19296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
34128 KB |
Output is correct |
2 |
Correct |
57 ms |
30036 KB |
Output is correct |
3 |
Correct |
63 ms |
32016 KB |
Output is correct |
4 |
Correct |
73 ms |
32340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
37972 KB |
Output is correct |
2 |
Correct |
81 ms |
38228 KB |
Output is correct |
3 |
Correct |
102 ms |
40416 KB |
Output is correct |
4 |
Correct |
73 ms |
34900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
36 ms |
38236 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
34 ms |
37968 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
45 ms |
45392 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
65 ms |
41540 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |