#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[400001];
struct segtree{
vector<int> tree[(1<<20)+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[400001];
int out[400001];
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);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
24916 KB |
Output is correct |
2 |
Correct |
15 ms |
24916 KB |
Output is correct |
3 |
Correct |
15 ms |
24912 KB |
Output is correct |
4 |
Correct |
13 ms |
24880 KB |
Output is correct |
5 |
Correct |
14 ms |
24916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
24916 KB |
Output is correct |
2 |
Correct |
15 ms |
24988 KB |
Output is correct |
3 |
Correct |
14 ms |
24916 KB |
Output is correct |
4 |
Correct |
13 ms |
24904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
25140 KB |
Output is correct |
2 |
Correct |
15 ms |
25224 KB |
Output is correct |
3 |
Correct |
14 ms |
25224 KB |
Output is correct |
4 |
Correct |
16 ms |
25340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
26868 KB |
Output is correct |
2 |
Correct |
23 ms |
26332 KB |
Output is correct |
3 |
Correct |
23 ms |
27092 KB |
Output is correct |
4 |
Correct |
19 ms |
27220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
31076 KB |
Output is correct |
2 |
Correct |
47 ms |
29004 KB |
Output is correct |
3 |
Correct |
118 ms |
35076 KB |
Output is correct |
4 |
Correct |
40 ms |
31012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
42444 KB |
Output is correct |
2 |
Correct |
124 ms |
45980 KB |
Output is correct |
3 |
Correct |
177 ms |
51016 KB |
Output is correct |
4 |
Correct |
151 ms |
51576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
112 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
122 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
96 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
89 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
94 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |