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;
const int INF = 0x3f3f3f3f;
int R, C;
int sum[205];
int H[5005][205];
int V[5005][205];
int opt[205][205];
struct Node {
int d[205][205];
Node() {
memset(d, INF, sizeof d);
}
};
bool upd(int &x, int y) {
if (x > y) { x = y; return 1; }
return 0;
}
Node merge(Node &y, Node &z, int cost[205]) {
Node x;
for (int i = 0; i < C; ++i) {
for (int j = 0; j < C; ++j) {
if (upd(x.d[i][0], y.d[i][j] + z.d[j][0] + cost[j])) opt[i][0] = j;
}
}
for (int i = 0; i < C; ++i) {
for (int j = 0; j < C; ++j) {
if (upd(x.d[C - 1][i], y.d[C - 1][j] + z.d[j][i] + cost[j])) opt[C - 1][i] = j;
}
}
for (int i = C - 2; i >= 0; --i) {
for (int j = 1; j < C; ++j) {
int l = opt[i][j - 1], r = opt[i + 1][j];
for (int k = l; k <= r; ++k) {
if (upd(x.d[i][j], y.d[i][k] + z.d[k][j] + cost[k])) opt[i][j] = k;
}
}
}
return x;
}
Node cal(int p) {
Node x;
sum[0] = 0;
for (int i = 1; i < C; ++i) {
sum[i] = sum[i - 1] + H[p][i - 1];
}
for (int i = 0; i < C; ++i) {
for (int j = 0; j <= i; ++j) {
x.d[i][j] = x.d[j][i] = sum[i] - sum[j];
}
}
return x;
}
struct SegmentTree {
Node T[1005];
#define mid ((l + r) >> 1)
void build(int i, int l, int r) {
if (r - l <= 20) {
T[i] = cal(l);
for (int j = l; j < r; ++j) {
Node tmp = cal(j + 1);
T[i] = merge(T[i], tmp, V[j]);
}
return;
}
build(i << 1, l, mid);
build(i << 1 | 1, mid + 1, r);
T[i] = merge(T[i << 1], T[i << 1 | 1], V[mid]);
}
void upd(int i, int l, int r, int p) {
if (r - l <= 20) {
T[i] = cal(l);
for (int j = l; j < r; ++j) {
Node tmp = cal(j + 1);
T[i] = merge(T[i], tmp, V[j]);
}
return;
}
if (mid >= p) upd(i << 1, l, mid, p);
else upd(i << 1 | 1, mid + 1, r, p);
T[i] = merge(T[i << 1], T[i << 1 | 1], V[mid]);
}
#undef mid
} IT;
void init(int _R, int _C, int _H[5000][200], int _V[5000][200]) {
R = _R, C = _C;
for (int i = 0; i < R; ++i) {
for (int j = 0; j < (C - 1); ++j) {
H[i][j] = _H[i][j];
}
}
for (int i = 0; i < (R - 1); ++i) {
for (int j = 0; j < C; ++j) {
V[i][j] = _V[i][j];
}
}
IT.build(1, 0, R - 1);
}
void changeH(int P, int Q, int W) {
H[P][Q] = W, IT.upd(1, 0, R - 1, P);
}
void changeV(int P, int Q, int W) {
V[P][Q] = W, IT.upd(1, 0, R - 1, P);
}
int escape(int V1, int V2) {
return IT.T[1].d[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]
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... |