#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6+5;
int n, cnt_node, timerDFS, sz[N], val[N];
int tin[N], tout[N], tour[N], bit[N];
vector<int> adj[N];
int dfs_Size(int x, int p){
sz[x] = 1;
tour[tin[x] = ++timerDFS] = x;
for(int v : adj[x])if(v != p) sz[x] += dfs_Size(v, x);
tout[x] = timerDFS;
return sz[x];
}
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);
}
int sack(int x, int p, bool keep){
int hev = 0;
for(int v : adj[x])if(v != p && sz[v] > sz[hev]) hev = v;
int 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);
int left = (val[x] ? get_range(val[x]+1, n) : 0);
int 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 + (val[cur_node] ? get_range(val[cur_node]+1, n) : 0);
right = right + (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:118:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
118 | freopen(name".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
rot.cpp:119:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
119 | freopen(name".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
35408 KB |
Output is correct |
2 |
Correct |
6 ms |
35516 KB |
Output is correct |
3 |
Correct |
7 ms |
35408 KB |
Output is correct |
4 |
Correct |
6 ms |
35408 KB |
Output is correct |
5 |
Correct |
6 ms |
35408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
35408 KB |
Output is correct |
2 |
Correct |
5 ms |
35408 KB |
Output is correct |
3 |
Correct |
6 ms |
35408 KB |
Output is correct |
4 |
Correct |
6 ms |
35408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
35408 KB |
Output is correct |
2 |
Correct |
7 ms |
35408 KB |
Output is correct |
3 |
Correct |
6 ms |
35408 KB |
Output is correct |
4 |
Correct |
6 ms |
35408 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
37968 KB |
Output is correct |
2 |
Correct |
8 ms |
37880 KB |
Output is correct |
3 |
Correct |
7 ms |
37968 KB |
Output is correct |
4 |
Correct |
9 ms |
38000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
39416 KB |
Output is correct |
2 |
Correct |
12 ms |
37812 KB |
Output is correct |
3 |
Correct |
31 ms |
40528 KB |
Output is correct |
4 |
Correct |
12 ms |
38736 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
43088 KB |
Output is correct |
2 |
Correct |
34 ms |
43456 KB |
Output is correct |
3 |
Correct |
36 ms |
47440 KB |
Output is correct |
4 |
Correct |
37 ms |
47176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
56144 KB |
Output is correct |
2 |
Correct |
47 ms |
51784 KB |
Output is correct |
3 |
Correct |
61 ms |
48960 KB |
Output is correct |
4 |
Correct |
43 ms |
47184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
49736 KB |
Output is correct |
2 |
Correct |
67 ms |
52984 KB |
Output is correct |
3 |
Correct |
70 ms |
58124 KB |
Output is correct |
4 |
Correct |
64 ms |
57672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
172 ms |
60232 KB |
Output is correct |
2 |
Correct |
118 ms |
61436 KB |
Output is correct |
3 |
Correct |
110 ms |
59976 KB |
Output is correct |
4 |
Correct |
117 ms |
60744 KB |
Output is correct |
5 |
Correct |
191 ms |
56400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
112 ms |
60232 KB |
Output is correct |
2 |
Runtime error |
57 ms |
65536 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
127 ms |
61768 KB |
Output is correct |
2 |
Correct |
113 ms |
63048 KB |
Output is correct |
3 |
Correct |
170 ms |
59976 KB |
Output is correct |
4 |
Correct |
132 ms |
61656 KB |
Output is correct |
5 |
Runtime error |
78 ms |
65536 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |