#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000005, mod = 998244353;
int n;
int hvy[MAXN], ans[MAXN], a[MAXN], dp[MAXN], leave[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){
if (adj[u].empty()) leave[u] = 1;
int sz = 1, mx = 0;
for (int v : adj[u]){
if (v != p){
int csz = pre_dfs(v, u);
leave[u] += leave[v];
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 |
31064 KB |
Output is correct |
2 |
Correct |
4 ms |
31064 KB |
Output is correct |
3 |
Correct |
4 ms |
31068 KB |
Output is correct |
4 |
Correct |
4 ms |
31068 KB |
Output is correct |
5 |
Correct |
4 ms |
31068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31068 KB |
Output is correct |
2 |
Correct |
5 ms |
31256 KB |
Output is correct |
3 |
Correct |
5 ms |
31068 KB |
Output is correct |
4 |
Correct |
4 ms |
31068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31068 KB |
Output is correct |
2 |
Correct |
5 ms |
31068 KB |
Output is correct |
3 |
Correct |
5 ms |
31292 KB |
Output is correct |
4 |
Correct |
5 ms |
31324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
31448 KB |
Output is correct |
2 |
Correct |
7 ms |
31320 KB |
Output is correct |
3 |
Correct |
6 ms |
31580 KB |
Output is correct |
4 |
Correct |
6 ms |
31580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
32348 KB |
Output is correct |
2 |
Correct |
12 ms |
31760 KB |
Output is correct |
3 |
Correct |
26 ms |
34436 KB |
Output is correct |
4 |
Correct |
10 ms |
32248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
37432 KB |
Output is correct |
2 |
Correct |
27 ms |
38736 KB |
Output is correct |
3 |
Correct |
30 ms |
39972 KB |
Output is correct |
4 |
Correct |
30 ms |
40016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
44636 KB |
Output is correct |
2 |
Correct |
41 ms |
42068 KB |
Output is correct |
3 |
Correct |
46 ms |
40352 KB |
Output is correct |
4 |
Correct |
42 ms |
39260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
40252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
154 ms |
45396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
96 ms |
44004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
118 ms |
45904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |