답안 #129898

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
129898 2019-07-13T13:26:28 Z alexpetrescu Tree Rotations (POI11_rot) C++14
0 / 100
103 ms 65536 KB
#include <cstdio>
#include <vector>
#include <algorithm>

//FILE *fin = fopen("a.in", "r"), *fout = fopen("a.out", "w");
#define fin stdin
#define fout stdout

#define LOGN 18

long long ans;

struct myc {
    int sz;
    std::vector < int > v[LOGN + 1];
};

inline long long calc(myc &a, myc &b) {
    long long ans = 0;
    for (int i = 0; (1 << i) <= a.sz; i++) {
        for (auto &x : a.v[i]) {
            for (int j = 0; (1 << j) <= b.sz; j++) {
                if (!b.v[j].empty() && b.v[j][0] < x) {
                    int rez = 0;
                    for (int pas = (1 << j) / 2; pas; pas >>= 1)
                        if (b.v[j][rez + pas] < x)
                            rez += pas;
                    ans += rez + 1;
                }
            }
        }
    }
    return ans;
}

inline void goMerge(std::vector < int > &a, std::vector < int > &b) {
    if (a.empty()) {
        a = b;
        return ;
    }
    if (b.empty())
        return ;
    int i = 0, j = 0, k = 0;
    std::vector < int > c(a.size() + b.size());
    while (i < (int)a.size() && j < (int)b.size()) {
        if (a[i] < b[j])
            c[k++] = a[i++];
        else
            c[k++] = b[j++];
    }
    while (i < (int)a.size())
        c[k++] = a[i++];
    while (j < (int)b.size())
        c[k++] = b[j++];
    a = c;
}

myc dfs() {
    int x;
    fscanf(fin, "%d", &x);

    myc a;

    if (x) {
        a.sz = 1;
        a.v[0].push_back(x);
        return a;
    }

    myc b = dfs();
    myc c = dfs();

    long long r;
    if (b.sz < c.sz)
        r = calc(b, c);
    else
        r = calc(c, b);
    ans += std::min(r, 1LL * b.sz * c.sz - r);

    a.sz = b.sz + c.sz;
    std::vector < int > tr;
    for (int i = 0; !tr.empty() || ((1 << i) <= (int)b.sz && (1 << i) <= (int)c.sz); i++) {
        goMerge(tr, b.v[i]);
        goMerge(tr, c.v[i]);
        if (a.sz & (1 << i)) {
            for (int j = 0; j < (1 << i); j++) {
                a.v[i].push_back(tr.back());
                tr.pop_back();
            }
            std::reverse(a.v[i].begin(), a.v[i].end());
        }
    }
    return a;
}

int main() {
    int degeaba;
    fscanf(fin, "%d", &degeaba);

    myc a = dfs();

    fprintf(fout, "%lld\n", ans);

    return 0;
}

Compilation message

rot.cpp: In function 'myc dfs()':
rot.cpp:60:11: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf(fin, "%d", &x);
     ~~~~~~^~~~~~~~~~~~~~~
rot.cpp: In function 'int main()':
rot.cpp:98:11: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     fscanf(fin, "%d", &degeaba);
     ~~~~~~^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Runtime error 90 ms 65536 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 43 ms 37112 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 60 ms 53604 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 38 ms 31736 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 504 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 50 ms 44024 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 76 ms 65536 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 35 ms 29604 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 3 ms 504 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 103 ms 65536 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 68 ms 55244 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -