#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define N 1000000
#define M (20*N)
#define INLINE inline __attribute__((always_inline))
unsigned seed = 0x86868686;
INLINE unsigned rand_(void)
{
return (seed << 3) ^ 0x3119475;
}
int n, id, a[N+1], b[N+1], c[N+1];
int A[M], B[M], L[M], R[M], S[M], alloc;
int read(void)
{
int v = ++id;
scanf("%d", a+v);
if (!a[v]) b[v] = read(), c[v] = read();
return v;
}
INLINE int treap0(int k)
{
A[++alloc] = k; B[alloc] = rand_(); S[alloc] = 1;
return alloc;
}
INLINE void pull(int v)
{
S[v] = 1 + S[L[v]] + S[R[v]];
}
void split(int v, int *l, int *r, int val)
{
if (!v) { *l=*r=0; return; }
if (A[v] <= val)
{
split(R[v], &R[v], r, val);
*l = v;
}
else
{
split(L[v], l, &L[v], val);
*r = v;
}
pull(v);
}
long long inv, ans;
void merge(int *v, int l, int r)
{
if (!l || !r) { *v = l^r; return; }
int y1, y2;
if (B[l] < B[r])
{
split(l, &y1, &y2, A[r]);
inv += 1ll * S[y2] * (1+S[L[r]]);
merge(R+r, R[r], y2);
merge(L+r, L[r], y1);
*v = r;
}
else
{
split(r, &y1, &y2, A[l]);
inv += 1ll * S[y1] * (1+S[R[l]]);
merge(R+l, R[l], y2);
merge(L+l, L[l], y1);
*v = l;
}
pull(*v);
}
int dfs(int u)
{
if (a[u]) return treap0(a[u]);
int lt = dfs(b[u]);
int rt = dfs(c[u]);
int v; long long c2 = 1ll*S[lt]*S[rt];
inv = 0;
merge(&v, lt, rt);
ans += (inv > c2 - inv) ? c2 - inv : inv;
return v;
}
int main()
{
scanf("%d", &n);
int root = read();
dfs(root);
printf("%lld", ans);
return 0;
}
Compilation message
rot.c: In function 'read':
rot.c:25:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
25 | scanf("%d", a+v);
| ^~~~~~~~~~~~~~~~
rot.c: In function 'main':
rot.c:98:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10584 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10840 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
12892 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12892 KB |
Output is correct |
4 |
Correct |
2 ms |
12888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
13144 KB |
Output is correct |
2 |
Correct |
8 ms |
12892 KB |
Output is correct |
3 |
Correct |
15 ms |
12892 KB |
Output is correct |
4 |
Correct |
5 ms |
13148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
13148 KB |
Output is correct |
2 |
Correct |
15 ms |
14172 KB |
Output is correct |
3 |
Correct |
28 ms |
14852 KB |
Output is correct |
4 |
Correct |
20 ms |
14872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
19360 KB |
Output is correct |
2 |
Correct |
31 ms |
17752 KB |
Output is correct |
3 |
Correct |
44 ms |
16644 KB |
Output is correct |
4 |
Correct |
156 ms |
15996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
16212 KB |
Output is correct |
2 |
Correct |
63 ms |
16980 KB |
Output is correct |
3 |
Correct |
46 ms |
18824 KB |
Output is correct |
4 |
Correct |
45 ms |
18772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
18724 KB |
Output is correct |
2 |
Correct |
51 ms |
17808 KB |
Output is correct |
3 |
Correct |
91 ms |
18060 KB |
Output is correct |
4 |
Correct |
143 ms |
17492 KB |
Output is correct |
5 |
Correct |
89 ms |
16552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
17744 KB |
Output is correct |
2 |
Correct |
78 ms |
21108 KB |
Output is correct |
3 |
Correct |
112 ms |
19812 KB |
Output is correct |
4 |
Correct |
72 ms |
21584 KB |
Output is correct |
5 |
Correct |
106 ms |
16976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
17488 KB |
Output is correct |
2 |
Correct |
115 ms |
18128 KB |
Output is correct |
3 |
Correct |
109 ms |
16980 KB |
Output is correct |
4 |
Correct |
104 ms |
17748 KB |
Output is correct |
5 |
Correct |
115 ms |
21788 KB |
Output is correct |
6 |
Correct |
73 ms |
16984 KB |
Output is correct |