Submission #612983

# Submission time Handle Problem Language Result Execution time Memory
612983 2022-07-30T02:41:52 Z 1bin Tree Rotations (POI11_rot) C++14
81 / 100
250 ms 53324 KB
#include <bits/stdc++.h>

using namespace std;

#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 4e5 + 5;
int n, t, a[NMAX], lc[NMAX], rc[NMAX], rt[NMAX];
int l[NMAX * 30], r[NMAX * 30], s[NMAX * 30], cnt;
ll ans, r1, r2;

void dfs(int x){
    cin >> a[x];
    if(!a[x]){
        lc[x] = ++t; dfs(lc[x]);
        rc[x] = ++t; dfs(rc[x]);
    }
    return;
}

int upd(int le, int ri, int k){
    int ix = ++cnt;
    s[ix] = 1;
    if(le == ri) return ix;
    int m = (le + ri) / 2;
    if(k <= m) l[ix] = upd(le, m, k);
    else r[ix] = upd(m + 1, ri, k);
    return ix;
}

int merge(int x, int y){
    if(!x) return y;
    if(!y) return x;
    r1 += s[l[x]] * s[r[y]];
    r2 += s[l[y]] * s[r[x]];
    l[x] = merge(l[x], l[y]);
    r[x] = merge(r[x], r[y]);
    s[x] = s[l[x]] + s[r[x]];
    return x;
}

ll dfs2(int x){
    if(a[x]) return 0;
    ll ret = dfs2(lc[x]) + dfs2(rc[x]);
    r1 = r2 = 0;
    rt[x] = merge(rt[lc[x]], rt[rc[x]]);
    ret += min(r1, r2);
    return ret;
}

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    cin >> n;
    dfs(++t);
    
    for(int i = 1; i <= t; i++) 
        if(a[i]) rt[i] = upd(1, n, a[i]);
    cout << dfs2(1);
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 324 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 456 KB Output is correct
2 Correct 2 ms 488 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 3 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1236 KB Output is correct
2 Correct 4 ms 1072 KB Output is correct
3 Correct 3 ms 1236 KB Output is correct
4 Correct 4 ms 1364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 3024 KB Output is correct
2 Correct 13 ms 2896 KB Output is correct
3 Correct 27 ms 6996 KB Output is correct
4 Correct 10 ms 3456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 12032 KB Output is correct
2 Correct 32 ms 12104 KB Output is correct
3 Correct 61 ms 14024 KB Output is correct
4 Correct 53 ms 14964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 82 ms 22948 KB Output is correct
2 Correct 63 ms 22220 KB Output is correct
3 Correct 85 ms 20920 KB Output is correct
4 Correct 80 ms 21236 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 136 ms 29744 KB Output is correct
2 Correct 141 ms 30396 KB Output is correct
3 Correct 104 ms 32732 KB Output is correct
4 Correct 139 ms 32368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 189 ms 44888 KB Output is correct
2 Correct 131 ms 45644 KB Output is correct
3 Correct 227 ms 44604 KB Output is correct
4 Correct 177 ms 45116 KB Output is correct
5 Correct 193 ms 43644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 236 ms 48260 KB Output is correct
2 Incorrect 192 ms 53324 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 250 ms 50700 KB Output isn't correct
2 Halted 0 ms 0 KB -