#include "wombats.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 5000 + 7;
const int M = 200 + 7;
const int INF = (int) 1e9 + 7;
const int BUCKETSIZE = 10;
int H[N][M];
int V[N][M];
int n;
int m;
struct Node {
int l;
int r;
vector<vector<int>> cost;
Node() :
l(0),
r(0),
cost(m, vector<int> (m, INF)) {
}
};
vector<Node> tree;
void placeSmall(Node &guy, int l, int r) {
guy.l = l;
guy.r = r;
for (int start = 0; start < m; start++) {
for (int col = 0; col < m; col++) {
guy.cost[start][col] = INF;
}
guy.cost[start][start] = 0;
for (int col = 1; col < m; col++) {
guy.cost[start][col] = min(guy.cost[start][col], guy.cost[start][col - 1] + H[l][col - 1]);
}
for (int col = m - 2; col >= 0; col--){
guy.cost[start][col] = min(guy.cost[start][col], guy.cost[start][col + 1] + H[l][col]);
}
}
for (int row = l + 1; row <= r; row++) {
/// add row j
for (int start = 0; start < m; start++) {
for (int col = 0; col < m; col++) {
guy.cost[start][col] += V[row - 1][col];
}
for (int col = 1; col < m; col++) {
guy.cost[start][col] = min(guy.cost[start][col], guy.cost[start][col - 1] + H[row][col - 1]);
}
for (int col = m - 2; col >= 0; col--){
guy.cost[start][col] = min(guy.cost[start][col], guy.cost[start][col + 1] + H[row][col]);
}
}
}
}
int A[M][M];
int B[M][M];
int opt[M][M];
void join(Node& solution, Node& lft, Node& rgh) {
solution.l = lft.l;
solution.r = rgh.r;
for (int i = 0; i < m; i++) {
for (int j = 0; j < m; j++) {
A[i][j] = lft.cost[i][j] + V[lft.r][j];
B[i][j] = rgh.cost[i][j];
solution.cost[i][j] = INF;
opt[i][j] = -1;
}
}
for (int i = 0; i < m; i++) {
for (int k = m - 1; k >= 0; k--) {
int nd = m - 1, st = 0;
if (k + 1 < m) {
nd = opt[i][k + 1];
}
if (i - 1 >= 0) {
st = opt[i - 1][k];
}
for (int j = st; j <= nd; j++) {
if (A[i][j] + B[j][k] < solution.cost[i][k]) {
solution.cost[i][k] = A[i][j] + B[j][k];
opt[i][k] = j;
}
}
}
}
}
void update(int v, int tl, int tr, int pos) {
int LEN = tr - tl + 1;
if (LEN <= BUCKETSIZE) {
placeSmall(tree[v], tl, tr);
return;
}
int tm = (tl + tr) / 2;
if (pos <= tm) {
update(2 * v, tl, tm, pos);
} else {
update(2 * v + 1, tm + 1, tr, pos);
}
join(tree[v], tree[2 * v], tree[2 * v + 1]);
}
void build(int v, int tl, int tr) {
while (v >= (int) tree.size()) tree.push_back({});
if (tr - tl + 1 <= BUCKETSIZE) {
placeSmall(tree[v], tl, tr);
return;
}
int tm = (tl + tr) / 2;
build(2 * v, tl, tm);
build(2 * v + 1, tm + 1, tr);
join(tree[v], tree[2 * v], tree[2 * v + 1]);
}
void init(int R, int C, int Hinit[5000][200], int Vinit[5000][200]) {
n = R;
m = C;
for (int i = 0; i < R; i++) for (int j = 0; j < C; j++) H[i][j] = Hinit[i][j], V[i][j] = Vinit[i][j];
build(1, 0, n - 1);
}
void changeH(int P, int Q, int W) {
H[P][Q] = W;
update(1, 0, n - 1, P);
}
void changeV(int P, int Q, int W) {
V[P][Q] = W;
update(1, 0, n - 1, P);
}
int escape(int V1, int V2) {
return tree[1].cost[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 |
6 ms |
12364 KB |
Output is correct |
2 |
Correct |
8 ms |
12364 KB |
Output is correct |
3 |
Correct |
78 ms |
14008 KB |
Output is correct |
4 |
Correct |
6 ms |
12364 KB |
Output is correct |
5 |
Correct |
6 ms |
12364 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
68 ms |
1388 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
2248 KB |
Output is correct |
2 |
Correct |
89 ms |
2252 KB |
Output is correct |
3 |
Correct |
90 ms |
2252 KB |
Output is correct |
4 |
Correct |
92 ms |
2252 KB |
Output is correct |
5 |
Correct |
105 ms |
2260 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
370 ms |
2260 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16332 KB |
Output is correct |
2 |
Correct |
9 ms |
16332 KB |
Output is correct |
3 |
Correct |
11 ms |
16408 KB |
Output is correct |
4 |
Correct |
41 ms |
17080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
2252 KB |
Output is correct |
2 |
Correct |
82 ms |
2252 KB |
Output is correct |
3 |
Correct |
90 ms |
2252 KB |
Output is correct |
4 |
Correct |
90 ms |
2260 KB |
Output is correct |
5 |
Correct |
86 ms |
2252 KB |
Output is correct |
6 |
Correct |
9 ms |
16332 KB |
Output is correct |
7 |
Correct |
8 ms |
16308 KB |
Output is correct |
8 |
Correct |
8 ms |
16332 KB |
Output is correct |
9 |
Correct |
54 ms |
17128 KB |
Output is correct |
10 |
Correct |
7 ms |
12364 KB |
Output is correct |
11 |
Correct |
6 ms |
12364 KB |
Output is correct |
12 |
Correct |
71 ms |
13932 KB |
Output is correct |
13 |
Correct |
6 ms |
12364 KB |
Output is correct |
14 |
Correct |
6 ms |
12416 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
1 ms |
332 KB |
Output is correct |
22 |
Correct |
0 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
90 ms |
1420 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
388 ms |
2252 KB |
Output is correct |
28 |
Correct |
1183 ms |
61004 KB |
Output is correct |
29 |
Correct |
1061 ms |
58164 KB |
Output is correct |
30 |
Correct |
1119 ms |
58172 KB |
Output is correct |
31 |
Correct |
1253 ms |
61980 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
2252 KB |
Output is correct |
2 |
Correct |
80 ms |
2208 KB |
Output is correct |
3 |
Correct |
89 ms |
2252 KB |
Output is correct |
4 |
Correct |
92 ms |
2252 KB |
Output is correct |
5 |
Correct |
90 ms |
2244 KB |
Output is correct |
6 |
Correct |
9 ms |
16284 KB |
Output is correct |
7 |
Correct |
9 ms |
16288 KB |
Output is correct |
8 |
Correct |
10 ms |
16332 KB |
Output is correct |
9 |
Correct |
41 ms |
17180 KB |
Output is correct |
10 |
Correct |
6 ms |
12364 KB |
Output is correct |
11 |
Correct |
6 ms |
12364 KB |
Output is correct |
12 |
Correct |
71 ms |
13948 KB |
Output is correct |
13 |
Correct |
6 ms |
12364 KB |
Output is correct |
14 |
Correct |
6 ms |
12364 KB |
Output is correct |
15 |
Correct |
1895 ms |
185448 KB |
Output is correct |
16 |
Correct |
4677 ms |
187492 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
296 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
440 KB |
Output is correct |
21 |
Correct |
1 ms |
440 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
332 KB |
Output is correct |
25 |
Correct |
1 ms |
432 KB |
Output is correct |
26 |
Correct |
1 ms |
436 KB |
Output is correct |
27 |
Correct |
66 ms |
1596 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
376 ms |
2348 KB |
Output is correct |
30 |
Correct |
1174 ms |
61252 KB |
Output is correct |
31 |
Correct |
4493 ms |
186140 KB |
Output is correct |
32 |
Correct |
4651 ms |
186060 KB |
Output is correct |
33 |
Correct |
1069 ms |
58412 KB |
Output is correct |
34 |
Correct |
4162 ms |
183276 KB |
Output is correct |
35 |
Correct |
1133 ms |
58324 KB |
Output is correct |
36 |
Correct |
4098 ms |
183240 KB |
Output is correct |
37 |
Correct |
1230 ms |
62164 KB |
Output is correct |
38 |
Correct |
4467 ms |
186760 KB |
Output is correct |
39 |
Correct |
0 ms |
332 KB |
Output is correct |
40 |
Correct |
4307 ms |
183292 KB |
Output is correct |