#include<bits/stdc++.h>
using namespace std;
#include "wombats.h"
const int M = 6e7 / 200 / 200;
int T[M + 10][200][200];
int h[5010][210], v[5010][210];
int f[210][210], opt[210][210];
int r, c;
void calc (int l, int r) {
for(int i = 0; i < c; i++) for(int j = 0; j < c; j++) f[i][j] = (i == j ? 0 : 1e9);
for(int row = l; row <= r; row++) {
if (row != l) {
for(int i = 0; i < c; i++) for(int j = 0; j < c; j++) {
f[i][j] += v[row - 1][j];
}
}
for(int i = 0; i < c; i++) {
for(int j = 1; j < c; j++) f[i][j] = min(f[i][j], f[i][j - 1] + h[row][j - 1]);
for(int j = c - 2; j >= 0; j--) f[i][j] = min(f[i][j], f[i][j + 1] + h[row][j]);
}
}
}
void up (int s, int l, int r, int pos) {
// if (s == 2) cout << l << " " << r << endl;
if (s * 2 > M || l == r) {
calc(l, r);
for(int i = 0; i < c; i++) for(int j = 0; j < c; j++) T[s][i][j] = f[i][j];
return;
}
int mid = l + r >> 1;
if (pos <= mid) up(s << 1, l, mid, pos);
if (pos > mid || pos < 0) up(s << 1 | 1, mid + 1, r, pos);
for(int i = c - 1; i >= 0; i--) for(int j = 0; j < c; j++) {
T[s][i][j] = 2e9;
int st = j - 1 >= 0 ? opt[i][j - 1] : 0;
int ed = i + 1 < c ? opt[i + 1][j] : c - 1;
for(int k = st; k <= ed; k++) {
int cost = T[s << 1][i][k] + v[mid][k] + T[s << 1 | 1][k][j];
if (T[s][i][j] > cost) {
T[s][i][j] = cost;
opt[i][j] = k;
}
}
}
}
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 + 1 < c; j++) h[i][j] = H[i][j];
for(int i = 0; i + 1 < r; i++) for(int j = 0; j < c; j++) v[i][j] = V[i][j];
up(1, 0, r - 1, -1);
// cout << T[2][2][1];
// exit(0);
// for(int i = 0; i < r; i++) {
// for(int j = 0; j + 1 < c; j++) cout << h[i][j] << " ";
// cout << endl;
// }
//
// for(int i = 0; i + 1 < r; i++) {
// for(int j = 0; j < c; j++) cout << v[i][j] << " ";
// cout << endl;
// }
}
void changeH(int P, int Q, int W) {
h[P][Q] = W;
up(1, 0, r - 1, P);
}
void changeV(int P, int Q, int W) {
v[P][Q] = W;
up(1, 0, r - 1, P);
}
int escape(int V1, int V2) {
return T[1][V1][V2];
}
#ifdef ngu
#define fail(s, x...) do { \
fprintf(stderr, s "\n", ## x); \
exit(1); \
} while(0)
static int H[5000][200];
static int V[5000][200];
int main() {
freopen ("task.out", "w", stdout);
int R, C, E, P, Q, W, V1, V2, event, i, j;
int res;
FILE *f = fopen("task.inp", "r");
if (!f)
fail("Failed to open input file.");
res = fscanf(f, "%d%d", &R, &C);
for (i = 0; i < R; ++i)
for (j = 0; j < C-1; ++j)
res = fscanf(f, "%d", &H[i][j]);
for (i = 0; i < R-1; ++i)
for (j = 0; j < C; ++j)
res = fscanf(f, "%d", &V[i][j]);
init(R, C, H, V);
res = fscanf(f, "%d", &E);
for (i = 0; i < E; i++) {
res = fscanf(f, "%d", &event);
if (event == 1) {
res = fscanf(f, "%d%d%d", &P, &Q, &W);
changeH(P, Q, W);
} else if (event == 2) {
res = fscanf(f, "%d%d%d", &P, &Q, &W);
changeV(P, Q, W);
} else if (event == 3) {
res = fscanf(f, "%d%d", &V1, &V2);
cout << escape(V1, V2) << endl;
// printf("%d\n", escape(V1, V2));
} else
fail("Invalid event type.");
}
fclose(f);
return 0;
}
#endif // ngu
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 up(int, int, int, int)':
wombats.cpp:37:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
37 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
141 ms |
248756 KB |
Output is correct |
2 |
Correct |
28 ms |
248668 KB |
Output is correct |
3 |
Correct |
78 ms |
250316 KB |
Output is correct |
4 |
Correct |
27 ms |
248752 KB |
Output is correct |
5 |
Correct |
27 ms |
248756 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
2 ms |
6532 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
3 ms |
18780 KB |
Output is correct |
5 |
Correct |
3 ms |
18780 KB |
Output is correct |
6 |
Correct |
3 ms |
18780 KB |
Output is correct |
7 |
Correct |
2 ms |
18780 KB |
Output is correct |
8 |
Correct |
3 ms |
16732 KB |
Output is correct |
9 |
Correct |
2 ms |
16732 KB |
Output is correct |
10 |
Correct |
3 ms |
18776 KB |
Output is correct |
11 |
Correct |
56 ms |
19904 KB |
Output is correct |
12 |
Correct |
3 ms |
18776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
49752 KB |
Output is correct |
2 |
Correct |
51 ms |
49756 KB |
Output is correct |
3 |
Correct |
65 ms |
49744 KB |
Output is correct |
4 |
Correct |
71 ms |
49740 KB |
Output is correct |
5 |
Correct |
68 ms |
49756 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
198 ms |
49744 KB |
Output is correct |
10 |
Correct |
2 ms |
10840 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
252820 KB |
Output is correct |
2 |
Correct |
31 ms |
252764 KB |
Output is correct |
3 |
Correct |
32 ms |
253012 KB |
Output is correct |
4 |
Correct |
55 ms |
253684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
49748 KB |
Output is correct |
2 |
Correct |
52 ms |
49736 KB |
Output is correct |
3 |
Correct |
66 ms |
49756 KB |
Output is correct |
4 |
Correct |
67 ms |
49748 KB |
Output is correct |
5 |
Correct |
62 ms |
49752 KB |
Output is correct |
6 |
Correct |
28 ms |
252864 KB |
Output is correct |
7 |
Correct |
28 ms |
252756 KB |
Output is correct |
8 |
Correct |
28 ms |
252752 KB |
Output is correct |
9 |
Correct |
57 ms |
253908 KB |
Output is correct |
10 |
Correct |
28 ms |
248664 KB |
Output is correct |
11 |
Correct |
27 ms |
248648 KB |
Output is correct |
12 |
Correct |
79 ms |
250384 KB |
Output is correct |
13 |
Correct |
28 ms |
248900 KB |
Output is correct |
14 |
Correct |
27 ms |
248656 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6648 KB |
Output is correct |
18 |
Correct |
3 ms |
18780 KB |
Output is correct |
19 |
Correct |
3 ms |
18908 KB |
Output is correct |
20 |
Correct |
3 ms |
18776 KB |
Output is correct |
21 |
Correct |
3 ms |
18776 KB |
Output is correct |
22 |
Correct |
4 ms |
16728 KB |
Output is correct |
23 |
Correct |
3 ms |
16732 KB |
Output is correct |
24 |
Correct |
3 ms |
18916 KB |
Output is correct |
25 |
Correct |
55 ms |
19868 KB |
Output is correct |
26 |
Correct |
3 ms |
18776 KB |
Output is correct |
27 |
Correct |
200 ms |
49748 KB |
Output is correct |
28 |
Correct |
1063 ms |
253264 KB |
Output is correct |
29 |
Correct |
865 ms |
253280 KB |
Output is correct |
30 |
Correct |
1034 ms |
253260 KB |
Output is correct |
31 |
Correct |
920 ms |
254200 KB |
Output is correct |
32 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
49752 KB |
Output is correct |
2 |
Correct |
51 ms |
49752 KB |
Output is correct |
3 |
Correct |
66 ms |
49756 KB |
Output is correct |
4 |
Correct |
71 ms |
49748 KB |
Output is correct |
5 |
Correct |
67 ms |
49748 KB |
Output is correct |
6 |
Correct |
28 ms |
253272 KB |
Output is correct |
7 |
Correct |
29 ms |
253016 KB |
Output is correct |
8 |
Correct |
28 ms |
252760 KB |
Output is correct |
9 |
Correct |
56 ms |
253780 KB |
Output is correct |
10 |
Correct |
27 ms |
248664 KB |
Output is correct |
11 |
Correct |
27 ms |
248664 KB |
Output is correct |
12 |
Correct |
77 ms |
250452 KB |
Output is correct |
13 |
Correct |
28 ms |
248660 KB |
Output is correct |
14 |
Correct |
28 ms |
248656 KB |
Output is correct |
15 |
Correct |
1783 ms |
252992 KB |
Output is correct |
16 |
Correct |
3561 ms |
255280 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
3 ms |
18780 KB |
Output is correct |
21 |
Correct |
3 ms |
18780 KB |
Output is correct |
22 |
Correct |
3 ms |
18932 KB |
Output is correct |
23 |
Correct |
2 ms |
18780 KB |
Output is correct |
24 |
Correct |
2 ms |
16836 KB |
Output is correct |
25 |
Correct |
2 ms |
16824 KB |
Output is correct |
26 |
Correct |
3 ms |
18780 KB |
Output is correct |
27 |
Correct |
55 ms |
21176 KB |
Output is correct |
28 |
Correct |
3 ms |
18776 KB |
Output is correct |
29 |
Correct |
195 ms |
49804 KB |
Output is correct |
30 |
Correct |
1050 ms |
257084 KB |
Output is correct |
31 |
Correct |
4067 ms |
261220 KB |
Output is correct |
32 |
Correct |
3473 ms |
261528 KB |
Output is correct |
33 |
Correct |
846 ms |
256648 KB |
Output is correct |
34 |
Correct |
3154 ms |
260684 KB |
Output is correct |
35 |
Correct |
950 ms |
256820 KB |
Output is correct |
36 |
Correct |
3684 ms |
260732 KB |
Output is correct |
37 |
Correct |
947 ms |
258708 KB |
Output is correct |
38 |
Correct |
3431 ms |
261908 KB |
Output is correct |
39 |
Correct |
2 ms |
10588 KB |
Output is correct |
40 |
Correct |
3409 ms |
260936 KB |
Output is correct |