#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];
}
# |
Verdict |
Execution time |
Memory |
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 |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
3 ms |
600 KB |
Output is correct |
3 |
Correct |
2 ms |
856 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
1624 KB |
Output is correct |
2 |
Correct |
8 ms |
860 KB |
Output is correct |
3 |
Correct |
23 ms |
2012 KB |
Output is correct |
4 |
Correct |
7 ms |
1372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
3156 KB |
Output is correct |
2 |
Correct |
26 ms |
3932 KB |
Output is correct |
3 |
Correct |
28 ms |
4944 KB |
Output is correct |
4 |
Correct |
30 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
9052 KB |
Output is correct |
2 |
Correct |
40 ms |
7260 KB |
Output is correct |
3 |
Correct |
48 ms |
5968 KB |
Output is correct |
4 |
Correct |
42 ms |
4948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
6384 KB |
Output is correct |
2 |
Correct |
56 ms |
7508 KB |
Output is correct |
3 |
Correct |
56 ms |
9812 KB |
Output is correct |
4 |
Correct |
54 ms |
9808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
176 ms |
11764 KB |
Output is correct |
2 |
Correct |
106 ms |
10380 KB |
Output is correct |
3 |
Correct |
90 ms |
10580 KB |
Output is correct |
4 |
Correct |
107 ms |
9812 KB |
Output is correct |
5 |
Correct |
156 ms |
9040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
10576 KB |
Output is correct |
2 |
Correct |
105 ms |
15328 KB |
Output is correct |
3 |
Correct |
116 ms |
13904 KB |
Output is correct |
4 |
Correct |
94 ms |
16100 KB |
Output is correct |
5 |
Correct |
201 ms |
10172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
130 ms |
10576 KB |
Output is correct |
2 |
Correct |
97 ms |
12112 KB |
Output is correct |
3 |
Correct |
171 ms |
10580 KB |
Output is correct |
4 |
Correct |
121 ms |
10924 KB |
Output is correct |
5 |
Correct |
89 ms |
16720 KB |
Output is correct |
6 |
Correct |
200 ms |
10580 KB |
Output is correct |