답안 #615856

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
615856 2022-07-31T13:24:57 Z 1bin Tree Rotations (POI11_rot) C++14
100 / 100
366 ms 23928 KB
#include <bits/stdc++.h>

using namespace std;

#define all(v) v.begin(), v.end()
typedef long long ll;
const int base = 1 << 20;
ll n, a[base], lc[base], rc[base], w[base], t, ans;
ll seg[1 << 21];

void dfs(int x){
    cin >> a[x];
    if(a[x]) w[x] = 1;
    else{
        lc[x] = ++t; dfs(lc[x]);
        rc[x] = ++t; dfs(rc[x]);
        if(w[lc[x]] > w[rc[x]]) swap(lc[x], rc[x]);
        w[x] = w[lc[x]] + w[rc[x]];
    }
    return;
}

void upd(int idx, ll v){
    idx += base;
    while(idx){
        seg[idx] += v; idx /= 2;
    }
    return;
}

ll sum(int l, int r){
    ll ret = 0;
    l += base; r += base;
    while(l <= r){
        if(l & 1) ret += seg[l++];
        if(!(r & 1)) ret += seg[r--];
        l /= 2; r /= 2;
    }
    return ret;
}

void f(int x, int v){
    if(a[x]){
        upd(a[x], v); return;
    }
    f(lc[x], v);
    f(rc[x], v);
    return;
}

ll cal(int x){
    if(a[x]) return sum(a[x] + 1, base - 1);
    return cal(lc[x]) + cal(rc[x]);
}

void dfs2(int x){
    if(a[x]) {
        upd(a[x], 1);
        return;
    }
    ll v = 0;
    dfs2(lc[x]);
    f(lc[x], -1);
    dfs2(rc[x]);
    v += cal(lc[x]);
    f(lc[x], 1);
    ans += min(v, w[lc[x]] * w[rc[x]] - v);
    return;
}

int main(void){
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    cin >> n;
    dfs(++t);
    dfs2(1);
    cout << ans;
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 468 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 1 ms 468 KB Output is correct
4 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 852 KB Output is correct
2 Correct 3 ms 724 KB Output is correct
3 Correct 2 ms 852 KB Output is correct
4 Correct 2 ms 968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 1876 KB Output is correct
2 Correct 13 ms 1408 KB Output is correct
3 Correct 46 ms 2772 KB Output is correct
4 Correct 8 ms 1748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 66 ms 4384 KB Output is correct
2 Correct 36 ms 5104 KB Output is correct
3 Correct 37 ms 6356 KB Output is correct
4 Correct 40 ms 6612 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 34 ms 11084 KB Output is correct
2 Correct 48 ms 9412 KB Output is correct
3 Correct 70 ms 8200 KB Output is correct
4 Correct 52 ms 7260 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 72 ms 9696 KB Output is correct
2 Correct 64 ms 10808 KB Output is correct
3 Correct 62 ms 12768 KB Output is correct
4 Correct 62 ms 13812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 261 ms 16240 KB Output is correct
2 Correct 144 ms 16588 KB Output is correct
3 Correct 115 ms 16452 KB Output is correct
4 Correct 151 ms 15692 KB Output is correct
5 Correct 293 ms 15096 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 125 ms 15308 KB Output is correct
2 Correct 134 ms 21836 KB Output is correct
3 Correct 157 ms 20668 KB Output is correct
4 Correct 114 ms 22604 KB Output is correct
5 Correct 366 ms 17204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 142 ms 15492 KB Output is correct
2 Correct 119 ms 19296 KB Output is correct
3 Correct 239 ms 17648 KB Output is correct
4 Correct 169 ms 17484 KB Output is correct
5 Correct 111 ms 23928 KB Output is correct
6 Correct 334 ms 17704 KB Output is correct