#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
template<class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
using ll = long long;
using db = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<db, db>;
using vi = vector<int>;
using vb = vector<bool>;
using vl = vector<ll>;
using vd = vector<db>;
using vs = vector<str>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vpd = vector<pd>;
#define mp make_pair
#define f first
#define s second
#define sz(x) (ll)(x).size()
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sor(x) sort(all(x))
#define ft front()
#define bk back()
#define pb push_back
#define pf push_front
#define lb lower_bound
#define ub upper_bound
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) FOR(i, 0, a)
#define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--)
#define R0F(i, a) ROF(i, 0, a)
#define EACH(a, x) for (auto& a : x)
const int MOD = 1e9 + 7;
const int MX = 2e5 + 10;
const ll INF = 1e18;
int N, num1 = 1, num2 = 1; ll ans; vi child[2 * MX];
int A[MX], L[2 * MX], R[2 * MX], cntL[2 * MX], cntR[2 * MX], BIT[MX];
void update(int ind, ll val) {
while (ind <= N) { BIT[ind] += val; ind += (ind & -ind); }
}
ll query(int ind) {
ll res = 0; while (ind > 0) { res += BIT[ind]; ind -= (ind & -ind); } return res;
}
void init(int X) {
int Y; cin >> Y;
if (Y == 0) {
L[X] = ++num1; cntL[X] = num2; init(L[X]);
R[X] = ++num1; cntR[X] = num2; init(R[X]);
}
else cntL[X] = num2, A[num2] = Y, cntR[X] = num2++;
}
void DFS(int X) {
if (cntR[X] - cntL[X] == 0) { update(A[cntL[X]], 1); return ; }
if (cntR[L[X]] - cntL[L[X]] < cntR[R[X]] - cntL[R[X]]) swap(L[X], R[X]);
// cout << X << " " << L[X] << " " << R[X] << endl;
DFS(R[X]); FOR(i, cntL[R[X]], cntR[R[X]] + 1) update(A[i], -1); DFS(L[X]);
ll sum = 0; FOR(i, cntL[R[X]], cntR[R[X]] + 1) sum += query(A[i]);
FOR(i, cntL[R[X]], cntR[R[X]] + 1) update(A[i], 1);
// cout << X << " " << min(sum, (cntR[L[X]] - cntL[L[X]] + 1) * (cntR[R[X]] - cntL[R[X]] + 1) - sum) << endl;
ans += min(sum, (cntR[L[X]] - cntL[L[X]] + 1) * (cntR[R[X]] - cntL[R[X]] + 1) - sum);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N; init(1); DFS(1); cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9716 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
9676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
7 ms |
9676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
10060 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
11 ms |
10700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
12000 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
36 ms |
16568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
50 ms |
14812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
103 ms |
18496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
81 ms |
17736 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
86 ms |
17804 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |