답안 #874210

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
874210 2023-11-16T12:53:44 Z sleepntsheep Tree Rotations (POI11_rot) C++17
63 / 100
158 ms 14420 KB
#include <assert.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define N 200000
#define M (18*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]) return 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; }
    if (B[l] < B[r])
    {
        int y1, y2;
        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
    {
        int y1, y2;
        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]);
    else
    {
        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.cpp: In function 'int read()':
rot.cpp:25:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   25 |     scanf("%d", a+v);
      |     ~~~~~^~~~~~~~~~~
rot.cpp: In function 'int main()':
rot.cpp:104:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  104 |     scanf("%d", &n);
      |     ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
5 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6488 KB Output is correct
2 Correct 1 ms 6488 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 6492 KB Output is correct
2 Correct 1 ms 6492 KB Output is correct
3 Correct 1 ms 6492 KB Output is correct
4 Correct 1 ms 6492 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 6748 KB Output is correct
2 Correct 2 ms 6492 KB Output is correct
3 Correct 2 ms 6776 KB Output is correct
4 Correct 2 ms 6748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 7268 KB Output is correct
2 Correct 6 ms 7000 KB Output is correct
3 Correct 14 ms 7260 KB Output is correct
4 Correct 5 ms 7260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 9816 KB Output is correct
2 Correct 15 ms 8792 KB Output is correct
3 Correct 26 ms 11612 KB Output is correct
4 Correct 20 ms 11612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 52 ms 14420 KB Output is correct
2 Correct 30 ms 13140 KB Output is correct
3 Correct 43 ms 11868 KB Output is correct
4 Correct 158 ms 11348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 17 ms 8540 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 46 ms 13520 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 27 ms 8800 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 74 ms 11856 KB Output isn't correct
2 Halted 0 ms 0 KB -