#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; init(R[X]); cntR[X] = num2;
}
else cntL[X] = num2, A[num2] = Y, cntR[X] = ++num2;
}
void DFS(int X) {
if (cntR[X] - cntL[X] == 1) { 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]]) update(A[i], -1); DFS(L[X]);
ll sum = 0; FOR(i, cntL[R[X]], cntR[R[X]]) sum += query(A[i]);
FOR(i, cntL[R[X]], cntR[R[X]]) 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]]) * (cntR[R[X]] - cntL[R[X]]) - sum);
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N; init(1); DFS(1); cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9716 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9692 KB |
Output is correct |
5 |
Correct |
7 ms |
9712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9736 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9720 KB |
Output is correct |
4 |
Correct |
6 ms |
9724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9932 KB |
Output is correct |
2 |
Correct |
8 ms |
9856 KB |
Output is correct |
3 |
Correct |
7 ms |
10060 KB |
Output is correct |
4 |
Correct |
9 ms |
10060 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
10572 KB |
Output is correct |
2 |
Correct |
13 ms |
10448 KB |
Output is correct |
3 |
Correct |
30 ms |
11128 KB |
Output is correct |
4 |
Correct |
12 ms |
10572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
11596 KB |
Output is correct |
2 |
Correct |
27 ms |
12788 KB |
Output is correct |
3 |
Correct |
30 ms |
13668 KB |
Output is correct |
4 |
Correct |
30 ms |
13752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
16212 KB |
Output is correct |
2 |
Correct |
39 ms |
15536 KB |
Output is correct |
3 |
Correct |
48 ms |
14524 KB |
Output is correct |
4 |
Correct |
40 ms |
13880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
14532 KB |
Output is correct |
2 |
Correct |
52 ms |
16196 KB |
Output is correct |
3 |
Incorrect |
63 ms |
17732 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
132 ms |
18220 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
87 ms |
17524 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
99 ms |
17460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |