답안 #871582

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
871582 2023-11-11T06:55:01 Z sleepntsheep Tree Rotations (POI11_rot) C++17
36 / 100
1000 ms 15432 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;
    int 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);
    }
    else
    {
        a[u] = a[L[u]];
        for (auto x : *a[R[u]]) a[u]->push_back(x);
    }
}

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;
}


# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
5 Correct 0 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2512 KB Output is correct
3 Correct 0 ms 2396 KB Output is correct
4 Correct 1 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 6 ms 2896 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 177 ms 3156 KB Output is correct
2 Correct 6 ms 2908 KB Output is correct
3 Correct 170 ms 3152 KB Output is correct
4 Correct 192 ms 3276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1006 ms 4476 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 9160 KB Output is correct
2 Execution timed out 1026 ms 6848 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1032 ms 15432 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 14 ms 6492 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 14 ms 6236 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 16 ms 7208 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 15 ms 6748 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -