답안 #129897

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
129897 2019-07-13T13:17:14 Z alexpetrescu Tree Rotations (POI11_rot) C++14
45 / 100
1000 ms 65540 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; (1 << i) <= a.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 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 8 ms 888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 38 ms 3832 KB Output is correct
2 Correct 8 ms 504 KB Output is correct
3 Correct 46 ms 3960 KB Output is correct
4 Correct 54 ms 4856 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 322 ms 13176 KB Output is correct
2 Correct 21 ms 688 KB Output is correct
3 Correct 45 ms 1088 KB Output is correct
4 Correct 171 ms 9228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 85 ms 1912 KB Output is correct
2 Execution timed out 1073 ms 23948 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 88 ms 65540 KB Execution killed with signal 9 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1068 ms 6488 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1068 ms 49980 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1082 ms 19484 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1077 ms 15696 KB Time limit exceeded
2 Halted 0 ms 0 KB -