#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define DEBUG 0
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int MAX_N = 4e5 + 10;
int n, cnt;
int L[MAX_N], R[MAX_N];
ordered_set <int> arr[MAX_N];
int read_input() {
int now;
cin >> now;
if(now == 0) {
now = ++cnt;
L[now] = read_input();
R[now] = read_input();
}
else {
now += n;
L[now] = R[now] = -1;
}
return now;
}
long long dfs(int u) {
if(L[u] == -1 and R[u] == -1) {
arr[u].insert(u - n);
return 0;
}
int v1 = L[u], v2 = R[u];
long long res = dfs(v1) + dfs(v2);
if(arr[v1].size() < arr[v2].size()) {
swap(v1, v2);
}
for(auto x : arr[v2]) {
res += arr[v1].size() - arr[v1].order_of_key(x + 1);
}
for(auto x : arr[v2]) {
arr[v1].insert(x);
}
return res;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
read_input();
cout << dfs(1);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
31612 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
26 ms |
31520 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
31580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
28 ms |
32260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
29 ms |
33532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
51 ms |
35712 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
65 ms |
45980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
87 ms |
41364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
119 ms |
50812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
118 ms |
48252 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
130 ms |
47456 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |