#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define pii pair<int, int>
#define p3i pair<pii, int>
#define pll pair<ll, ll>
#define p3l pair<pll, ll>
#define lseg L, (L+R)/2, N*2+1
#define rseg (L+R)/2+1, R, N*2+2
#define ub upper_bound
#define lb lower_bound
#define pq priority_queue
#define MN 1000000007
#define fox(k, x) for (int k=0; k<x; ++k)
#define fox1(k, x) for (int k=1; k<=x; ++k)
#define foxr(k, x) for (int k=x-1; k>=0; --k)
#define fox1r(k, x) for (int k=x; k>0; --k)
#define ms multiset
#define flood(x) memset(x, 0x3f3f3f3f, sizeof x)
#define drain(x) memset(x, 0, sizeof x)
#define rng() ((rand() << 14)+rand())
#define scan(X) do{while((X=getchar())<'0'); for(X-='0'; '0'<=(_=getchar()); X=(X<<3)+(X<<1)+_-'0');}while(0)
char _;
#define pi 3.14159265358979323846
int bit[200005], a[200005], sub[400005], n, t, p, lft[400005], rit[400005];
void add(int P, int V){
for(; P<=200000; P+=P&-P) bit[P]+=V;
}
int sum(int P, int C=0){
for(; P>0; P-=P&-P) C+=bit[P];
return C;
}
int read(){
int t;
cin >> t;
if (t!=0){
sub[t]=1;
return t;
}
t=(p++);
lft[t]=read();
rit[t]=read();
sub[t]=1+sub[lft[t]]+sub[rit[t]];
}
void clr(int N){
if (N<=n){
if (a[N]){
a[N]=0;
add(N, -1);
}
return;
}
clr(lft[N]);
clr(rit[N]);
}
ll ans, c, c2;
void query(int N){
if (N<=n){
c+=sum(N);
c2+=sum(200000);
return;
}
query(lft[N]);
query(rit[N]);
}
void on(int N){
if (N<=n){
a[N]=1;
add(N, 1);
return;
}
on(lft[N]);
on(rit[N]);
}
void dfs(int N){
if (N<=n){
a[N]=1;
add(N, 1);
return;
}
int a, b;
if (sub[lft[N]]>sub[rit[N]]){
a=lft[N]; b=rit[N];
} else {
a=rit[N]; b=lft[N];
}
dfs(b); clr(b);
dfs(a);
c=c2=0;
query(b); on(b);
ans+=min(c, c2-c);
}
int main(){
cin >> n;
p=n+1;
read();
dfs(n+1);
cout << ans;
return 0;
}
Compilation message
rot.cpp: In function 'int read()':
rot.cpp:49:1: warning: control reaches end of non-void function [-Wreturn-type]
}
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
2 ms |
600 KB |
Output is correct |
4 |
Correct |
2 ms |
712 KB |
Output is correct |
5 |
Correct |
2 ms |
712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
712 KB |
Output is correct |
2 |
Correct |
2 ms |
740 KB |
Output is correct |
3 |
Correct |
2 ms |
760 KB |
Output is correct |
4 |
Correct |
2 ms |
760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
760 KB |
Output is correct |
2 |
Correct |
3 ms |
776 KB |
Output is correct |
3 |
Correct |
3 ms |
800 KB |
Output is correct |
4 |
Correct |
2 ms |
824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
976 KB |
Output is correct |
2 |
Correct |
6 ms |
1008 KB |
Output is correct |
3 |
Correct |
5 ms |
1160 KB |
Output is correct |
4 |
Correct |
5 ms |
1192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1608 KB |
Output is correct |
2 |
Correct |
15 ms |
1608 KB |
Output is correct |
3 |
Correct |
42 ms |
2032 KB |
Output is correct |
4 |
Correct |
13 ms |
2032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
3008 KB |
Output is correct |
2 |
Correct |
48 ms |
3964 KB |
Output is correct |
3 |
Correct |
50 ms |
5044 KB |
Output is correct |
4 |
Correct |
52 ms |
5316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
8160 KB |
Output is correct |
2 |
Correct |
70 ms |
8160 KB |
Output is correct |
3 |
Correct |
88 ms |
8160 KB |
Output is correct |
4 |
Correct |
71 ms |
8160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
111 ms |
9552 KB |
Output is correct |
2 |
Correct |
114 ms |
10860 KB |
Output is correct |
3 |
Correct |
102 ms |
13160 KB |
Output is correct |
4 |
Correct |
101 ms |
14032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
263 ms |
16216 KB |
Output is correct |
2 |
Correct |
173 ms |
17008 KB |
Output is correct |
3 |
Correct |
166 ms |
18580 KB |
Output is correct |
4 |
Correct |
183 ms |
19724 KB |
Output is correct |
5 |
Correct |
287 ms |
20608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
182 ms |
23164 KB |
Output is correct |
2 |
Correct |
191 ms |
26972 KB |
Output is correct |
3 |
Correct |
223 ms |
27556 KB |
Output is correct |
4 |
Correct |
162 ms |
30428 KB |
Output is correct |
5 |
Correct |
336 ms |
30428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
204 ms |
31040 KB |
Output is correct |
2 |
Correct |
194 ms |
33240 KB |
Output is correct |
3 |
Correct |
270 ms |
34128 KB |
Output is correct |
4 |
Correct |
208 ms |
36160 KB |
Output is correct |
5 |
Correct |
165 ms |
40368 KB |
Output is correct |
6 |
Correct |
372 ms |
40368 KB |
Output is correct |