Submission #871642

# Submission time Handle Problem Language Result Execution time Memory
871642 2023-11-11T08:22:20 Z sleepntsheep Tree Rotations (POI11_rot) C++17
36 / 100
1000 ms 31864 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 1000005

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using oset = tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update>;

int root, n, L[N], R[N], A[N], alloc;
oset *a[N];
i64 t[N], sz[N];

int read()
{
    int u = ++alloc, p;
    cin >> p;
    if (p) return sz[u] = 1, A[u] = p, u;
    L[u] = read(); R[u] = read();
    return sz[u] = sz[L[u]] + sz[R[u]], 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 dfs0(int u)
{
    if (!L[u]) return void((a[u] = new oset())->insert({A[u], u}));

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


    i64 l2r = 0, r2l = 0;
 
    if (a[R[u]]->size() > a[L[u]]->size())
    {
        for (const pair<int, int> &x : *a[L[u]])
            l2r += a[R[u]]->order_of_key(*a[R[u]]->lower_bound({x.first, 0})),
            r2l += sz[R[u]] - a[R[u]]->order_of_key(*a[R[u]]->lower_bound({x.first+1, 0}));
    }
    else
    {
        for (const pair<int, int> &x : *a[R[u]])
            l2r += sz[L[u]] - a[L[u]]->order_of_key(*a[L[u]]->lower_bound({x.first+1, 0})),
            r2l += a[L[u]]->order_of_key(*a[L[u]]->lower_bound({x.first, 0}));
    }

    cerr << l2r<<' ' <<r2l<<endl;
    r2l=l2r=0;
 
    for (auto x : *a[L[u]]) upd(x.first, 1);
    for (auto x : *a[R[u]]) l2r += qry(x.first + 1);
    for (auto x : *a[L[u]]) upd(x.first, -1);
    for (auto x : *a[R[u]]) upd(x.first, 1);
    for (auto x : *a[L[u]]) r2l += qry(x.first + 1);
    for (auto x : *a[R[u]]) upd(x.first, -1);

    cerr << l2r<<' ' <<r2l<<endl;
    cerr<< endl;

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

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


vector<int> corona;

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

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

    dfs0(root);
    dfs2(root);

    u64 z{0};

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

    cout << z;
    return 0;
}


Compilation message

In file included from /usr/include/c++/10/ext/pb_ds/detail/pat_trie_/pat_trie_.hpp:45,
                 from /usr/include/c++/10/ext/pb_ds/detail/container_base_dispatch.hpp:90,
                 from /usr/include/c++/10/ext/pb_ds/assoc_container.hpp:48,
                 from rot.cpp:20:
rot.cpp: In function 'void dfs0(int)':
rot.cpp:100:17: warning: comparison of integer expressions of different signedness: 'i64' {aka 'long long int'} and '__gnu_pbds::detail::bin_search_tree_set<std::pair<int, int>, __gnu_pbds::null_type, std::less<std::pair<int, int> >, __gnu_pbds::detail::tree_traits<std::pair<int, int>, __gnu_pbds::null_type, std::less<std::pair<int, int> >, __gnu_pbds::tree_order_statistics_node_update, __gnu_pbds::rb_tree_tag, std::allocator<char> >, std::allocator<char> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  100 |     assert(sz[u]==a[u]->size());
      |            ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8540 KB Output is correct
3 Correct 1 ms 8540 KB Output is correct
4 Correct 1 ms 8540 KB Output is correct
5 Correct 1 ms 8788 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 3 ms 8536 KB Output is correct
3 Correct 3 ms 8540 KB Output is correct
4 Correct 3 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 8764 KB Output is correct
2 Correct 12 ms 8796 KB Output is correct
3 Correct 12 ms 8796 KB Output is correct
4 Correct 19 ms 8840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 422 ms 9540 KB Output is correct
2 Correct 60 ms 8920 KB Output is correct
3 Correct 357 ms 9616 KB Output is correct
4 Correct 277 ms 9700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1067 ms 13736 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 778 ms 16244 KB Output is correct
2 Execution timed out 1069 ms 15888 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1057 ms 31864 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1067 ms 13472 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1034 ms 27096 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1082 ms 22008 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1053 ms 23548 KB Time limit exceeded
2 Halted 0 ms 0 KB -