Submission #680074

#TimeUsernameProblemLanguageResultExecution timeMemory
680074bashkortWombats (IOI13_wombats)C++17
100 / 100
9524 ms68780 KiB
#include <bits/stdc++.h>
#include "wombats.h"

using namespace std;

inline bool ckmin(int &a, int b) {
    return (a > b) && (a = b, true);
}

constexpr int B = 70, R = 5000, C = 200, K = (R + B - 1) / B + 1;
constexpr int N = K * 2;

int r, c, need, sz = 1;
int dp[2 * N][C][C], dist[K][C][C], H[R][C], V[R][C], opt[C][C];


void pull(int x) {
    memset(dp[x], 0x3f, sizeof(dp[x]));

    if (dp[x << 1 | 1][0][0] == -1) {
        memcpy(dp[x], dp[x << 1], sizeof(dp[x]));
        return;
    }

    for (int a = 0; a < c; ++a) {
        for (int b = c - 1; b >= 0; --b) {
            int lx = (a == 0 ? 0 : opt[a - 1][b]);
            int rx = (b == c - 1 ? c - 1 : opt[a][b + 1]);

            for (int mid = lx; mid <= rx; ++mid) {
                if (ckmin(dp[x][a][b], dp[x << 1][a][mid] + dp[x << 1 | 1][mid][b])) {
                    opt[a][b] = mid;
                }
            }
        }
    }
}

void updateDist(int L) {
    if (L < 0) {
        return;
    }

    int lim = max(0, B * L - 1);

    if (lim < r) {
        memset(dist[L], 0x3f, sizeof(dist[L]));
        for (int a = 0; a < c; ++a) {
            dist[L][a][a] = 0;
        }

        for (int i = min(r, B * (L + 1)) - 1; i >= lim; --i) {
            for (int b = 0; b < c; ++b) {
                for (int a = 0; a < c - 1; ++a) {
                    ckmin(dist[L][a + 1][b], dist[L][a][b] + H[i][a]);
                }
                for (int a = c - 2; a >= 0; --a) {
                    ckmin(dist[L][a][b], dist[L][a + 1][b] + H[i][a]);
                }

                if (i != lim) {
                    for (int a = 0; a < c; ++a) {
                        dist[L][a][b] += V[i - 1][a];
                    }
                }
            }
        }

        memcpy(dp[sz + L], dist[L], sizeof(dp[sz + L]));
    } else {
        memset(dp[sz + L], -1, sizeof(dp[sz + L]));
    }

    for (int x = sz + L; x >>= 1;) {
        pull(x);
    }
}

void init(int R, int C, int H[5000][200], int V[5000][200]) {
    r = R, c = C;
    memcpy(::H, H, sizeof(::H)), memcpy(::V, V, sizeof(::V));

    need = r / B + 1;
    sz = 1 << __lg(need) + bool(need & (need - 1));
    for (int i = 0; i < sz; ++i) {
        updateDist(i);
    }
}

void changeH(int P, int Q, int W) {
    H[P][Q] = W;

    updateDist(P / B);
    if ((P + 1) % B == 0 && P / B + 1 < need) {
        updateDist(P / B + 1);
    }
}

void changeV(int P, int Q, int W) {
    V[P][Q] = W;

    updateDist((P + 1) / B);
}

int escape(int V1, int V2) {
    return dp[1][V1][V2];
}

Compilation message (stderr)

grader.c: In function 'int main()':
grader.c:15:6: warning: variable 'res' set but not used [-Wunused-but-set-variable]
   15 |  int res;
      |      ^~~
wombats.cpp: In function 'void init(int, int, int (*)[200], int (*)[200])':
wombats.cpp:84:26: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   84 |     sz = 1 << __lg(need) + bool(need & (need - 1));
      |               ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...