#include <bits/stdc++.h>
// #include "D:/debug.h"
using namespace std;
const int MAXN = 4e6 + 5;
// #define int long long
int n;
int g[MAXN][2];
int sz[MAXN];
long long dp[MAXN];
int a[MAXN];
struct fenwick_tree {
int bit[MAXN + 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;
}
int getR(int l, int r){
return get(r) - get(l - 1);
}
} ft;
int tdfs = 0, cnt = 0;
void build_tree(int u) {
for(int i=0; i<2; i++) {
int x;
cin>>x;
if(x == 0) {
tdfs++;
g[u][i] = tdfs;
build_tree(tdfs);
}
else {
tdfs++;
g[u][i] = tdfs;
a[tdfs] = x;
cnt++;
}
if(cnt == n) return;
}
}
void calsz(int u){
sz[u] = 1;
for(int i = 0; i < 2; i++){
if(g[u][i] != 0) calsz(g[u][i]), sz[u] += sz[g[u][i]];
}
}
void mod(int u, int val){
if(a[u] != 0) ft.update(a[u], val);
for(int i = 0; i < 2; i++)
if(g[u][i] != 0) mod(g[u][i], val);
}
long long c1, c2;
void cal(int u){
if(a[u] != 0){
c1 += ft.get(a[u] - 1);
c2 += ft.getR(a[u] + 1, n);
}
for(int i = 0; i < 2; i++)
if(g[u][i] != 0) cal(g[u][i]);
}
void dfs(int u){
int bc = 0;
for(int i = 0; i < 2; i++){
if(sz[g[u][i]] > sz[bc]) bc = g[u][i];
}
for(int i = 0; i < 2; i++)
if(g[u][i] != bc && g[u][i] != 0) dfs(g[u][i]), dp[u] += dp[g[u][i]], mod(g[u][i], -1);
if(bc) dfs(bc), dp[u] += dp[bc];
if(a[u] != 0) ft.update(a[u], 1);
c1 = 0, c2 = 0;
for(int i = 0; i < 2; i++)
if(g[u][i] != bc && g[u][i] != 0) cal(g[u][i]);
dp[u] += min(c1,c2);
for(int i = 0; i < 2; i++)
if(g[u][i] != bc && g[u][i] != 0) mod(g[u][i], 1);
}
int main(){
cin >> n;
build_tree(0);
calsz(1);
dfs(1);
cout << dp[1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
2 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1628 KB |
Output is correct |
2 |
Correct |
8 ms |
860 KB |
Output is correct |
3 |
Correct |
23 ms |
1852 KB |
Output is correct |
4 |
Correct |
12 ms |
1408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
3016 KB |
Output is correct |
2 |
Correct |
26 ms |
3924 KB |
Output is correct |
3 |
Correct |
30 ms |
4952 KB |
Output is correct |
4 |
Correct |
42 ms |
5208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
8976 KB |
Output is correct |
2 |
Correct |
47 ms |
7092 KB |
Output is correct |
3 |
Correct |
49 ms |
5788 KB |
Output is correct |
4 |
Correct |
44 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
6588 KB |
Output is correct |
2 |
Correct |
58 ms |
7508 KB |
Output is correct |
3 |
Correct |
56 ms |
9812 KB |
Output is correct |
4 |
Correct |
67 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
11600 KB |
Output is correct |
2 |
Correct |
111 ms |
10576 KB |
Output is correct |
3 |
Correct |
120 ms |
10580 KB |
Output is correct |
4 |
Correct |
114 ms |
9860 KB |
Output is correct |
5 |
Correct |
174 ms |
9064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
10576 KB |
Output is correct |
2 |
Correct |
113 ms |
15384 KB |
Output is correct |
3 |
Correct |
124 ms |
13904 KB |
Output is correct |
4 |
Correct |
132 ms |
16216 KB |
Output is correct |
5 |
Correct |
216 ms |
10320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
125 ms |
10716 KB |
Output is correct |
2 |
Correct |
103 ms |
12156 KB |
Output is correct |
3 |
Correct |
152 ms |
10624 KB |
Output is correct |
4 |
Correct |
122 ms |
10832 KB |
Output is correct |
5 |
Correct |
93 ms |
16748 KB |
Output is correct |
6 |
Correct |
188 ms |
10576 KB |
Output is correct |