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 <algorithm>
#define INF 0x7fffffff
using namespace std;
static int R, C, N, M, H[5000][200], V[5000][200], D[5000][200][2], A[300], B[300];
static int Idx[1024][200][200], Left[1024], Right[1024], NodeNum[1024], Cnt, TerminalCnt;
void TerminalReset(int NodeNum) {
int i, j, k;
for(k=0 ; k<C ; k++) {
for(i=Left[NodeNum] ; i<=Right[NodeNum] ; i++)
for(j=0 ; j<C ; j++)
D[i][j][0]=D[i][j][1]=INF;
D[Left[NodeNum]][k][0]=D[Left[NodeNum]][k][1]=0;
for(i=k-1 ; i>=0 ; i--)
D[Left[NodeNum]][i][1]=D[Left[NodeNum]][i+1][1]+H[Left[NodeNum]][i];
for(i=k+1 ; i<C ; i++)
D[Left[NodeNum]][i][0]=D[Left[NodeNum]][i-1][0]+H[Left[NodeNum]][i-1];
for(i=Left[NodeNum]+1 ; i<=Right[NodeNum] ; i++) {
for(j=0 ; j<C ; j++)
D[i][j][0]=D[i][j][1]=min(D[i-1][j][0],D[i-1][j][1])+V[i-1][j];
for(j=1 ; j<C ; j++)
D[i][j][0]=min(D[i][j][0],D[i][j-1][0]+H[i][j-1]);
for(j=C-2 ; j>=0 ; j--)
D[i][j][1]=min(D[i][j][1],D[i][j+1][1]+H[i][j]);
}
for(i=0 ; i<C ; i++)
Idx[NodeNum][k][i]=min(D[Right[NodeNum]][i][0],D[Right[NodeNum]][i][1]);
}
}
void InnerReset(int NodeNum) {
int i, j, k, X, Y, Min, Temp;
if(Left[2*NodeNum]==-1 && Right[2*NodeNum+1]==-1)
return;
if(Right[2*NodeNum+1]==-1) {
for(i=0 ; i<C ; i++)
for(j=0 ; j<C ; j++)
Idx[NodeNum][i][j]=Idx[2*NodeNum][i][j];
}
else {
B[0]=0;
B[1]=C-1;
for(i=0 ; i<C ; i++) {
for(j=0 ; j<i+2 ; j++)
A[j]=B[j];
X=0;
Y=C-i-1;
B[0]=0;
for(j=1 ; j<i+2 ; j++) {
Min=INF;
for(k=A[j-1] ; k<=A[j] ; k++)
if(Min>Idx[2*NodeNum][X][k]+V[Right[2*NodeNum]][k]+Idx[2*NodeNum+1][k][Y]) {
Min=Idx[2*NodeNum][X][k]+V[Right[2*NodeNum]][k]+Idx[2*NodeNum+1][k][Y];
Temp=k;
}
Idx[NodeNum][X++][Y++]=Min;
B[j]=Temp;
}
B[i+2]=C-1;
}
}
}
void NodeNumbering(int Now) {
if(Now<M) {
NodeNumbering(2*Now);
NodeNum[Now]=++Cnt;
if(TerminalCnt<N)
NodeNumbering(2*Now+1);
}
else {
TerminalCnt++;
NodeNum[Now]=++Cnt;
}
}
void init(int R_, int C_, int H_[5000][200], int V_[5000][200]) {
int i, j;
R=R_;
C=C_;
N=(R+9)/10;
for(M=1 ; M<N ; M*=2);
NodeNumbering(1);
for(i=M ; i<M+N ; i++) {
Left[i]=10*(i-M);
Right[i]=Left[i]+9;
}
Right[M+N-1]=R-1;
for(i=M+N ; i<2*M ; i++)
Left[i]=Right[i]=-1;
for(i=M-1 ; i>=1 ;i--) {
if(Left[2*i]==-1 && Right[2*i+1]==-1)
Left[i]=Right[i]=-1;
else if(Right[2*i+1]==-1) {
Left[i]=Left[2*i];
Right[i]=Right[2*i];
}
else {
Left[i]=Left[2*i];
Right[i]=Right[2*i+1];
}
}
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];
for(i=M ; i<M+N ; i++)
TerminalReset(i);
for(i=M-1 ; i>=1 ; i--)
InnerReset(i);
}
void changeH(int P, int Q, int W) {
int Num=M+P/10;
H[P][Q]=W;
TerminalReset(Num);
Num/=2;
while(Num) {
InnerReset(Num);
Num/=2;
}
}
int CommonAnc(int X, int Y) {
int Now=1;
while(!(NodeNum[X]<=NodeNum[Now] && NodeNum[Now]<=NodeNum[Y])) {
if(NodeNum[Y]<NodeNum[Now])
Now=2*Now;
else
Now=2*Now+1;
}
return Now;
}
void changeV(int P, int Q, int W) {
int Num=M+P/10;
V[P][Q]=W;
if(P%10==9) {
Num=CommonAnc(Num,Num+1);
while(Num) {
InnerReset(Num);
Num/=2;
}
}
else {
TerminalReset(Num);
Num/=2;
while(Num) {
InnerReset(Num);
Num/=2;
}
}
}
int escape(int V1, int V2) {
return Idx[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... |