Submission #871589

# Submission time Handle Problem Language Result Execution time Memory
871589 2023-11-11T07:00:42 Z sleepntsheep Tree Rotations (POI11_rot) C++17
36 / 100
1000 ms 14704 KB
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
#include <deque>
#include <set>
#include <utility>
#include <array>

using i64 = long long;
using u64 = unsigned long long;
using f64 = double;
using f80 = long double;

using namespace std;
#define ALL(x) x.begin(), x.end()
#define ShinLena cin.tie(nullptr)->sync_with_stdio(false);
#define N 200005

int root, n, L[N], R[N], A[N], alloc;
vector<int> *a[N];
i64 t[N];

int read()
{
    int u = ++alloc, p;
    cin >> p;
    if (p) return A[u] = p, u;
    L[u] = read();
    R[u] = read();
    return u;
}

static inline i64 qry(u64 p)
{
    u64 z{0};
    for (; p < N; p+=p&-p) z += t[p];
    return z;
}

static inline void upd(u64 p, i64 k)
{
    for (; p; p-=p&-p) t[p] += k;
}

void dfs(int u)
{
    if (!L[u]) return void(a[u] = new vector<int>({A[u]}));

    dfs(L[u]), dfs(R[u]);

    u64 l2r = 0, r2l = 0;

    for (auto x : *a[L[u]]) upd(x, 1);
    for (auto x : *a[R[u]]) l2r += qry(x + 1);
    for (auto x : *a[L[u]]) upd(x, -1);
    for (auto x : *a[R[u]]) upd(x, 1);
    for (auto x : *a[L[u]]) r2l += qry(x + 1);
    for (auto x : *a[R[u]]) upd(x, -1);

    if (l2r > r2l) swap(L[u], R[u]);

    if (a[R[u]]->size() > a[L[u]]->size())
    {
        a[u] = a[R[u]];
        for (auto x : *a[L[u]]) a[u]->push_back(x);
        delete a[L[u]];
    }
    else
    {
        a[u] = a[L[u]];
        for (auto x : *a[R[u]]) a[u]->push_back(x);
        delete a[R[u]];
    }
}

vector<int> corona;

void dfs1(int u)
{
    if (!L[u]) corona.push_back(A[u]);
    else dfs1(L[u]), dfs1(R[u]);
}

int main()
{
    ShinLena;
    cin >> n;
    root = read();

    dfs(root);
    dfs1(root);

    u64 z{0};

    for (auto x : corona) upd(x, 1), z += qry(x+1);

    cout << z;
    return 0;
}


# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 0 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2568 KB Output is correct
4 Correct 5 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 185 ms 3008 KB Output is correct
2 Correct 6 ms 2648 KB Output is correct
3 Correct 171 ms 2908 KB Output is correct
4 Correct 190 ms 3164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1052 ms 4188 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 93 ms 4492 KB Output is correct
2 Execution timed out 1043 ms 6228 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1051 ms 14704 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 18 ms 5468 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 13 ms 5212 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 17 ms 6260 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 14 ms 5820 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -