#include<iostream>
#include<cstdio>
using namespace std;
const int N=400005;
int sum[4000005],ls[4000005],rs[4000005];
int root[N],l[N],r[N],val[N];
long long ans,s1,s2;
int cnt,n,seg;
void init(int ro){
scanf ("%d",&val[ro]);
if (!val[ro]){
l[ro]=++cnt;
init(l[ro]);
r[ro]=++cnt;
init(r[ro]);
}
}
void pushup(int rt){
sum[rt]=sum[ls[rt]]+sum[rs[rt]];
}
void build(int l,int r,int &rt,int key){
if (!rt)rt=++seg;
if (l==r){sum[rt]++;return;}
int mid=(l+r)>>1;
if (key<=mid)build(l,mid,ls[rt],key);
else build(mid+1,r,rs[rt],key);
pushup(rt);
}
int merge(int r1,int r2){
if (!r1 || !r2)return r1+r2;
s1+=(long long)sum[rs[r1]]*sum[ls[r2]];
s2+=(long long)sum[ls[r1]]*sum[rs[r2]];
ls[r1]=merge(ls[r1],ls[r2]);
rs[r1]=merge(rs[r1],rs[r2]);
pushup(r1);
return r1;
}
void solve(int ro){
if (!ro)return;
solve(l[ro]);solve(r[ro]);
if (!val[ro]){
s1=s2=0;
root[ro]=merge(root[l[ro]],root[r[ro]]);
ans+=min(s1,s2);
}
}
int main (){
scanf ("%d",&n);cnt=1;
init(1);
for (int i=1;i<=cnt;++i)
if (val[i])build(1,n,root[i],val[i]);
solve(1);
printf ("%lld",ans);
return 0;
}
Compilation message
rot.cpp: In function 'void init(int)':
rot.cpp:10:11: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | scanf ("%d",&val[ro]);
| ~~~~~~^~~~~~~~~~~~~~~
rot.cpp: In function 'int main()':
rot.cpp:48:11: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | scanf ("%d",&n);cnt=1;
| ~~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1112 KB |
Output is correct |
2 |
Correct |
2 ms |
1112 KB |
Output is correct |
3 |
Correct |
3 ms |
1372 KB |
Output is correct |
4 |
Correct |
2 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
2908 KB |
Output is correct |
2 |
Correct |
6 ms |
2908 KB |
Output is correct |
3 |
Correct |
17 ms |
7260 KB |
Output is correct |
4 |
Correct |
6 ms |
3416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
12380 KB |
Output is correct |
2 |
Correct |
29 ms |
12120 KB |
Output is correct |
3 |
Correct |
36 ms |
13660 KB |
Output is correct |
4 |
Correct |
28 ms |
14684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
22308 KB |
Output is correct |
2 |
Correct |
46 ms |
22096 KB |
Output is correct |
3 |
Correct |
57 ms |
21084 KB |
Output is correct |
4 |
Correct |
43 ms |
21816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
30544 KB |
Output is correct |
2 |
Correct |
96 ms |
30288 KB |
Output is correct |
3 |
Correct |
74 ms |
31832 KB |
Output is correct |
4 |
Correct |
80 ms |
31412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
108 ms |
45552 KB |
Output is correct |
2 |
Correct |
118 ms |
45512 KB |
Output is correct |
3 |
Correct |
153 ms |
44400 KB |
Output is correct |
4 |
Correct |
119 ms |
45040 KB |
Output is correct |
5 |
Correct |
122 ms |
44008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
135 ms |
49312 KB |
Output is correct |
2 |
Correct |
122 ms |
52416 KB |
Output is correct |
3 |
Correct |
165 ms |
51284 KB |
Output is correct |
4 |
Correct |
131 ms |
52652 KB |
Output is correct |
5 |
Correct |
151 ms |
50516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
161 ms |
52000 KB |
Output is correct |
2 |
Correct |
161 ms |
52988 KB |
Output is correct |
3 |
Correct |
153 ms |
52252 KB |
Output is correct |
4 |
Correct |
166 ms |
52108 KB |
Output is correct |
5 |
Correct |
167 ms |
55360 KB |
Output is correct |
6 |
Correct |
133 ms |
52064 KB |
Output is correct |