#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>
using i64 = long long;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;
using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
#define N 200005
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using oset = tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update>;
int root, n, L[N], R[N], A[N], alloc, sz[N];
oset *a[N];
i64 t[N];
int read()
{
int u = ++alloc, p;
cin >> p;
if (p) return sz[u] = 1, A[u] = p, u;
L[u] = read();
R[u] = read();
return sz[u] = sz[L[u]] + sz[R[u]], u;
}
static inline i64 qry(u64 p)
{
u64 z{0};
for (; p < N; p+=p&-p) z += t[p];
return z;
}
static inline void upd(u64 p, i64 k)
{
for (; p; p-=p&-p) t[p] += k;
}
void dfs0(int u)
{
if (!L[u]) return void((a[u] = new oset())->insert({A[u], u}));
dfs0(L[u]), dfs0(R[u]);
u64 l2r = 0, r2l = 0;
if (a[R[u]]->size() > a[L[u]]->size())
{
for (pair<int, int> x : *a[L[u]])
l2r += a[R[u]]->order_of_key(*a[R[u]]->lower_bound({x.first, -1}));
r2l = u64(a[R[u]]->size()) * a[L[u]]->size() - l2r;
}
else
{
for (pair<int, int> x : *a[R[u]])
r2l += a[L[u]]->order_of_key(*a[L[u]]->lower_bound({x.first, -1}));
l2r = u64(a[R[u]]->size()) * a[L[u]]->size() - r2l;
}
if (r2l < l2r) swap(L[u], R[u]);
if (a[R[u]]->size() > a[L[u]]->size())
{
a[u] = a[R[u]];
for (auto x : *a[L[u]]) a[u]->insert(x);
}
else
{
a[u] = a[L[u]];
for (auto x : *a[R[u]]) a[u]->insert(x);
}
}
vector<int> corona;
void dfs2(int u)
{
if (!L[u]) corona.push_back(A[u]);
else dfs2(L[u]), dfs2(R[u]);
}
int main()
{
ShinLena;
cin >> n;
root = read();
dfs0(root);
dfs2(root);
u64 z{0};
for (auto x : corona) upd(x, 1), z += qry(x+1);
cout << z;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
3932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
7004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
87 ms |
30932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
94 ms |
36292 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
115 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
24 ms |
10832 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
16 ms |
7772 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
16 ms |
7404 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |