#include<bits/stdc++.h>
#include"wombats.h"
using namespace std;
const int rmax=5e3+5,cmax=2e2+5,block_size=10,inf=1e9;
int r,c,h[rmax][cmax],v[rmax][cmax];
int tree[(1<<10)+42][cmax][cmax];
int current[cmax][cmax],help[cmax][cmax];
int pref[cmax];
int opt[cmax][cmax];
void brute_one(int i,int j,int k)
{
for(int p=0;p<c;p++)
{
int cur=current[j][p];
cur=cur+abs(pref[p]-pref[k]);
cur+=v[i-1][p];
if(help[j][k]>cur)
{
help[j][k]=cur;
opt[j][k]=p;
}
}
//cout<<"brute_one "<<j<<" "<<k<<" "<<opt[j][k]<<endl;
}
void make_block(int which)
{
for(int j=0;j<c;j++)
for(int k=0;k<c;k++)
if(j==k)current[j][k]=0;
else current[j][k]=inf;
int start=which*block_size,stop=min(r-1,which*block_size+block_size-1);
for(int i=start;i<=stop;i++)
{
for(int j=0;j<c;j++)
for(int k=0;k<c;k++)
help[j][k]=inf;
for(int j=0;j<c;j++)
pref[j+1]=pref[j]+h[i][j];
if(i==start)
{
for(int k=0;k<c;k++)
for(int p=0;p<c;p++)
help[k][p]=abs(pref[p]-pref[k]);
}
else
{
/*
for(int j=0;j<c;j++)
for(int k=0;k<c;k++)
for(int p=0;p<c;p++)
{
int cur=current[j][p];
cur=cur+abs(pref[p]-pref[k]);
cur+=v[i-1][p];
help[j][k]=min(help[j][k],cur);
}
*/
memset(opt,-1,sizeof(opt));
/*
for(int j=0;j<c;j++)
for(int k=0;k<c;k++)
brute_one(i,j,k);
*/
for(int t=0;t<c;t++)
{
brute_one(i,t,0);
brute_one(i,c-1,t);
}
for(int diff=-c;diff<=c;diff++)
{
for(int j=0;j<c;j++)
{
int k=j+diff;
if(0<=k&&k<c&&opt[j][k]==-1)
{
//cout<<j<<" "<<k<<endl;
assert(opt[j][k-1]!=-1&&opt[j+1][k]!=-1);
help[j][k]=inf;
for(int p=opt[j][k-1];p<=opt[j+1][k];p++)
{
int cur=current[j][p];
cur=cur+abs(pref[p]-pref[k]);
cur+=v[i-1][p];
if(help[j][k]>cur)
{
help[j][k]=cur;
opt[j][k]=p;
}
}
}
}
}
}
for(int j=0;j<c;j++)
for(int k=0;k<c;k++)
current[j][k]=help[j][k];
/*
cout<<start<<" "<<i<<endl;
for(int j=0;j<c;j++)
{
for(int k=0;k<c;k++)cout<<current[j][k]<<" ";
cout<<endl;
}
cout<<"---"<<endl;
*/
}
}
void brute(int node,int j,int k,int av)
{
tree[node][j][k]=inf;
for(int p=0;p<c;p++)
{
int cur=tree[node*2][j][p]+v[av*block_size+block_size-1][p]+tree[node*2+1][p][k];
//cout<<"cur= "<<cur<<endl;
if(tree[node][j][k]>cur)
{
tree[node][j][k]=cur;
opt[j][k]=p;
}
}
//cout<<"brute "<<j<<" "<<k<<" "<<opt[j][k]<<endl;
}
void build(int node,int l,int r,int pos)
{
if(l==r)
{
for(int j=0;j<c;j++)
for(int k=0;k<c;k++)
tree[node][j][k]=current[j][k];
return;
}
int av=(l+r)/2;
if(pos<=av)build(node*2,l,av,pos);
else build(node*2+1,av+1,r,pos);
//opt[i][j-1]<=opt[i][j]<=opt[i+1][j]
/*
for(int j=0;j<c;j++)
for(int k=0;k<c;k++)
{
tree[node][j][k]=inf;
for(int p=0;p<c;p++)
tree[node][j][k]=min(tree[node][j][k],tree[node*2][j][p]+v[av*block_size+block_size-1][p]+tree[node*2+1][p][k]);
}
*/
memset(opt,-1,sizeof(opt));
for(int i=0;i<c;i++)
{
brute(node,i,0,av);
brute(node,c-1,i,av);
}
for(int diff=-c;diff<=c;diff++)
{
for(int j=0;j<c;j++)
{
int k=j+diff;
if(0<=k&&k<c&&opt[j][k]==-1)
{
//cout<<j<<" "<<k<<endl;
assert(opt[j][k-1]!=-1&&opt[j+1][k]!=-1);
tree[node][j][k]=inf;
for(int p=opt[j][k-1];p<=opt[j+1][k];p++)
{
int cur=tree[node*2][j][p]+v[av*block_size+block_size-1][p]+tree[node*2+1][p][k];
if(tree[node][j][k]>cur)
{
tree[node][j][k]=cur;
opt[j][k]=p;
}
}
}
}
}
}
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];
for(int i=0;i*block_size<r;i++)
{
make_block(i);
build(1,0,(r-1)/block_size,i);
}
}
void changeH(int P,int Q,int W)
{
h[P][Q]=W;
make_block(P/block_size);
build(1,0,(r-1)/block_size,P/block_size);
}
void changeV(int P,int Q,int W)
{
v[P][Q]=W;
make_block(P/block_size);
build(1,0,(r-1)/block_size,P/block_size);
}
int escape(int V1,int V2)
{
return tree[1][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 |
120 ms |
12740 KB |
Output is correct |
2 |
Correct |
126 ms |
12720 KB |
Output is correct |
3 |
Correct |
195 ms |
15476 KB |
Output is correct |
4 |
Correct |
111 ms |
12740 KB |
Output is correct |
5 |
Correct |
111 ms |
12752 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
428 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
0 ms |
588 KB |
Output is correct |
5 |
Correct |
2 ms |
556 KB |
Output is correct |
6 |
Correct |
1 ms |
588 KB |
Output is correct |
7 |
Correct |
2 ms |
588 KB |
Output is correct |
8 |
Correct |
2 ms |
588 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
588 KB |
Output is correct |
11 |
Correct |
81 ms |
2968 KB |
Output is correct |
12 |
Correct |
2 ms |
588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
276 ms |
2636 KB |
Output is correct |
2 |
Correct |
206 ms |
2544 KB |
Output is correct |
3 |
Correct |
271 ms |
2632 KB |
Output is correct |
4 |
Correct |
272 ms |
2636 KB |
Output is correct |
5 |
Correct |
313 ms |
2652 KB |
Output is correct |
6 |
Correct |
1 ms |
428 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
428 KB |
Output is correct |
9 |
Correct |
918 ms |
2628 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
21420 KB |
Output is correct |
2 |
Correct |
137 ms |
21412 KB |
Output is correct |
3 |
Correct |
119 ms |
21432 KB |
Output is correct |
4 |
Correct |
162 ms |
22796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
236 ms |
2636 KB |
Output is correct |
2 |
Correct |
196 ms |
2604 KB |
Output is correct |
3 |
Correct |
264 ms |
2612 KB |
Output is correct |
4 |
Correct |
258 ms |
2636 KB |
Output is correct |
5 |
Correct |
317 ms |
2616 KB |
Output is correct |
6 |
Correct |
116 ms |
21460 KB |
Output is correct |
7 |
Correct |
127 ms |
21408 KB |
Output is correct |
8 |
Correct |
136 ms |
21480 KB |
Output is correct |
9 |
Correct |
165 ms |
22820 KB |
Output is correct |
10 |
Correct |
120 ms |
12736 KB |
Output is correct |
11 |
Correct |
118 ms |
12740 KB |
Output is correct |
12 |
Correct |
193 ms |
15532 KB |
Output is correct |
13 |
Correct |
121 ms |
12700 KB |
Output is correct |
14 |
Correct |
125 ms |
12748 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
1 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
588 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
2 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
588 KB |
Output is correct |
25 |
Correct |
84 ms |
2932 KB |
Output is correct |
26 |
Correct |
2 ms |
588 KB |
Output is correct |
27 |
Correct |
939 ms |
2632 KB |
Output is correct |
28 |
Correct |
2926 ms |
104564 KB |
Output is correct |
29 |
Correct |
2887 ms |
86356 KB |
Output is correct |
30 |
Correct |
3104 ms |
86192 KB |
Output is correct |
31 |
Correct |
3102 ms |
106184 KB |
Output is correct |
32 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
237 ms |
2632 KB |
Output is correct |
2 |
Correct |
201 ms |
2604 KB |
Output is correct |
3 |
Correct |
248 ms |
2632 KB |
Output is correct |
4 |
Correct |
248 ms |
2636 KB |
Output is correct |
5 |
Correct |
250 ms |
2536 KB |
Output is correct |
6 |
Correct |
117 ms |
21440 KB |
Output is correct |
7 |
Correct |
118 ms |
21492 KB |
Output is correct |
8 |
Correct |
117 ms |
21432 KB |
Output is correct |
9 |
Correct |
158 ms |
22868 KB |
Output is correct |
10 |
Correct |
113 ms |
12668 KB |
Output is correct |
11 |
Correct |
113 ms |
12744 KB |
Output is correct |
12 |
Correct |
187 ms |
15428 KB |
Output is correct |
13 |
Correct |
111 ms |
12812 KB |
Output is correct |
14 |
Correct |
113 ms |
12716 KB |
Output is correct |
15 |
Correct |
5339 ms |
190884 KB |
Output is correct |
16 |
Correct |
11990 ms |
191860 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
588 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
2 ms |
588 KB |
Output is correct |
25 |
Correct |
1 ms |
588 KB |
Output is correct |
26 |
Correct |
1 ms |
588 KB |
Output is correct |
27 |
Correct |
82 ms |
2912 KB |
Output is correct |
28 |
Correct |
1 ms |
588 KB |
Output is correct |
29 |
Correct |
919 ms |
2692 KB |
Output is correct |
30 |
Correct |
2745 ms |
104504 KB |
Output is correct |
31 |
Correct |
10019 ms |
189312 KB |
Output is correct |
32 |
Correct |
10912 ms |
189316 KB |
Output is correct |
33 |
Correct |
3070 ms |
86272 KB |
Output is correct |
34 |
Correct |
11436 ms |
156420 KB |
Output is correct |
35 |
Correct |
2997 ms |
86260 KB |
Output is correct |
36 |
Correct |
10722 ms |
156220 KB |
Output is correct |
37 |
Correct |
2829 ms |
106156 KB |
Output is correct |
38 |
Correct |
10232 ms |
189928 KB |
Output is correct |
39 |
Correct |
1 ms |
460 KB |
Output is correct |
40 |
Correct |
10909 ms |
156436 KB |
Output is correct |