Submission #603834

# Submission time Handle Problem Language Result Execution time Memory
603834 2022-07-24T12:15:56 Z KoD Wombats (IOI13_wombats) C++17
100 / 100
2861 ms 185492 KB
#include "wombats.h"
#include <bits/stdc++.h>

using std::array;

constexpr int INF = (1 << 30) - 1;
const int MAX_R = 5000;
const int MAX_C = 200;
const int BLOCK = 10;
const int SEG = 512;

template <class T>
bool setmin(T& x, const T& y) {
    if (x > y) {
        x = y;
        return true;
    }
    return false;
}

// grid data
int R, C;
int H[MAX_R][MAX_C];
int V[MAX_R][MAX_C];

// segtree data
array<array<int, MAX_C>, MAX_C> dist[2 * SEG];
int append[2 * SEG];

void calc_group(const int g) {
    const int i0 = BLOCK * g;
    const int i1 = std::min(R, i0 + BLOCK);
    static array<int, MAX_C> accum = {};
    static array<int, MAX_C> next = {};
    const auto setup = [&](const int i) {
        for (int j = 0; j < C - 1; ++j)
            accum[j + 1] = accum[j] + H[i][j];
    };
    setup(i0);
    auto& dp = dist[SEG + g];
    for (int a = 0; a < C; ++a)
        for (int b = a; b < C; ++b)
            dp[a][b] = dp[b][a] = accum[b] - accum[a];
    for (int i = i0; i < i1 - 1; ++i) {
        setup(i + 1);
        for (int a = 0; a < C; ++a) {
            next.fill(INF);
            for (int b = 0, min = INF; b < C; ++b) {
                setmin(min, dp[a][b] + V[i][b] - accum[b]);
                setmin(next[b], min + accum[b]);
            }
            for (int b = C - 1, min = INF; b >= 0; --b) {
                setmin(min, dp[a][b] + V[i][b] + accum[b]);
                setmin(next[b], min - accum[b]);
            }
            dp[a] = next;
        }
    }
}

void fetch(const int g) {
    const int l = 2 * g;
    const int r = 2 * g + 1;
    append[g] = append[r];
    if (append[l] == R - 1) {
        dist[g] = dist[l];
        return;
    }
    auto& dp = dist[g];
    for (int a = 0; a < C; ++a)
        for (int b = 0; b < C; ++b)
            dp[a][b] = INF;
    static array<array<int, MAX_C>, MAX_C> best = {};
    const auto check = [&](const int a, const int c, const int b) {
        if (setmin(dp[a][b], dist[l][a][c] + V[append[l]][c] + dist[r][c][b]))
            best[a][b] = c;
    };
    for (int a = 0; a < C; ++a)
        for (int b = 0; b < C; ++b)
            check(a, b, a);
    for (int len = 1; len < C; ++len) {
        for (int a = 0; a + len < C; ++a)
            for (int b = best[a][a + len - 1]; b <= best[a + 1][a + len]; ++b)
                check(a, b, a + len);
        for (int a = len; a < C; ++a)
            for (int b = best[a - 1][a - len]; b <= best[a][a - len + 1]; ++b)
                check(a, b, a - len);
    }
}

void init(int r, int c, int h[MAX_R][MAX_C], int v[MAX_R][MAX_C]) {
    R = r, C = c;
    std::memcpy(H, h, sizeof(H));
    std::memcpy(V, v, sizeof(V));
    const int n = (R + BLOCK - 1) / BLOCK;
    for (int g = 0; g < n; ++g)
        calc_group(g);
    for (int g = 0; g < SEG; ++g)
        append[SEG + g] = std::min(BLOCK * (g + 1) - 1, R - 1);
    for (int g = SEG - 1; g > 0; --g)
        fetch(g);
}

void changeH(int p, int q, int w) {
    H[p][q] = w;
    const int g = p / BLOCK;
    calc_group(g);
    for (int k = SEG + g; (k >>= 1) > 0;)
        fetch(k);
}

void changeV(int p, int q, int w) {
    V[p][q] = w;
    const int g = p / BLOCK;
    calc_group(g);
    for (int k = SEG + g; (k >>= 1) > 0;)
        fetch(k);
}

int escape(int v0, int v1) {
    return dist[1][v0][v1];
}

Compilation message

grader.c: In function 'int main()':
grader.c:15:6: warning: variable 'res' set but not used [-Wunused-but-set-variable]
   15 |  int res;
      |      ^~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 18516 KB Output is correct
2 Correct 12 ms 18620 KB Output is correct
3 Correct 73 ms 21292 KB Output is correct
4 Correct 10 ms 18648 KB Output is correct
5 Correct 10 ms 18644 KB Output is correct
6 Correct 39 ms 88236 KB Output is correct
7 Correct 43 ms 88268 KB Output is correct
8 Correct 38 ms 88308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 88284 KB Output is correct
2 Correct 40 ms 88196 KB Output is correct
3 Correct 42 ms 88232 KB Output is correct
4 Correct 38 ms 88140 KB Output is correct
5 Correct 44 ms 88248 KB Output is correct
6 Correct 41 ms 88240 KB Output is correct
7 Correct 39 ms 88328 KB Output is correct
8 Correct 38 ms 88180 KB Output is correct
9 Correct 39 ms 88256 KB Output is correct
10 Correct 40 ms 88152 KB Output is correct
11 Correct 103 ms 90572 KB Output is correct
12 Correct 39 ms 88144 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 110 ms 88884 KB Output is correct
2 Correct 106 ms 88740 KB Output is correct
3 Correct 117 ms 88768 KB Output is correct
4 Correct 115 ms 88776 KB Output is correct
5 Correct 114 ms 88752 KB Output is correct
6 Correct 39 ms 88260 KB Output is correct
7 Correct 39 ms 88268 KB Output is correct
8 Correct 39 ms 88196 KB Output is correct
9 Correct 316 ms 88856 KB Output is correct
10 Correct 39 ms 88272 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 23364 KB Output is correct
2 Correct 13 ms 23284 KB Output is correct
3 Correct 14 ms 23360 KB Output is correct
4 Correct 45 ms 24652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 120 ms 88788 KB Output is correct
2 Correct 111 ms 88688 KB Output is correct
3 Correct 127 ms 88768 KB Output is correct
4 Correct 114 ms 88776 KB Output is correct
5 Correct 124 ms 88752 KB Output is correct
6 Correct 19 ms 23228 KB Output is correct
7 Correct 13 ms 23252 KB Output is correct
8 Correct 14 ms 23364 KB Output is correct
9 Correct 45 ms 24628 KB Output is correct
10 Correct 10 ms 18532 KB Output is correct
11 Correct 11 ms 18644 KB Output is correct
12 Correct 73 ms 21404 KB Output is correct
13 Correct 11 ms 18624 KB Output is correct
14 Correct 10 ms 18560 KB Output is correct
15 Correct 45 ms 88268 KB Output is correct
16 Correct 39 ms 88268 KB Output is correct
17 Correct 40 ms 88224 KB Output is correct
18 Correct 38 ms 88188 KB Output is correct
19 Correct 38 ms 88192 KB Output is correct
20 Correct 40 ms 88180 KB Output is correct
21 Correct 39 ms 88228 KB Output is correct
22 Correct 42 ms 88372 KB Output is correct
23 Correct 40 ms 88168 KB Output is correct
24 Correct 42 ms 88152 KB Output is correct
25 Correct 105 ms 90516 KB Output is correct
26 Correct 46 ms 88140 KB Output is correct
27 Correct 315 ms 88776 KB Output is correct
28 Correct 692 ms 104328 KB Output is correct
29 Correct 818 ms 102012 KB Output is correct
30 Correct 797 ms 101912 KB Output is correct
31 Correct 746 ms 105996 KB Output is correct
32 Correct 39 ms 88240 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 108 ms 88752 KB Output is correct
2 Correct 100 ms 88656 KB Output is correct
3 Correct 126 ms 88780 KB Output is correct
4 Correct 115 ms 88676 KB Output is correct
5 Correct 115 ms 88640 KB Output is correct
6 Correct 13 ms 23252 KB Output is correct
7 Correct 17 ms 23432 KB Output is correct
8 Correct 16 ms 23352 KB Output is correct
9 Correct 47 ms 24652 KB Output is correct
10 Correct 10 ms 18516 KB Output is correct
11 Correct 10 ms 18552 KB Output is correct
12 Correct 71 ms 21380 KB Output is correct
13 Correct 10 ms 18652 KB Output is correct
14 Correct 12 ms 18644 KB Output is correct
15 Correct 907 ms 184092 KB Output is correct
16 Correct 2845 ms 185492 KB Output is correct
17 Correct 39 ms 88268 KB Output is correct
18 Correct 39 ms 88320 KB Output is correct
19 Correct 39 ms 88288 KB Output is correct
20 Correct 38 ms 88244 KB Output is correct
21 Correct 38 ms 88220 KB Output is correct
22 Correct 42 ms 88176 KB Output is correct
23 Correct 38 ms 88176 KB Output is correct
24 Correct 39 ms 88172 KB Output is correct
25 Correct 40 ms 88224 KB Output is correct
26 Correct 38 ms 88188 KB Output is correct
27 Correct 98 ms 90636 KB Output is correct
28 Correct 38 ms 88212 KB Output is correct
29 Correct 324 ms 88780 KB Output is correct
30 Correct 730 ms 104296 KB Output is correct
31 Correct 2861 ms 182716 KB Output is correct
32 Correct 2766 ms 182708 KB Output is correct
33 Correct 942 ms 101928 KB Output is correct
34 Correct 2839 ms 166948 KB Output is correct
35 Correct 841 ms 101812 KB Output is correct
36 Correct 2676 ms 166808 KB Output is correct
37 Correct 753 ms 105988 KB Output is correct
38 Correct 2353 ms 183288 KB Output is correct
39 Correct 39 ms 88312 KB Output is correct
40 Correct 2794 ms 166992 KB Output is correct