Submission #1020422

# Submission time Handle Problem Language Result Execution time Memory
1020422 2024-07-12T04:14:55 Z nathan4690 Tree Rotations (POI11_rot) C++17
36 / 100
1000 ms 65536 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define pll ll
#define ld long double
#define el cout << '\n'
#define f1(i,n) for(ll i=1;i<=n;i++)
#define __file_name ""
using namespace std;
using namespace __gnu_pbds;
#define ordered_set tree<pll, null_type, less<pll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace std;
const ll maxn = 4e5+5, inf=1e18;

ll n,a[maxn];
vector<ll> G[maxn];
ordered_set values[maxn];

ll dfs(ll u, ll p){
    if(a[u]){
        // cout << u << ' ' << a[u];el;
        values[u].insert(a[u]);
        return 0;
    }
    ll resp = 0;
    ll le = 0, ri = 0;
    for(ll c: G[u]){
        if(c != p){
            resp += dfs(c, u);
            if(le == 0) le = c;
            else ri = c;
        }
    }
    if(values[ri].size() < values[le].size()) swap(values[ri], values[le]);
    // le < ri
    ll res1 = 0, res2 = 0;
    // res1: count inversion if le comes before ri
    // res2: count inversions if le comes after ri
    for(auto item: values[le]){
        res1 += values[ri].order_of_key(item);
        res2 += values[ri].size() - values[ri].order_of_key(item+1);
    }
    // cout << u << ' ' << res1 << ' ' << res2;el;
    for(ll c: G[u]){
        if(c != p){
            if(values[c].size() > values[u].size()) values[u].swap(values[c]);
            for(auto item: values[c]){
                values[u].insert(item);
            }
            values[c].clear();
        }
    }
    return resp + min(res1, res2);
}

ll timer = 0;

ll readInput(){
    ll v; cin >> v;
    ll myval = timer;
    timer++;
    if(v){
        a[myval] = v;
    }else{
        ll c1 = readInput(), c2 = readInput();
        G[c1].push_back(myval);
        G[myval].push_back(c1);
        G[c2].push_back(myval);
        G[myval].push_back(c2);
    }
    return myval;
}

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);

    if(fopen(__file_name ".inp", "r")){
        freopen(__file_name ".inp","r",stdin);
        freopen(__file_name ".out","w",stdout);
    }
    // code here
    cin >> n;
    timer = 1;
    ll root = readInput();
    cout << dfs(root, 0);
    return 0;
}

/*
Code by: Nguyen Viet Trung Nhan
Cau Giay Secondary School
*/

Compilation message

rot.cpp: In function 'int main()':
rot.cpp:79:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   79 |         freopen(__file_name ".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
rot.cpp:80:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   80 |         freopen(__file_name ".out","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 31 ms 47196 KB Output is correct
2 Correct 29 ms 47188 KB Output is correct
3 Correct 29 ms 47192 KB Output is correct
4 Correct 29 ms 47196 KB Output is correct
5 Correct 30 ms 47424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 47184 KB Output is correct
2 Correct 30 ms 47444 KB Output is correct
3 Correct 30 ms 47408 KB Output is correct
4 Correct 30 ms 47280 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 47440 KB Output is correct
2 Correct 30 ms 47428 KB Output is correct
3 Correct 31 ms 47452 KB Output is correct
4 Correct 34 ms 47512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 97 ms 49036 KB Output is correct
2 Correct 36 ms 48208 KB Output is correct
3 Correct 91 ms 49212 KB Output is correct
4 Correct 120 ms 49204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1048 ms 52304 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 156 ms 58960 KB Output is correct
2 Execution timed out 1091 ms 58704 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 71 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1073 ms 65536 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 80 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 73 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 71 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -