#include <bits/stdc++.h>
namespace std
{
template <class Fun>
class y_combinator_result
{
Fun fun_;
public:
template <class T>
explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}
template <class... Args>
decltype(auto) operator()(Args &&... args)
{
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template <class Fun>
decltype(auto) y_combinator(Fun &&fun)
{
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
} // namespace std
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef vector<pll> vll;
typedef vector<vl> vvl;
#define fori(i, n) for (int i = 0; i < n; i++)
#define ford(i, n) for (int i = n - 1; i >= 0; i--)
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define repd(i, a, b) for (int i = a; i >= b; i--)
#define trav(x, a) for (auto &x : a)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define endl '\n'
#define sz(a) (int)(a).size()
#define fi first
#define se second
clock_t time_p = clock();
void time_taken()
{
time_p = clock() - time_p;
cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}
const ll mod = 1e9 + 7;
const ll INF = 1e18;
#include <bits/extc++.h>
using namespace __gnu_pbds;
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// s.order_of_key(x) -> Number of elements in s strictly smaller than x
// auto it = s.find_by_order(k) -> iterator to the kth smallest element(0-indexed)
struct BIT
{
vector<long long> bit;
BIT(int n = 1e5 + 6) : bit(n)
{
}
void update(int x, int v)
{
while (x < int(bit.size()))
{
bit[x] += v;
x += (x & -x);
}
}
long long query(int x)
{
long long sum = 0;
while (x > 0)
{
sum += bit[x];
x -= (x & -x);
}
return sum;
}
void upd_range(int l, int r, int v)
{
update(l, v);
update(r + 1, -v);
}
long long query_range(int l, int r)
{
return query(r) - query(l - 1);
}
};
int main()
{
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int n;
cin >> n;
vi adj[2 * n + 10], sub(2 * n + 10), val(2 * n + 10);
BIT b(n + 10);
ll ans = 0;
int cur = 0;
function<int()> read = [&]() -> int {
int p;
cin >> p;
int me = cur++;
val[me] = p;
sub[me] = 1;
if (p)
{
return 1;
}
adj[me].pb(cur);
sub[me] += read();
adj[me].pb(cur);
sub[me] += read();
return sub[me];
};
read();
function<vi(int, bool)> go = [&](int u, bool keep) -> vi {
vi ret;
if (val[u])
{
if (keep)
b.update(val[u], 1);
ret.pb(val[u]);
return ret;
}
bool fl = (sub[adj[u][0]] > sub[adj[u][1]]);
if (fl)
{
ll i1 = 0, i2 = 0;
auto v1 = go(adj[u][1], false);
auto v2 = go(adj[u][0], true);
ret = v2;
for (int x : v1)
{
ret.pb(x);
int lt = b.query(x);
int gt = sz(v2) - lt;
i1 += lt;
i2 += gt;
}
for (int x : v1)
b.update(x, 1);
ans += min(i1, i2);
}
else
{
ll i1 = 0, i2 = 0;
auto v1 = go(adj[u][0], false);
auto v2 = go(adj[u][1], true);
ret = v2;
for (int x : v1)
{
ret.pb(x);
int lt = b.query(x);
int gt = sz(v2) - lt;
i1 += lt;
i2 += gt;
}
for (int x : v1)
b.update(x, 1);
ans += min(i1, i2);
}
if (!keep)
{
for (auto x : ret)
b.update(x, -1);
}
return ret;
};
go(0, 1);
cout << ans << endl;
time_taken();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1516 KB |
Output is correct |
2 |
Correct |
4 ms |
1004 KB |
Output is correct |
3 |
Correct |
6 ms |
1516 KB |
Output is correct |
4 |
Correct |
7 ms |
1644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
3692 KB |
Output is correct |
2 |
Correct |
11 ms |
2028 KB |
Output is correct |
3 |
Correct |
29 ms |
4076 KB |
Output is correct |
4 |
Correct |
25 ms |
3308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
6788 KB |
Output is correct |
2 |
Correct |
152 ms |
9708 KB |
Output is correct |
3 |
Correct |
244 ms |
12588 KB |
Output is correct |
4 |
Correct |
264 ms |
13244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1092 ms |
24172 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
167 ms |
17180 KB |
Output is correct |
2 |
Execution timed out |
1091 ms |
20132 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
468 ms |
28140 KB |
Output is correct |
2 |
Correct |
553 ms |
28228 KB |
Output is correct |
3 |
Correct |
332 ms |
26856 KB |
Output is correct |
4 |
Correct |
494 ms |
26528 KB |
Output is correct |
5 |
Correct |
196 ms |
21192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
650 ms |
27428 KB |
Output is correct |
2 |
Execution timed out |
1097 ms |
38124 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
607 ms |
26420 KB |
Output is correct |
2 |
Execution timed out |
1090 ms |
31136 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |