답안 #765878

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
765878 2023-06-25T06:55:58 Z vjudge1 Tree Rotations (POI11_rot) C++17
100 / 100
158 ms 12104 KB
#include <iostream>
#include <vector>
#include <cmath>

using namespace std;
using ll = long long;

const int N = 1e6 + 123;

int l[N], r[N], temp, sz[N], cnt[N];

int build() {
    int v = temp++;
    int x;
    cin >> x;
    if (x) {
        sz[v] = cnt[v] = 1;
        l[v] = r[v] = x;
        // cout << v << " " << l[v] << " " << r[v] << endl;
        return v;
    }
    l[v] = build();
    r[v] = build();
    sz[v] = sz[l[v]] + sz[r[v]] + 1;
    cnt[v] = cnt[l[v]] + cnt[r[v]];
        // cout << v << " " << l[v] << " " << r[v] << endl;
    return v;
}

int f[N];

void upd(int v, int t) {
    while (v < N) f[v] += t, v |= v + 1;
}

int get(int v) {
    int r = 0;
    while (v >= 0) r += f[v], v &= v + 1, v--;
    return r;
}

void clr(int v) {
    if (l[v] == r[v]) upd(l[v], -1);
    else clr(l[v]), clr(r[v]);
}

ll ans, res;

void calc(int v) {
    if (l[v] == r[v]) res += get(l[v]);
    else calc(l[v]), calc(r[v]);
}

void add(int v) {
    if (l[v] == r[v]) upd(l[v], +1);
    else add(l[v]), add(r[v]);
}

void dfs(int v, int type) {
    // return;
    // cout << v << endl;
    if (l[v] != r[v]) {
        if (sz[l[v]] > sz[r[v]]) {
            dfs(r[v], 1);
            dfs(l[v], 0);
            res = 0;
            calc(r[v]);
            add(r[v]);
        } else {
            dfs(l[v], 1);
            dfs(r[v], 0);
            res = 0;
            calc(l[v]);
            add(l[v]);
        }
        ll cost = min(cnt[l[v]] * 1ll * cnt[r[v]] - res, 1ll * res);
        // cout << v << " " << cost << " " << temp << " cost" << endl;
        ans += cost;
    } else {
        upd(l[v], +1);
    }
    if (type == 1) {
        clr(v);
    }
}

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0);
    int n;
    cin >> n;
    dfs(build(), 0);
    cout << ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 0 ms 388 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 0 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 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 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 596 KB Output is correct
2 Correct 2 ms 468 KB Output is correct
3 Correct 1 ms 596 KB Output is correct
4 Correct 1 ms 596 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 1236 KB Output is correct
2 Correct 7 ms 724 KB Output is correct
3 Correct 20 ms 1364 KB Output is correct
4 Correct 5 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 2132 KB Output is correct
2 Correct 18 ms 3004 KB Output is correct
3 Correct 20 ms 3796 KB Output is correct
4 Correct 20 ms 3764 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 6904 KB Output is correct
2 Correct 26 ms 5332 KB Output is correct
3 Correct 35 ms 4308 KB Output is correct
4 Correct 27 ms 3784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 37 ms 4768 KB Output is correct
2 Correct 34 ms 5500 KB Output is correct
3 Correct 34 ms 7440 KB Output is correct
4 Correct 40 ms 7240 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 116 ms 8272 KB Output is correct
2 Correct 72 ms 7500 KB Output is correct
3 Correct 59 ms 7628 KB Output is correct
4 Correct 81 ms 7352 KB Output is correct
5 Correct 127 ms 6364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 7820 KB Output is correct
2 Correct 66 ms 11176 KB Output is correct
3 Correct 77 ms 9936 KB Output is correct
4 Correct 56 ms 11860 KB Output is correct
5 Correct 152 ms 7192 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 76 ms 7964 KB Output is correct
2 Correct 61 ms 8604 KB Output is correct
3 Correct 112 ms 7456 KB Output is correct
4 Correct 81 ms 7988 KB Output is correct
5 Correct 52 ms 12104 KB Output is correct
6 Correct 158 ms 7416 KB Output is correct