답안 #765874

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
765874 2023-06-25T06:53:16 Z SeDunion Tree Rotations (POI11_rot) C++17
72 / 100
129 ms 11172 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;

void calc(int v) {
    if (l[v] == r[v]) temp += 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);
            temp = 0;
            calc(r[v]);
            add(r[v]);
        } else {
            dfs(l[v], 1);
            dfs(r[v], 0);
            temp = 0;
            calc(l[v]);
            add(l[v]);
        }
        ll cost = min(cnt[l[v]] * 1ll * cnt[r[v]] - temp, 1ll * temp);
        // 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 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 1 ms 340 KB Output is correct
5 Correct 1 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 0 ms 340 KB Output is correct
4 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 2 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 724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 1236 KB Output is correct
2 Correct 7 ms 724 KB Output is correct
3 Correct 22 ms 1428 KB Output is correct
4 Correct 5 ms 1108 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 40 ms 2176 KB Output is correct
2 Correct 20 ms 3004 KB Output is correct
3 Correct 20 ms 3796 KB Output is correct
4 Correct 20 ms 3796 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 6952 KB Output is correct
2 Correct 26 ms 5344 KB Output is correct
3 Correct 43 ms 4308 KB Output is correct
4 Correct 26 ms 3788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 4740 KB Output is correct
2 Correct 39 ms 5484 KB Output is correct
3 Correct 35 ms 7380 KB Output is correct
4 Correct 35 ms 7220 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 129 ms 8368 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 7888 KB Output is correct
2 Incorrect 82 ms 11172 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 80 ms 7996 KB Output isn't correct
2 Halted 0 ms 0 KB -