#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using ld = long double;
using pll = pair<ll, ll>;
// and so on
int arr[400001];
int in[400001];
int out[400001];
vector<vector<int> > mp;
int id = -1;
int fnd(int s, int e, int v) {
return upper_bound(arr + s, arr + e + 1, v) - (arr + s);
}
int parse(int x) {
int c;
cin >> c;
in[x] = x;
if (c == 0) {
int l = parse(++id);
int r = parse(++id);
mp[x].push_back(l);
mp[x].push_back(r);
} else {
arr[x] = c;
}
out[x] = id;
return in[x];
}
ll sol(int x) {
ll ret = 0;
if (arr[x] == 0) {
int a = mp[x][0];
int b = mp[x][1];
ret += sol(a) + sol(b);
if (out[a] - in[a] < out[b] - in[b]) swap(a, b);
ll acnt = 0, bcnt = 0;
int zero_cnt = fnd(in[a], out[a], 0);
int siz = out[a] - in[a] + 1 - zero_cnt;
int pre = in[a];
for (int i = in[b]; i <= out[b]; i++) {
if (!arr[i]) continue;
int idx = fnd(pre, out[a], arr[i] - 1);
int cnt = idx - zero_cnt + (pre - in[a]);
pre = in[a] + idx;
acnt += cnt;
bcnt += siz - cnt;
}
ret += min(acnt, bcnt);
sort(arr + in[x], arr + out[x] + 1);
return ret;
} else {
return 0;
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
mp.resize(2 * N + 1);
int r = parse(++id);
cout << sol(r);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
4 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
138 ms |
980 KB |
Output is correct |
2 |
Correct |
5 ms |
724 KB |
Output is correct |
3 |
Correct |
188 ms |
992 KB |
Output is correct |
4 |
Correct |
147 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1093 ms |
2388 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
5460 KB |
Output is correct |
2 |
Execution timed out |
1090 ms |
6996 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1078 ms |
15968 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1089 ms |
12884 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1093 ms |
21644 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1087 ms |
21076 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1092 ms |
21828 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |