#include<bits/stdc++.h>
// #define int long long
using namespace std;
using ll = long long;
const int N = 8e5+5;
const int label = 2e5+5;
int n, cnt_node, timerDFS, val[N];
int tin[N], tout[N], tour[N], bit[label];
vector<int> adj[N];
void dfs_Size(int x, int p){
tour[tin[x] = ++timerDFS] = x;
for(int v : adj[x])if(v != p) dfs_Size(v, x);
tout[x] = timerDFS;
return;
}
void update(int p, int val){
if(!p) return;
for(; p <= n; p += p&-p) bit[p] += val;
return;
}
int get(int p){
int res = 0;
if(!p || p < 0) return 0;
for(; p; p -= p&-p) res += bit[p];
return res;
}
int get_range(int l, int r){
return get(r) - get(l-1);
}
void sack_add(int x){
if(val[x]) update(val[x], 1);
}
void sack_rem(int x){
if(val[x]) update(val[x], -1);
}
ll sack(int x, int p, bool keep){
int Mx = 0, hev = 0;
for(int v : adj[x])if(v != p){
int szv = tout[v] - tin[v]+1;
if(szv > Mx) hev = v, Mx = szv;
}
ll answer = 0;
for(int v : adj[x])if(v != p && v != hev) answer = answer + sack(v, x, 0);
if(hev) answer = answer + sack(hev, x, 1);
sack_add(x);
ll left = (val[x] ? get_range(val[x]+1, n) : 0);
ll right = (val[x] ? get_range(1, val[x]-1) : 0);
for(int v : adj[x])if(v != p && v != hev){
for(int i = tin[v]; i <= tout[v]; i++){
int cur_node = tour[i];
left = left + 1ll * (val[cur_node] ? get_range(val[cur_node]+1, n) : 0);
right = right + 1ll * (val[cur_node] ? get_range(1, val[cur_node]-1) : 0);
}
for(int i = tin[v]; i <= tout[v]; i++){
sack_add(tour[i]);
}
}
if(!keep){
for(int i = tin[x]; i <= tout[x]; i++)
sack_rem(tour[i]);
}
return answer + min(left, right);
}
void input(int cur_node)
{
int x; cin >> x;
if(!x){
// Start 2 new subtree
for(int i = 0; i < 2; i++){
adj[cur_node].push_back(++cnt_node);
input(cnt_node);
}
}
else{
// Leaf info
val[cur_node] = x;
}
return;
}
void solve()
{
cin >> n;
cnt_node = 1;
input(cnt_node);
dfs_Size(1, -1);
cout << sack(1, -1, 0) <<"\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
solve();
return 0;
}
Compilation message
rot.cpp: In function 'int32_t main()':
rot.cpp:123:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
123 | freopen(name".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
rot.cpp:124:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
124 | freopen(name".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27216 KB |
Output is correct |
2 |
Correct |
5 ms |
27216 KB |
Output is correct |
3 |
Correct |
5 ms |
27216 KB |
Output is correct |
4 |
Correct |
6 ms |
27216 KB |
Output is correct |
5 |
Correct |
5 ms |
27216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
27216 KB |
Output is correct |
2 |
Correct |
6 ms |
27216 KB |
Output is correct |
3 |
Correct |
5 ms |
27216 KB |
Output is correct |
4 |
Correct |
5 ms |
27216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27216 KB |
Output is correct |
2 |
Correct |
5 ms |
27436 KB |
Output is correct |
3 |
Correct |
5 ms |
27216 KB |
Output is correct |
4 |
Correct |
5 ms |
27216 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27728 KB |
Output is correct |
2 |
Correct |
6 ms |
27216 KB |
Output is correct |
3 |
Correct |
6 ms |
27728 KB |
Output is correct |
4 |
Correct |
6 ms |
27728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
30800 KB |
Output is correct |
2 |
Correct |
12 ms |
29764 KB |
Output is correct |
3 |
Correct |
33 ms |
30288 KB |
Output is correct |
4 |
Correct |
12 ms |
30576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
30800 KB |
Output is correct |
2 |
Correct |
31 ms |
32820 KB |
Output is correct |
3 |
Correct |
35 ms |
34580 KB |
Output is correct |
4 |
Correct |
32 ms |
34364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
40472 KB |
Output is correct |
2 |
Correct |
46 ms |
36680 KB |
Output is correct |
3 |
Correct |
53 ms |
34128 KB |
Output is correct |
4 |
Correct |
44 ms |
32848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
33360 KB |
Output is correct |
2 |
Correct |
58 ms |
35144 KB |
Output is correct |
3 |
Correct |
67 ms |
39768 KB |
Output is correct |
4 |
Correct |
63 ms |
39252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
165 ms |
41288 KB |
Output is correct |
2 |
Correct |
108 ms |
38964 KB |
Output is correct |
3 |
Correct |
94 ms |
39692 KB |
Output is correct |
4 |
Correct |
104 ms |
38472 KB |
Output is correct |
5 |
Correct |
162 ms |
36688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
100 ms |
39280 KB |
Output is correct |
2 |
Correct |
108 ms |
48544 KB |
Output is correct |
3 |
Correct |
131 ms |
45488 KB |
Output is correct |
4 |
Correct |
95 ms |
50016 KB |
Output is correct |
5 |
Correct |
203 ms |
38992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
111 ms |
38984 KB |
Output is correct |
2 |
Correct |
100 ms |
40264 KB |
Output is correct |
3 |
Correct |
153 ms |
37704 KB |
Output is correct |
4 |
Correct |
120 ms |
38984 KB |
Output is correct |
5 |
Correct |
104 ms |
50248 KB |
Output is correct |
6 |
Correct |
208 ms |
39184 KB |
Output is correct |