#include <bits/stdc++.h>
using namespace std;
const int N = 4e5+7;
int n, t;
int tree[N];
int l[N], r[N], lt[N], rt[N];
vector<int> p;
long long ans;
void add(int v, int x) {
for (; v < N; v|=v+1)
tree[v] += x;
}
int sum(int v) {
int ans = 0;
for (; v >= 0; v = (v&(v+1))-1)
ans += tree[v];
return ans;
}
int dfs() {
int x; cin>>x;
if (x) {
l[t] = r[t] = p.size();
p.push_back(x-1);
} else {
int left = dfs(), right = dfs();
l[t] = min(l[left], l[right]);
r[t] = max(r[left], r[right]);
lt[t] = left; rt[t] = right;
}
return t++;
}
void dfsSolve(int v, bool store) {
if (l[v] == r[v]) {
if (store)
add(p[l[v]], 1);
return;
}
if (r[rt[v]]-l[rt[v]] > r[lt[v]]-l[lt[v]])
swap(lt[v], rt[v]);
dfsSolve(rt[v], 0);
dfsSolve(lt[v], 1);
long long res = 0;
for (int x = l[rt[v]]; x <= r[rt[v]]; x++)
res += sum(p[x]);
ans += min(res, 1LL*(r[rt[v]]-l[rt[v]]+1)*(r[lt[v]]-l[lt[v]]+1)-res);
if (store)
for (int x = l[rt[v]]; x <= r[rt[v]]; x++)
add(p[x], 1);
else
for (int x = l[lt[v]]; x <= r[lt[v]]; x++)
add(p[x], -1);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
ans = t = 0;
dfsSolve(dfs(), 0);
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
380 KB |
Output is correct |
4 |
Correct |
2 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
632 KB |
Output is correct |
2 |
Correct |
4 ms |
632 KB |
Output is correct |
3 |
Correct |
3 ms |
632 KB |
Output is correct |
4 |
Correct |
3 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1144 KB |
Output is correct |
2 |
Correct |
10 ms |
1016 KB |
Output is correct |
3 |
Correct |
21 ms |
1912 KB |
Output is correct |
4 |
Correct |
8 ms |
1272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
2568 KB |
Output is correct |
2 |
Correct |
23 ms |
2932 KB |
Output is correct |
3 |
Correct |
28 ms |
3960 KB |
Output is correct |
4 |
Correct |
27 ms |
4088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
5876 KB |
Output is correct |
2 |
Correct |
37 ms |
5792 KB |
Output is correct |
3 |
Correct |
47 ms |
5108 KB |
Output is correct |
4 |
Correct |
38 ms |
4704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
5360 KB |
Output is correct |
2 |
Correct |
55 ms |
6648 KB |
Output is correct |
3 |
Correct |
48 ms |
7668 KB |
Output is correct |
4 |
Correct |
49 ms |
7796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
8432 KB |
Output is correct |
2 |
Correct |
89 ms |
9332 KB |
Output is correct |
3 |
Correct |
87 ms |
9344 KB |
Output is correct |
4 |
Correct |
98 ms |
8944 KB |
Output is correct |
5 |
Correct |
139 ms |
8556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
8044 KB |
Output is correct |
2 |
Correct |
95 ms |
12016 KB |
Output is correct |
3 |
Correct |
109 ms |
11376 KB |
Output is correct |
4 |
Correct |
82 ms |
12512 KB |
Output is correct |
5 |
Correct |
159 ms |
9712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
8160 KB |
Output is correct |
2 |
Correct |
97 ms |
10848 KB |
Output is correct |
3 |
Correct |
154 ms |
10092 KB |
Output is correct |
4 |
Correct |
101 ms |
9964 KB |
Output is correct |
5 |
Correct |
88 ms |
13236 KB |
Output is correct |
6 |
Correct |
171 ms |
10096 KB |
Output is correct |