#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005, mod = 998244353;
int n;
int hvy[MAXN], a[MAXN];
long long dp[MAXN];
vector<int> adj[MAXN];
struct FenwickTree{
int bit[MAXN];
void upd(int i, int x){
while (i <= n){
bit[i] += x;
i += i & (-i);
}
}
int sum(int i){
int ans = 0;
while (i){
ans += bit[i];
i -= i & (-i);
}
return ans;
}
int get(int l, int r){
if (l > r) return 0;
return sum(r) - sum(l - 1);
}
}ft;
int node = 1;
void input(){
int x; cin >> x;
a[node] = x;
if (x) return;
int nw = node;
adj[nw].push_back(++node);
input();
adj[nw].push_back(++node);
input();
}
int pre_dfs(int u, int p){
int sz = 1, mx = 0;
for (int v : adj[u]){
if (v != p){
int csz = pre_dfs(v, u);
sz += csz;
if (mx < csz){
mx = csz;
hvy[u] = v;
}
}
}
return sz;
}
void ope(int u, int p, int x){
if (a[u]) ft.upd(a[u], x);
for (int v : adj[u]){
if (v != p) ope(v, u, x);
}
}
long long c1, c2;
void calc(int u, int p){
if (a[u]){
c1 += ft.get(a[u] + 1, n);
c2 += ft.get(1, a[u] - 1);
}
for (int v : adj[u]){
if (v != p) calc(v, u);
}
}
void dfs(int u, int p){
for (int &v : adj[u]){
if (v != p && v != hvy[u]){
dfs(v, u);
dp[u] += dp[v];
ope(v, u, -1);
}
}
if (hvy[u]){
dfs(hvy[u], u);
dp[u] += dp[hvy[u]];
}
c1 = c2 = 0;
for (int v : adj[u]){
if (v != p && v != hvy[u]){
calc(v, u);
}
}
dp[u] += min(c1, c2);
for (int v : adj[u]){
if (v != p && v != hvy[u]){
ope(v, u, 1);
}
}
if (a[u]) ft.upd(a[u], 1);
}
void solve(){
cin >> n;
input();
pre_dfs(1, 1);
dfs(1, 1);
cout << dp[1];
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1; // cin >> t;
while (t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31068 KB |
Output is correct |
2 |
Correct |
4 ms |
31068 KB |
Output is correct |
3 |
Correct |
4 ms |
31068 KB |
Output is correct |
4 |
Correct |
4 ms |
31068 KB |
Output is correct |
5 |
Correct |
5 ms |
31068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31068 KB |
Output is correct |
2 |
Correct |
4 ms |
31068 KB |
Output is correct |
3 |
Correct |
4 ms |
31180 KB |
Output is correct |
4 |
Correct |
6 ms |
31320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31064 KB |
Output is correct |
2 |
Correct |
6 ms |
31276 KB |
Output is correct |
3 |
Correct |
6 ms |
31068 KB |
Output is correct |
4 |
Correct |
5 ms |
31068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
33624 KB |
Output is correct |
2 |
Correct |
7 ms |
33372 KB |
Output is correct |
3 |
Correct |
6 ms |
33628 KB |
Output is correct |
4 |
Correct |
6 ms |
33624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
34396 KB |
Output is correct |
2 |
Correct |
12 ms |
33628 KB |
Output is correct |
3 |
Correct |
31 ms |
34140 KB |
Output is correct |
4 |
Correct |
10 ms |
34136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
36956 KB |
Output is correct |
2 |
Correct |
27 ms |
38492 KB |
Output is correct |
3 |
Correct |
28 ms |
39764 KB |
Output is correct |
4 |
Correct |
33 ms |
39504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
44116 KB |
Output is correct |
2 |
Correct |
41 ms |
41556 KB |
Output is correct |
3 |
Correct |
46 ms |
39620 KB |
Output is correct |
4 |
Correct |
38 ms |
38780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
39772 KB |
Output is correct |
2 |
Correct |
49 ms |
41296 KB |
Output is correct |
3 |
Correct |
50 ms |
44112 KB |
Output is correct |
4 |
Correct |
47 ms |
43868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
147 ms |
44956 KB |
Output is correct |
2 |
Correct |
102 ms |
45028 KB |
Output is correct |
3 |
Correct |
83 ms |
45392 KB |
Output is correct |
4 |
Correct |
108 ms |
44628 KB |
Output is correct |
5 |
Correct |
165 ms |
43348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
44112 KB |
Output is correct |
2 |
Correct |
97 ms |
49744 KB |
Output is correct |
3 |
Correct |
114 ms |
49232 KB |
Output is correct |
4 |
Correct |
93 ms |
52304 KB |
Output is correct |
5 |
Correct |
201 ms |
44628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
132 ms |
43856 KB |
Output is correct |
2 |
Correct |
110 ms |
46928 KB |
Output is correct |
3 |
Correct |
151 ms |
44968 KB |
Output is correct |
4 |
Correct |
115 ms |
45640 KB |
Output is correct |
5 |
Correct |
81 ms |
52864 KB |
Output is correct |
6 |
Correct |
198 ms |
45016 KB |
Output is correct |