#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 |
4 ms |
31068 KB |
Output is correct |
2 |
Correct |
5 ms |
31068 KB |
Output is correct |
3 |
Correct |
5 ms |
31236 KB |
Output is correct |
4 |
Correct |
6 ms |
31068 KB |
Output is correct |
5 |
Correct |
6 ms |
31204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
31068 KB |
Output is correct |
2 |
Correct |
4 ms |
31068 KB |
Output is correct |
3 |
Correct |
5 ms |
31064 KB |
Output is correct |
4 |
Correct |
5 ms |
31068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31064 KB |
Output is correct |
2 |
Correct |
4 ms |
31064 KB |
Output is correct |
3 |
Correct |
5 ms |
31068 KB |
Output is correct |
4 |
Correct |
5 ms |
31068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
33624 KB |
Output is correct |
2 |
Correct |
7 ms |
33440 KB |
Output is correct |
3 |
Correct |
6 ms |
33676 KB |
Output is correct |
4 |
Correct |
6 ms |
33628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
34316 KB |
Output is correct |
2 |
Correct |
12 ms |
33624 KB |
Output is correct |
3 |
Correct |
27 ms |
34392 KB |
Output is correct |
4 |
Correct |
10 ms |
34136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
36952 KB |
Output is correct |
2 |
Correct |
32 ms |
38492 KB |
Output is correct |
3 |
Correct |
32 ms |
39760 KB |
Output is correct |
4 |
Correct |
30 ms |
39532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
44120 KB |
Output is correct |
2 |
Correct |
37 ms |
41556 KB |
Output is correct |
3 |
Correct |
47 ms |
39760 KB |
Output is correct |
4 |
Correct |
38 ms |
38740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
39792 KB |
Output is correct |
2 |
Correct |
48 ms |
40968 KB |
Output is correct |
3 |
Correct |
51 ms |
44116 KB |
Output is correct |
4 |
Correct |
48 ms |
43860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
145 ms |
45108 KB |
Output is correct |
2 |
Correct |
106 ms |
43600 KB |
Output is correct |
3 |
Correct |
86 ms |
44116 KB |
Output is correct |
4 |
Correct |
100 ms |
43092 KB |
Output is correct |
5 |
Correct |
185 ms |
41812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
43924 KB |
Output is correct |
2 |
Correct |
98 ms |
49744 KB |
Output is correct |
3 |
Correct |
105 ms |
47504 KB |
Output is correct |
4 |
Correct |
90 ms |
50772 KB |
Output is correct |
5 |
Correct |
201 ms |
43220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
43856 KB |
Output is correct |
2 |
Correct |
97 ms |
45140 KB |
Output is correct |
3 |
Correct |
152 ms |
43464 KB |
Output is correct |
4 |
Correct |
120 ms |
44172 KB |
Output is correct |
5 |
Correct |
80 ms |
51212 KB |
Output is correct |
6 |
Correct |
185 ms |
43344 KB |
Output is correct |