#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
int root, n, L[N], R[N], A[N], alloc, sz[N];
vector<int> *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;
}
struct persistent_segment_tree
{
int alloc;
} pt;
void dfs(int u)
{
if (!L[u]) return void(a[u] = new vector<int>({A[u]}));
dfs(L[u]), dfs(R[u]);
u64 l2r = 0, r2l = 0;
if (0) {
for (auto x : *a[L[u]]) upd(x, 1);
for (auto x : *a[R[u]]) l2r += qry(x + 1);
for (auto x : *a[L[u]]) upd(x, -1);
for (auto x : *a[R[u]]) upd(x, 1);
for (auto x : *a[L[u]]) r2l += qry(x + 1);
for (auto x : *a[R[u]]) upd(x, -1);
}
if (l2r > r2l) 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]->push_back(x);
}
else
{
a[u] = a[L[u]];
for (auto x : *a[R[u]]) a[u]->push_back(x);
}
}
vector<int> corona;
void dfs1(int u)
{
if (!L[u]) corona.push_back(A[u]);
else dfs1(L[u]), dfs1(R[u]);
}
int main()
{
ShinLena;
cin >> n;
root = read();
dfs(root);
dfs1(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 |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
2 ms |
3164 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
4444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
14 ms |
9052 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
16472 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
84 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
89 ms |
65536 KB |
Execution killed with signal 9 |
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 |
20 ms |
7396 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |