#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 _cnt = 0;
int arr[200001];
struct segtree{
vector<int> tree[(1<<22)+1];
int siz;
void init(int N){
for(siz=1;siz<N;siz<<=1);
for(int i=0;i<N;i++){
tree[siz+i] = {arr[i]};
}
for(int pos=siz-1;pos;pos--){
tree[pos] = tree[pos*2];
tree[pos].insert(tree[pos].end(),tree[pos*2+1].begin(),tree[pos*2+1].end());
sort(tree[pos].begin(),tree[pos].end());
}
}
int sol(int l, int r, int s, int e, int pos, int v){
if(s<=l && r<=e) return upper_bound(tree[pos].begin(),tree[pos].end(),v) - tree[pos].begin();
if(e<l || r<s) return 0;
return sol(l,(l+r)/2,s,e,pos*2,v)+sol((l+r)/2+1,r,s,e,pos*2+1,v);
}
int sol(int s, int e, int v){
return sol(0,siz-1,s,e,1,v);
}
} tree;
int in[200001];
int out[200001];
vector<vector<int> > mp;
int id = -1;
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 = tree.sol(in[a],out[a],0);
int siz = out[a] - in[a] + 1 - zero_cnt;
for(int i=in[b];i<=out[b];i++){
if(!arr[i]) continue;
int cnt = tree.sol(in[a],out[a],arr[i]-1) - zero_cnt;
//cout<<cnt<<endl;
acnt += cnt;
bcnt += siz - cnt;
}
ret += min(acnt, bcnt);
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);
tree.init(id+1);
cout<<sol(r);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
39 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
35 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
36 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
35 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
39 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
37 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
36 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
34 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
34 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
32 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
34 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |