Submission #765781

# Submission time Handle Problem Language Result Execution time Memory
765781 2023-06-25T05:10:13 Z vjudge1 Tree Rotations (POI11_rot) C++17
0 / 100
79 ms 9668 KB
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <bitset>
#include <cmath>
#include <queue>
#include <map>
#include <set>

// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ent "\n"

const int maxn = 1e6 + 100;
const ll INF = (ll)2e18;
const int inf = (ll)2e9;
const int maxl = 26;
const int MOD = 1e9 + 7;

int n, szv;
int L[maxn];
int R[maxn];
int sz[maxn];
int cnt[maxn];
int t[maxn];
ll dp[maxn];

void calc(int v){
    if(v <= n){
        sz[v] = 1;
        cnt[v] = 1;
        return;
    }
    calc(L[v]);
    calc(R[v]);
    sz[v] = sz[L[v]] + sz[R[v]] + 1;
    cnt[v] = cnt[L[v]] + cnt[R[v]];
}

void upd(int i, int x){
    for(; i <= n; i |= (i + 1)) t[i] += x;
}

int get(int i){
    int res = 0;
    for(; i > 0; i = (i & (i + 1)) - 1) res += t[i];
    return res;
}

ll ans(int v){
    if(v <= n) return get(v);
    return ans(L[v]) + ans(R[v]);
}

void updv(int v, int d){
    if(v <= n) upd(v, d);
    else updv(L[v], d), updv(R[v], d);
}

void dfs(int v, int clear){
    if(v <= n){
        if(clear) return;
        upd(v, 1);
    } else{
        if(sz[L[v]] < sz[R[v]]) swap(L[v], R[v]);
        dfs(L[v], 0);
        dfs(R[v], 1);
        ll cal = ans(R[v]);
        dp[v] = dp[L[v]] + dp[R[v]] + min(cal, 1ll * cnt[L[v]] * cnt[R[v]] - cal);
        if(!clear) updv(R[v], 1);
        else updv(L[v], -1);
    }
}

int rec(){
    int x; cin >> x;
    if(!x){
        int v = ++szv;
        L[v] = rec();
        R[v] = rec();
        return v;
    } else return x;
}

void test(){
    cin >> n;
    szv = n;
    rec();
    calc(n + 1);
    dfs(n + 1, 0);
    cout << dp[n + 1];
}

int main(){
    // freopen("cows.in", "r", stdin);
    // freopen("cows.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    int t; t = 1;
    while(t--) test();
}
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 600 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 1284 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 2456 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 17 ms 7512 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 30 ms 5684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 79 ms 9668 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 51 ms 9420 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 58 ms 9524 KB Output isn't correct
2 Halted 0 ms 0 KB -