#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]);
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);
ans += min(sum, 1LL * (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 |
6 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 |
6 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 |
10 ms |
10700 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
12108 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
44 ms |
16932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
53 ms |
15560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
100 ms |
19524 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
85 ms |
19084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
87 ms |
19076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |