#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int sz =2e5+1;
const int ssz=5e3+1;
const int mod=1e9+7;
int p,n,q,timer,m,cnt;
long long lb,rb;
queue<int>qe;
int ans[sz];
int lf[sz],rt[sz];
int lc[sz*20],rc[sz*20];
int cap[sz*20];
int val[sz];
int fa[sz];
void read(int &now){
now = ++cnt;
scanf("%d",&val[now]);
if(!val[now]){
read(lf[now]);
read(rt[now]);
}
}
void upd(int pos,int &num,int l = 1, int r = n){
if(!num) num = ++timer;
cap[num] = 1;
if(l == r) return;
int mid = (l + r)>>1;
if(pos <= mid) upd(pos,lc[num],l,mid);
else upd(pos,rc[num],mid + 1, r);
}
int merge(int roota, int rootb){
if(!roota or !rootb) return roota + rootb;
cap[roota]+=cap[rootb];
lb += cap[rc[rootb]] * cap[lc[roota]];
rb += cap[rc[roota]] * cap[lc[rootb]];
lc[roota] = merge(lc[roota],lc[rootb]);
rc[roota] = merge(rc[roota],rc[rootb]);
rootb = 0;
return roota;
}
long long dfs(int u){
long long res = 0;
if(!val[u]){
res = dfs(lf[u]) + dfs(rt[u]);
lb = rb = 0;
fa[u] = merge(fa[lf[u]],fa[rt[u]]);
res = res + min(lb,rb);
}
else upd(val[u],fa[u]);
return res;
}
int main(){
scanf("%d",&n);
int root;
read(root);
long long res = dfs(root);
cout<<res<<endl;
}
Compilation message
rot.cpp: In function 'void read(int&)':
rot.cpp:18:11: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
18 | scanf("%d",&val[now]);
| ~~~~~^~~~~~~~~~~~~~~~
rot.cpp: In function 'int main()':
rot.cpp:54:12: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
5 |
Correct |
1 ms |
340 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 |
1 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 |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1108 KB |
Output is correct |
2 |
Correct |
3 ms |
1108 KB |
Output is correct |
3 |
Correct |
2 ms |
1236 KB |
Output is correct |
4 |
Correct |
2 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
3028 KB |
Output is correct |
2 |
Correct |
8 ms |
2876 KB |
Output is correct |
3 |
Correct |
18 ms |
6868 KB |
Output is correct |
4 |
Correct |
7 ms |
3284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
11852 KB |
Output is correct |
2 |
Correct |
32 ms |
11852 KB |
Output is correct |
3 |
Correct |
34 ms |
13772 KB |
Output is correct |
4 |
Correct |
43 ms |
14812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
22728 KB |
Output is correct |
2 |
Correct |
57 ms |
22152 KB |
Output is correct |
3 |
Correct |
61 ms |
20760 KB |
Output is correct |
4 |
Correct |
55 ms |
21256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
2976 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1064 ms |
47204 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
88 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
69 ms |
65536 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |