#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]; ll 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 |
9676 KB |
Output is correct |
3 |
Correct |
5 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9744 KB |
Output is correct |
5 |
Correct |
6 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9704 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 |
9804 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
7 ms |
9932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
10156 KB |
Output is correct |
2 |
Correct |
8 ms |
10000 KB |
Output is correct |
3 |
Correct |
7 ms |
10060 KB |
Output is correct |
4 |
Correct |
7 ms |
10188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
10972 KB |
Output is correct |
2 |
Correct |
15 ms |
10488 KB |
Output is correct |
3 |
Correct |
26 ms |
11468 KB |
Output is correct |
4 |
Correct |
13 ms |
10908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
12620 KB |
Output is correct |
2 |
Correct |
28 ms |
13664 KB |
Output is correct |
3 |
Correct |
32 ms |
14788 KB |
Output is correct |
4 |
Correct |
32 ms |
14916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
18996 KB |
Output is correct |
2 |
Correct |
40 ms |
17220 KB |
Output is correct |
3 |
Correct |
48 ms |
15824 KB |
Output is correct |
4 |
Correct |
40 ms |
15016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
16944 KB |
Output is correct |
2 |
Correct |
56 ms |
17892 KB |
Output is correct |
3 |
Correct |
56 ms |
20204 KB |
Output is correct |
4 |
Correct |
54 ms |
21016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
138 ms |
22148 KB |
Output is correct |
2 |
Correct |
95 ms |
22600 KB |
Output is correct |
3 |
Correct |
87 ms |
22692 KB |
Output is correct |
4 |
Correct |
99 ms |
21988 KB |
Output is correct |
5 |
Correct |
165 ms |
21068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
21492 KB |
Output is correct |
2 |
Correct |
96 ms |
27636 KB |
Output is correct |
3 |
Correct |
108 ms |
26312 KB |
Output is correct |
4 |
Correct |
87 ms |
28532 KB |
Output is correct |
5 |
Correct |
173 ms |
22652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
21572 KB |
Output is correct |
2 |
Correct |
102 ms |
24620 KB |
Output is correct |
3 |
Correct |
135 ms |
23100 KB |
Output is correct |
4 |
Correct |
110 ms |
23376 KB |
Output is correct |
5 |
Correct |
92 ms |
29316 KB |
Output is correct |
6 |
Correct |
184 ms |
23124 KB |
Output is correct |