#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 = 200, R = 5000, C = 200, K = (R + B - 1) / B;
int r, c, need;
int dp[K][C][C], fr[K][C][C], H[R][C], V[R][C];
void update_dp(int L) {
if (L == 0) {
memcpy(dp[L], fr[L], sizeof(dp[L]));
return;
}
memset(dp[L], 0x3f, sizeof(dp[L]));
for (int cc = 0; cc < c; ++cc) {
const int idx = L * B - 1;
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[L][cc][mid], dp[L - 1][cc][b] + V[idx][b] + fr[L][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 = B * L;
vector<int> lazy(c);
for (int i = min(r, B * (L + 1)) - 1; i >= lim; --i) {
vector<int> pref(c);
for (int x = 0; x < c - 1; ++x) {
pref[x + 1] = pref[x] + H[i][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(fr[L][a][mid], fr[L][b][mid] + lazy[b] - lazy[a] + abs(pref[a] - pref[b]))) {
opt = b;
}
}
solve(l, mid, optl, opt), solve(mid + 1, r, opt, optr);
};
solve(0, c, 0, c - 1);
}
if (i != lim) {
for (int a = 0; a < c; ++a) {
lazy[a] += V[i - 1][a];
}
}
}
for (int a = 0; a < c; ++a) {
for (int cc = 0; cc < c; ++cc) {
fr[L][a][cc] += lazy[a];
}
}
}
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;
for (int i = 0; i < need; ++i) {
update_fr(i);
}
for (int i = 0; i < need; ++i) {
update_dp(i);
}
}
void changeH(int P, int Q, int W) {
H[P][Q] = W;
int L = P / B;
update_fr(L);
for (int i = L; i < need; ++i) {
update_dp(i);
}
}
void changeV(int P, int Q, int W) {
V[P][Q] = W;
int L = P / B;
if ((P + 1) % B != 0) {
update_fr(L);
}
for (int i = L; i < need; ++i) {
update_dp(i);
}
}
int escape(int V1, int V2) {
return dp[need - 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;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
19868 KB |
Output is correct |
2 |
Correct |
29 ms |
19796 KB |
Output is correct |
3 |
Correct |
119 ms |
21420 KB |
Output is correct |
4 |
Correct |
57 ms |
19964 KB |
Output is correct |
5 |
Correct |
77 ms |
19952 KB |
Output is correct |
6 |
Correct |
6 ms |
8456 KB |
Output is correct |
7 |
Correct |
5 ms |
8404 KB |
Output is correct |
8 |
Correct |
5 ms |
8404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
8404 KB |
Output is correct |
2 |
Correct |
4 ms |
8404 KB |
Output is correct |
3 |
Correct |
5 ms |
8432 KB |
Output is correct |
4 |
Incorrect |
5 ms |
8404 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4124 ms |
8620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
23768 KB |
Output is correct |
2 |
Correct |
97 ms |
23768 KB |
Output is correct |
3 |
Correct |
78 ms |
23768 KB |
Output is correct |
4 |
Correct |
142 ms |
24516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4067 ms |
8620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4250 ms |
8624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |