답안 #612960

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
612960 2022-07-30T02:35:56 Z 1bin Tree Rotations (POI11_rot) C++17
63 / 100
106 ms 65536 KB
#include <bits/stdc++.h>

using namespace std;

#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 2e6 + 5;
int n, t, a[NMAX], lc[NMAX], rc[NMAX];
int rt[NMAX], l[NMAX], r[NMAX], s[NMAX], 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;
}
# 결과 실행 시간 메모리 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 328 KB Output is correct
5 Correct 1 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 1 ms 340 KB Output is correct
4 Correct 1 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 2 ms 472 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1200 KB Output is correct
2 Correct 4 ms 1184 KB Output is correct
3 Correct 3 ms 1236 KB Output is correct
4 Correct 2 ms 1364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 3152 KB Output is correct
2 Correct 8 ms 3020 KB Output is correct
3 Correct 21 ms 7180 KB Output is correct
4 Correct 9 ms 3412 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 39 ms 12320 KB Output is correct
2 Correct 29 ms 12376 KB Output is correct
3 Correct 46 ms 14332 KB Output is correct
4 Correct 50 ms 15228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 97 ms 23464 KB Output is correct
2 Correct 70 ms 22716 KB Output is correct
3 Correct 69 ms 21308 KB Output is correct
4 Correct 67 ms 21920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 73 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 95 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 100 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 106 ms 65536 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -