// ~ Be Name Khoda ~ //
#include "wombats.h"
#include <bits/stdc++.h>
//#pragma GCC optimize ("O3")
//#pragma GCC target("avx2")
//#pragma GCC optimize("unroll-loops,Ofast")
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define all(x) x.begin(), x.end()
#define fi first
#define se second
const int maxnR = 5002;
const int maxnt = 700;
const int maxnC = 202;
const int mod = 2e9;
const int lim = 11;
int n, m, h[maxnR][maxnC], v[maxnR][maxnC], chil[maxnt][2];
int dp[2][maxnC][maxnC], opt[maxnC][maxnC], newnode = 1;
struct __dp{
int dp[maxnC][maxnC];
} seg[maxnt];
inline void comb(__dp *ret, int mid, const __dp &a, const __dp &b){
for(int dim = -(m - 1); dim < m; dim++) for(int i = max(0, -dim); i < min(m, m - dim); i++){
int j = dim + i;
int l = (j ? opt[i][j - 1] : 0), r = (i + 1 < m ? opt[i + 1][j] : m - 1);
ret->dp[i][j] = mod;
for(int k = l; k <= r; k++){
int val = a.dp[i][k] + b.dp[k][j] + v[mid - 1][k];
if(val < ret->dp[i][j]){
opt[i][j] = k;
ret->dp[i][j] = val;
}
}
}
}
inline void cre(__dp *a, int l, int r){
for(int i = 0; i < m; i++) for(int j = 0; j < m; j++)
dp[(l + 1) & 1][i][j] = mod;
for(int i = 0; i < m; i++)
dp[(l + 1) & 1][i][i] = 0;
for(int j = l; j < r; j++) for(int x = 0; x < m; x++){
int mn = mod;
for(int i = 0; i < m; i++){
dp[j & 1][x][i] = min(mn, dp[(j + 1) & 1][x][i] + (j > l ? v[j - 1][i] : 0));
mn = dp[j & 1][x][i] + h[j][i];
}
mn = mod;
for(int i = m - 1; i >= 0; i--){
dp[j & 1][x][i] = min(mn, dp[j & 1][x][i]);
if(i)
mn = dp[j & 1][x][i] + h[j][i - 1];
//cout << "check " << j << ' ' << x << ' ' << i << ' ' << dp[j & 1][x][i] << ' ' << mn << endl;
}
}
for(int i = 0; i < m; i++) for(int j = 0; j < m; j++)
a->dp[i][j] = dp[(r - 1) & 1][i][j];
}
void upd(int l, int r, int id, int v){
int mid = (l + r) >> 1;
if(id < mid && chil[v][0])
upd(l, mid, id, chil[v][0]);
if(id >= mid && chil[v][1])
upd(mid, r, id, chil[v][1]);
if(chil[v][0] && chil[v][1])
comb(&seg[v], mid, seg[chil[v][0]], seg[chil[v][1]]);
else
cre(&seg[v], l, r);
}
int build(int l, int r){
if(r - l != n && r - l <= lim)
return 0;
int v = newnode++;
assert(newnode < maxnt);
int mid = (l + r) >> 1;
chil[v][0] = build(l, mid);
chil[v][1] = build(mid, r);
if(chil[v][0] && chil[v][1])
comb(&seg[v], mid, seg[chil[v][0]], seg[chil[v][1]]);
else
cre(&seg[v], l, r);
return v;
}
void init(int R, int C, int H[5000][200], int V[5000][200]) {
n = R;
m = C;
ll sum = 0;
for(int i = 0; i < n; i++) for(int j = 0; j < m; j++){
h[i][j] = H[i][j];
v[i][j] = V[i][j];
sum += v[i][0];
}
build(0, n);
}
void changeH(int p, int q, int w) {
h[p][q] = w;
upd(0, n, p, 1);
}
void changeV(int p, int q, int w) {
v[p][q] = w;
upd(0, n, p, 1);
}
int escape(int v1, int v2) {
return seg[1].dp[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;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
14292 KB |
Output is correct |
2 |
Correct |
6 ms |
14268 KB |
Output is correct |
3 |
Correct |
59 ms |
17092 KB |
Output is correct |
4 |
Correct |
6 ms |
14336 KB |
Output is correct |
5 |
Correct |
6 ms |
14292 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
54 ms |
1420 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
2132 KB |
Output is correct |
2 |
Correct |
48 ms |
2004 KB |
Output is correct |
3 |
Correct |
56 ms |
2132 KB |
Output is correct |
4 |
Correct |
56 ms |
2132 KB |
Output is correct |
5 |
Correct |
56 ms |
2108 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
251 ms |
2096 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
18644 KB |
Output is correct |
2 |
Correct |
9 ms |
18620 KB |
Output is correct |
3 |
Correct |
9 ms |
18652 KB |
Output is correct |
4 |
Correct |
36 ms |
20004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
2132 KB |
Output is correct |
2 |
Correct |
51 ms |
2004 KB |
Output is correct |
3 |
Correct |
56 ms |
2132 KB |
Output is correct |
4 |
Correct |
55 ms |
2132 KB |
Output is correct |
5 |
Correct |
60 ms |
2124 KB |
Output is correct |
6 |
Correct |
8 ms |
18644 KB |
Output is correct |
7 |
Correct |
9 ms |
18644 KB |
Output is correct |
8 |
Correct |
9 ms |
18644 KB |
Output is correct |
9 |
Correct |
37 ms |
20036 KB |
Output is correct |
10 |
Correct |
7 ms |
14292 KB |
Output is correct |
11 |
Correct |
7 ms |
14292 KB |
Output is correct |
12 |
Correct |
60 ms |
17132 KB |
Output is correct |
13 |
Correct |
7 ms |
14292 KB |
Output is correct |
14 |
Correct |
6 ms |
14292 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
55 ms |
2764 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
252 ms |
2204 KB |
Output is correct |
28 |
Correct |
680 ms |
62796 KB |
Output is correct |
29 |
Correct |
617 ms |
59756 KB |
Output is correct |
30 |
Correct |
655 ms |
59496 KB |
Output is correct |
31 |
Correct |
691 ms |
64408 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
2132 KB |
Output is correct |
2 |
Correct |
48 ms |
2004 KB |
Output is correct |
3 |
Correct |
55 ms |
2132 KB |
Output is correct |
4 |
Correct |
59 ms |
2132 KB |
Output is correct |
5 |
Correct |
54 ms |
2004 KB |
Output is correct |
6 |
Correct |
8 ms |
18656 KB |
Output is correct |
7 |
Correct |
12 ms |
18620 KB |
Output is correct |
8 |
Correct |
9 ms |
18644 KB |
Output is correct |
9 |
Correct |
36 ms |
20084 KB |
Output is correct |
10 |
Correct |
6 ms |
14288 KB |
Output is correct |
11 |
Correct |
7 ms |
14292 KB |
Output is correct |
12 |
Correct |
60 ms |
17104 KB |
Output is correct |
13 |
Correct |
7 ms |
14292 KB |
Output is correct |
14 |
Correct |
7 ms |
14320 KB |
Output is correct |
15 |
Correct |
739 ms |
107748 KB |
Output is correct |
16 |
Correct |
2835 ms |
109128 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
308 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
55 ms |
2760 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
230 ms |
2200 KB |
Output is correct |
30 |
Correct |
662 ms |
62740 KB |
Output is correct |
31 |
Correct |
2523 ms |
106492 KB |
Output is correct |
32 |
Correct |
2604 ms |
106500 KB |
Output is correct |
33 |
Correct |
616 ms |
59668 KB |
Output is correct |
34 |
Correct |
2383 ms |
103136 KB |
Output is correct |
35 |
Correct |
650 ms |
59612 KB |
Output is correct |
36 |
Correct |
2493 ms |
103068 KB |
Output is correct |
37 |
Correct |
689 ms |
64456 KB |
Output is correct |
38 |
Correct |
2579 ms |
107072 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
2669 ms |
103236 KB |
Output is correct |