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<stdlib.h>
#include<queue>
#include<vector>
#include<functional>
#include<string.h>
using namespace std;
#define X first
#define Y second
typedef pair<int,int> P;
typedef pair<int,P> Pi;
const int id = 512;
const int inf = 1e9;
const int bz = 10;
int H[5000][200],V[5000][200];
int T[1030][200][200];
int tmp[bz+1][200];
int R,C;
void upd(int x,int y)
{
int i,j;
memset(tmp[0],0,sizeof(tmp[0]));
for(i=x-1;i>=0;i--)tmp[0][i]=tmp[0][i+1]+H[bz*y][i];
for(i=x+1;i<C;i++)tmp[0][i] =tmp[0][i-1]+H[bz*y][i-1];
for(i=1;i<=bz && bz*y+i<R;i++){
for(j=0;j<C;j++)tmp[i][j]=tmp[i-1][j]+V[bz*y+i-1][j];
for(j=C-2;j>=0;j--)tmp[i][j]=min(tmp[i][j],tmp[i][j+1]+H[bz*y+i][j]);
for(j=1;j<C;j++)tmp[i][j]=min(tmp[i][j],tmp[i][j-1]+H[bz*y+i][j-1]);
}
for(j=0;j<C;j++)T[id+y][x][j]=tmp[i-1][j];
}
void Union(int t)
{
int i,j,k;
int sk[200];
for(i=0;i<C;i++)sk[i]=0;
for(i=0;i<C;i++)for(j=0;j<C;j++)T[t][i][j]=inf;
for(i=C-1;i>=-C+1;i--){
for(j=min(C-1,C-i-1);j>=0&&j+i>=0;j--){
int s=j==max(0,-i)?0:sk[j-1];
int d=j==min(C-1,C-i-1)?C-1:sk[j];
for(k=s;k<=d;k++){
if(T[t][j][j+i] > T[t<<1][j][k] + T[t<<1|1][k][j+i]){
T[t][j][j+i]=T[t<<1][j][k] + T[t<<1|1][k][j+i];
sk[j]=k;
}
}
}
}
for(i=0;i<C;i++)if(T[t][i][i]==inf)T[t][i][i]=0;
}
void init(int R, int C, int H_[5000][200], int V_[5000][200]){
int i,j;
for(i=0;i<R;i++)for(j=0;j<C-1;j++)H[i][j]=H_[i][j];
for(i=0;i<R-1;i++)for(j=0;j<C;j++)V[i][j]=V_[i][j];
::R=R;
::C=C;
for(i=0;i<C;i++){
for(j=0;bz*j<R;j++){
upd(i,j);
}
for(;j<id;j++){
for(int u=0;u<C;u++)T[id+j][i][u]=i==u?0:inf;
}
}
for(i=id-1;i;i--)Union(i);
}
void changeH(int P, int Q, int W){
H[P][Q]=W;
int i,r=P/bz;
for(i=0;i<C;i++)upd(i,r);
if(P%bz == 0 && P){
for(i=0;i<C;i++)upd(i,r-1);
int t=(r+id-1)/2;
while(t){Union(t);t>>=1;}
}
r+=id;
r>>=1;
while(r){
Union(r);
r>>=1;
}
}
void changeV(int P, int Q, int W) {
V[P][Q]=W;
int i,r=P/bz;
for(i=0;i<C;i++)upd(i,r);
r+=id;
r>>=1;
while(r){
Union(r);
r>>=1;
}
}
int escape(int V1, int V2){
return T[1][V1][V2];
}
# | 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... |