#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 = 1, R = 5000, C = 200, K = (R + B - 1) / B;
constexpr int N = K * 4;
int r, c, need, sz = 1;
int dp[N][C][C], fr[K][C][C], H[R][C], V[R][C];
void pull(int x) {
memset(dp[x], 0x3f, sizeof(dp[x]));
for (int a = 0; a < c; ++a) {
function<void(int, int, int, int)> solve = [&](int l, int r, int optl, int optr) {
if (l >= r) {
return;
}
int mid = (l + r) >> 1;
int opt = optl;
for (int b = optl; b <= optr; ++b) {
if (ckmin(dp[x][a][mid], dp[x << 1][a][b] + dp[x << 1 | 1][b][mid])) {
opt = b;
}
}
solve(l, mid, optl, opt);
solve(mid + 1, r, opt, optr);
};
solve(0, c, 0, c - 1);
}
}
void update_fr(int L) {
memset(fr[L], 0x3f, sizeof(fr[L]));
for (int a = 0; a < c; ++a) {
fr[L][a][a] = 0;
}
int lim = max(0, B * L - 1);
for (int i = min(r, B * (L + 1)) - 1; i >= lim; --i) {
for (int a = 0; a < c - 1; ++a) {
for (int cc = 0; cc < c; ++cc) {
ckmin(fr[L][a + 1][cc], fr[L][a][cc] + H[i][a]);
}
}
for (int a = c - 2; a >= 0; --a) {
for (int cc = 0; cc < c; ++cc) {
ckmin(fr[L][a][cc], fr[L][a + 1][cc] + H[i][a]);
}
}
if (i != lim) {
for (int cc = 0; cc < c; ++cc) {
for (int a = 0; a < c; ++a) {
fr[L][a][cc] += V[i - 1][a];
}
}
}
}
memcpy(dp[sz + L], fr[L], 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) / B;
sz = 1 << __lg(need) + bool(need & (need - 1));
for (int i = 0; i < need; ++i) {
update_fr(i);
}
}
void changeH(int P, int Q, int W) {
H[P][Q] = W;
int L = P / B;
update_fr(L);
if ((P + 1) % B == 0 && L + 1 < need) {
update_fr(L + 1);
}
}
void changeV(int P, int Q, int W) {
V[P][Q] = W;
int L = (P + 1) / B;
update_fr(L);
}
int escape(int V1, int V2) {
return dp[1][V1][V2];
}
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;
| ^~~
wombats.cpp: In function 'void init(int, int, int (*)[200], int (*)[200])':
wombats.cpp:79:26: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
79 | sz = 1 << __lg(need) + bool(need & (need - 1));
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/cckXu5LV.o: in function `pull(int)':
wombats.cpp:(.text+0x289): relocation truncated to fit: R_X86_64_PC32 against symbol `c' defined in .bss section in /tmp/cckXu5LV.o
wombats.cpp:(.text+0x2fc): relocation truncated to fit: R_X86_64_PC32 against symbol `c' defined in .bss section in /tmp/cckXu5LV.o
wombats.cpp:(.text+0x34a): relocation truncated to fit: R_X86_64_PC32 against symbol `c' defined in .bss section in /tmp/cckXu5LV.o
/tmp/cckXu5LV.o: in function `update_fr(int)':
wombats.cpp:(.text+0x3e4): relocation truncated to fit: R_X86_64_PC32 against symbol `c' defined in .bss section in /tmp/cckXu5LV.o
wombats.cpp:(.text+0x428): relocation truncated to fit: R_X86_64_PC32 against symbol `r' defined in .bss section in /tmp/cckXu5LV.o
/tmp/cckXu5LV.o: in function `init':
wombats.cpp:(.text+0x6bc): relocation truncated to fit: R_X86_64_PC32 against symbol `r' defined in .bss section in /tmp/cckXu5LV.o
wombats.cpp:(.text+0x6ca): relocation truncated to fit: R_X86_64_PC32 against symbol `c' defined in .bss section in /tmp/cckXu5LV.o
wombats.cpp:(.text+0x6ef): relocation truncated to fit: R_X86_64_PC32 against symbol `need' defined in .bss section in /tmp/cckXu5LV.o
wombats.cpp:(.text+0x72c): relocation truncated to fit: R_X86_64_PC32 against symbol `need' defined in .bss section in /tmp/cckXu5LV.o
/tmp/cckXu5LV.o: in function `changeH':
wombats.cpp:(.text+0x774): relocation truncated to fit: R_X86_64_PC32 against symbol `need' defined in .bss section in /tmp/cckXu5LV.o
/tmp/cckXu5LV.o: in function `_GLOBAL__sub_I_r':
wombats.cpp:(.text.startup+0xb): additional relocation overflows omitted from the output
/usr/bin/ld: failed to convert GOTPCREL relocation; relink with --no-relax
collect2: error: ld returned 1 exit status