#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;
vector<int> lst[400001];
int id = -1;
int fnd(int idx, int v){
return upper_bound(lst[idx].begin(),lst[idx].end(),v) - lst[idx].begin();
}
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(a, 0);
int siz = lst[a].size();
for(int i=in[b];i<=out[b];i++){
if(!arr[i]) continue;
int cnt = fnd(a,arr[i]) - zero_cnt;
acnt += cnt;
bcnt += siz - cnt;
}
ret += min(acnt, bcnt);
lst[x] = lst[a];
lst[x].insert(lst[x].end(),lst[b].begin(),lst[b].end());
sort(lst[x].begin(),lst[x].end());
lst[a].clear();
lst[b].clear();
return ret;
} else {
lst[x] = {arr[x]};
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 |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
5 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
5 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9940 KB |
Output is correct |
2 |
Correct |
6 ms |
9936 KB |
Output is correct |
3 |
Correct |
6 ms |
9808 KB |
Output is correct |
4 |
Correct |
10 ms |
11220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
184 ms |
44532 KB |
Output is correct |
2 |
Correct |
10 ms |
11108 KB |
Output is correct |
3 |
Correct |
272 ms |
49764 KB |
Output is correct |
4 |
Correct |
127 ms |
59084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
282 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
26868 KB |
Output is correct |
2 |
Runtime error |
127 ms |
65536 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
198 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
216 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
321 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
215 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
209 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |