#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 1000005
#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;
oset *a[N];
i64 t[N], sz[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;
}
u64 order(const oset &s, oset::iterator it)
{
return it == s.end() ? s.size() : s.order_of_key(*it);
}
void dfs0(int u)
{
if (!L[u]) return void((a[u] = new oset())->insert({A[u], u}));
dfs0(L[u]), dfs0(R[u]);
i64 l2r = 0, r2l = 0;
if (a[R[u]]->size() > a[L[u]]->size())
{
for (const pair<int, int> &x : *a[L[u]])
l2r += order(*a[R[u]], a[R[u]]->lower_bound({x.first, 0})),
r2l += sz[R[u]] - order(*a[R[u]], a[R[u]]->lower_bound({x.first+1, 0}));
}
else
{
for (const pair<int, int> &x : *a[R[u]])
l2r += sz[L[u]] - order(*a[L[u]], a[L[u]]->lower_bound({x.first+1, 0})),
r2l += order(*a[L[u]], a[L[u]]->lower_bound({x.first, 0}));
}
if (r2l < l2r) swap(L[u], R[u]);
if (sz[R[u]] > sz[L[u]])
{
a[u] = a[R[u]];
for (auto x : *a[L[u]]) a[u]->insert(x);
delete a[L[u]];
}
else
{
a[u] = a[L[u]];
for (auto x : *a[R[u]]) a[u]->insert(x);
delete a[R[u]];
}
}
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 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
5 |
Correct |
1 ms |
8536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
1 ms |
8540 KB |
Output is correct |
4 |
Correct |
1 ms |
8540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8540 KB |
Output is correct |
2 |
Correct |
2 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Correct |
2 ms |
8796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
9304 KB |
Output is correct |
2 |
Correct |
7 ms |
9052 KB |
Output is correct |
3 |
Correct |
4 ms |
9308 KB |
Output is correct |
4 |
Correct |
5 ms |
9560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
11096 KB |
Output is correct |
2 |
Correct |
21 ms |
11860 KB |
Output is correct |
3 |
Correct |
68 ms |
13672 KB |
Output is correct |
4 |
Correct |
15 ms |
13076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
15736 KB |
Output is correct |
2 |
Correct |
61 ms |
18264 KB |
Output is correct |
3 |
Correct |
73 ms |
20308 KB |
Output is correct |
4 |
Correct |
68 ms |
20308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
29536 KB |
Output is correct |
2 |
Correct |
93 ms |
27328 KB |
Output is correct |
3 |
Correct |
124 ms |
22080 KB |
Output is correct |
4 |
Correct |
83 ms |
20816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
156 ms |
22904 KB |
Output is correct |
2 |
Correct |
150 ms |
25176 KB |
Output is correct |
3 |
Correct |
125 ms |
32848 KB |
Output is correct |
4 |
Correct |
138 ms |
32932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
375 ms |
39424 KB |
Output is correct |
2 |
Correct |
208 ms |
33576 KB |
Output is correct |
3 |
Correct |
245 ms |
37196 KB |
Output is correct |
4 |
Correct |
260 ms |
34764 KB |
Output is correct |
5 |
Correct |
444 ms |
34128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
270 ms |
35336 KB |
Output is correct |
2 |
Correct |
261 ms |
50772 KB |
Output is correct |
3 |
Correct |
310 ms |
40092 KB |
Output is correct |
4 |
Correct |
221 ms |
49960 KB |
Output is correct |
5 |
Correct |
562 ms |
41812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
312 ms |
43812 KB |
Output is correct |
2 |
Correct |
279 ms |
41276 KB |
Output is correct |
3 |
Correct |
408 ms |
38344 KB |
Output is correct |
4 |
Correct |
341 ms |
43864 KB |
Output is correct |
5 |
Correct |
229 ms |
50116 KB |
Output is correct |
6 |
Correct |
520 ms |
42320 KB |
Output is correct |