Submission #1098529

# Submission time Handle Problem Language Result Execution time Memory
1098529 2024-10-09T13:42:42 Z BLOBVISGOD Tree Rotations (POI11_rot) C++17
81 / 100
346 ms 65536 KB
#include "bits/stdc++.h"
using namespace std;
#define rep(i,a,b) for(int i=(a); i<(b); ++i)
#define all(x) x.begin(),x.end()
#define sz(x) int(x.size())
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_multiset;

ordered_multiset nums[int(4e5)];

int main(){
    cin.tie(NULL),cin.sync_with_stdio(false);
    
    int n; cin >> n;
    int m = n*2-2;

    int cc = 0;
    vvi adj(m+1);

    auto build = [&](int& at, auto&& build) -> int {
        int v; cin >> v;
        if (v==0){
            int my = at;
            adj[my].push_back(build(--at,build));
            adj[my].push_back(build(--at,build));
            return my;
        }else{
            at++;
            return v-1;
        }
    };

    build(m,build);

    auto dfs = [&](int at, auto&& dfs) -> array<ll,2> {
        if (at<n){
            nums[cc].insert(at);
            return {0,cc++};
        }else{
            array<ll,2> inds;
            ll ans = 0;
            rep(i,0,2){
                auto ret = dfs(adj[at][i],dfs);
                ans += ret[0];
                inds[i] = ret[1];
            }

            ll minext = 1e18;
            if (sz(nums[inds[0]]) < sz(nums[inds[1]])) swap(inds[0],inds[1]);

            {
                // 
                ll extl = 0, extr = 0;
                for(auto c : nums[inds[1]]){
                    int smaller = nums[inds[0]].order_of_key(c);
                    extl += sz(nums[inds[0]]) - smaller;
                    extr += smaller;
                }
                minext = min(extl, extr);
            }

            // merge small into large
            for(auto c : nums[inds[1]]) nums[inds[0]].insert(c);
            nums[inds[1]].clear();
            return {ans + minext, inds[0]};
        }
    };

    cout << dfs(n*2-2,dfs)[0] << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 24 ms 31580 KB Output is correct
2 Correct 23 ms 31624 KB Output is correct
3 Correct 23 ms 31580 KB Output is correct
4 Correct 22 ms 31580 KB Output is correct
5 Correct 32 ms 31716 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 31576 KB Output is correct
2 Correct 21 ms 31576 KB Output is correct
3 Correct 23 ms 31576 KB Output is correct
4 Correct 22 ms 31580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 24 ms 31836 KB Output is correct
2 Correct 24 ms 31828 KB Output is correct
3 Correct 25 ms 31836 KB Output is correct
4 Correct 22 ms 31940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 23 ms 32556 KB Output is correct
2 Correct 24 ms 32344 KB Output is correct
3 Correct 29 ms 32596 KB Output is correct
4 Correct 24 ms 32616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 34392 KB Output is correct
2 Correct 35 ms 33368 KB Output is correct
3 Correct 67 ms 36288 KB Output is correct
4 Correct 32 ms 34460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 87 ms 39252 KB Output is correct
2 Correct 66 ms 41388 KB Output is correct
3 Correct 69 ms 43856 KB Output is correct
4 Correct 64 ms 44112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 78 ms 52308 KB Output is correct
2 Correct 81 ms 50084 KB Output is correct
3 Correct 107 ms 45456 KB Output is correct
4 Correct 80 ms 44368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 123 ms 48312 KB Output is correct
2 Correct 121 ms 49740 KB Output is correct
3 Correct 104 ms 57592 KB Output is correct
4 Correct 105 ms 57428 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 243 ms 62376 KB Output is correct
2 Correct 194 ms 57684 KB Output is correct
3 Correct 185 ms 59216 KB Output is correct
4 Correct 207 ms 58448 KB Output is correct
5 Correct 346 ms 57752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 204 ms 59988 KB Output is correct
2 Runtime error 133 ms 65536 KB Execution killed with signal 9
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 242 ms 64748 KB Output is correct
2 Correct 208 ms 61524 KB Output is correct
3 Correct 304 ms 59476 KB Output is correct
4 Correct 263 ms 64592 KB Output is correct
5 Runtime error 103 ms 65536 KB Execution killed with signal 9
6 Halted 0 ms 0 KB -