#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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
380 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 |
# |
Verdict |
Execution time |
Memory |
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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
3 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
4 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
728 KB |
Output is correct |
2 |
Correct |
5 ms |
632 KB |
Output is correct |
3 |
Correct |
75 ms |
760 KB |
Output is correct |
4 |
Correct |
92 ms |
760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1079 ms |
1272 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
58 ms |
2936 KB |
Output is correct |
2 |
Execution timed out |
1079 ms |
3292 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1072 ms |
6516 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1076 ms |
6260 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1083 ms |
9836 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1081 ms |
9584 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1075 ms |
9712 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |