This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "wombats.h"
#include <bits/stdc++.h>
using namespace std;
int block_size;
int num_blocks;
int global_H[5000][200];
int global_V[5000][200];
int qsr[5000][200];
int C;
class BlockAnswer {
public:
vector<vector<int>> ans;
int rs, rt;
BlockAnswer(int r1, int r2) {
rs = r1;
rt = r2;
ans.resize(C);
for (int i = 0; i < C; i++) {
ans[i].resize(C, 1000000000);
}
}
virtual void update_i(int col) {
vector<int> dpst;
dpst.resize(C, 1000000000);
dpst[col] = 0;
for (int row = rs; row <= rt; row++) {
vector<int> dpnw;
dpnw.resize(C, 1000000000);
int pref = 1000000000;
for (int nc = 0; nc < C; nc++) {
pref = min(pref, dpst[nc] - qsr[row][nc]);
dpnw[nc] =
min(dpnw[nc], pref + qsr[row][nc] + global_V[row][nc]);
}
pref = 1000000000;
for (int nc = C - 1; nc >= 0; nc--) {
pref = min(pref, dpst[nc] + qsr[row][nc]);
dpnw[nc] =
min(dpnw[nc], pref - qsr[row][nc] + global_V[row][nc]);
}
swap(dpst, dpnw);
}
ans[col] = dpst;
}
void update() {
for (int i = 0; i < C; i++) {
update_i(i);
}
}
};
vector<BlockAnswer> blans;
class GlobalAnswer : public BlockAnswer {
public:
GlobalAnswer() : BlockAnswer(0, 0) {}
void update_i(int col) override {
vector<int> dpst;
dpst.resize(C, 1000000000);
dpst[col] = 0;
for (int blk = 0; blk < num_blocks; blk++) {
vector<int> dpnw;
dpnw.resize(C, 1000000000);
for (int nc = 0; nc < C; nc++) {
for (int oc = 0; oc < C; oc++) {
dpnw[nc] = min(dpnw[nc], dpst[oc] + blans[blk].ans[oc][nc]);
}
}
swap(dpst, dpnw);
}
ans[col] = dpst;
}
};
GlobalAnswer *glob;
void init(int R, int C, int H[5000][200], int V[5000][200]) {
block_size = sqrt(R * C);
num_blocks = (R + block_size - 1) / block_size;
::C = C;
memcpy(global_H, H, 5000 * 200 * 4);
memcpy(global_V, V, 5000 * 200 * 4);
for (int j = 0; j < C; j++)
global_V[R - 1][j] = 0;
for (int j = 0; j < R; j++) {
qsr[j][0] = 0;
for (int i = 0; i < C - 1; i++) {
qsr[j][i + 1] = qsr[j][i] + global_H[j][i];
}
}
for (int i = 0; i < num_blocks; i++) {
int r1 = i * block_size;
int r2 = min((i + 1) * block_size - 1, R - 1);
blans.emplace_back(r1, r2);
blans.back().update();
}
glob = new GlobalAnswer();
glob->update();
}
void changeH(int P, int Q, int W) {
global_H[P][Q] = W;
for (int i = 0; i < C - 1; i++) {
qsr[P][i + 1] = qsr[P][i] + global_H[P][i];
}
blans[P / block_size].update();
glob->update();
}
void changeV(int P, int Q, int W) {
global_V[P][Q] = W;
blans[P / block_size].update();
glob->update();
}
int escape(int V1, int V2) { return glob->ans[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;
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |