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 M 2147483647
using namespace std;
int r,c,h[5010][210],v[5010][210],tree[1026][210][210],d[11][210][210],arr[210],D[11][210][210],imsi[210],t,ch[2010],arr2[210],imsi2[210];
void Merge(int x)
{
int i,j,k,l,w=0; x/=2;
while(x){
ch[x]=1, arr[1]=1, arr[2]=c;
if(ch[x*2+1]==0){
for(j=1;j<=c;j++) for(k=1;k<=c;k++) tree[x][j][k]=tree[x*2][j][k];
x/=2; continue;
}
for(j=c;j>=1;j--){
for(k=1;k<=c-j+1;k++){
tree[x][k][k+j-1]=M;
for(l=arr[k];l<=arr[k+1];l++){
if(tree[x][k][k+j-1]>tree[x*2][k][l]+tree[x*2+1][l][k+j-1]) tree[x][k][k+j-1]=tree[x*2][k][l]+tree[x*2+1][l][k+j-1], imsi[k]=l;
}
}
for(k=2;k<=c-j+2;k++) arr[k]=imsi[k-1];
arr[1]=1; arr[c-j+3]=c;
}
arr[1]=1, arr[2]=c;
for(j=c;j>=1;j--){
for(k=1;k<=c-j+1;k++){
tree[x][k+j-1][k]=M;
for(l=arr[k];l<=arr[k+1];l++){
if(tree[x][k+j-1][k]>tree[x*2][k+j-1][l]+tree[x*2+1][l][k]) tree[x][k+j-1][k]=tree[x*2][k+j-1][l]+tree[x*2+1][l][k], imsi[k]=l;
}
}
for(k=2;k<=c-j+2;k++) arr[k]=imsi[k-1];
arr[1]=1; arr[c-j+3]=c;
}
x/=2;
}
}
void update(int x)
{
int p,i,j,k,l,w=0,s1,s2;
for(i=(x-1)*10+1;i<=min(r-1,(x-1)*10+10);i++){
w++; arr[1]=arr2[1]=1, arr[2]=arr2[2]=c;
for(j=c;j>=1;j--){
for(k=1;k<=c-j+1;k++){
d[w][k][k+j-1]=M; d[w][k+j-1][k]=M;
for(l=arr[k];l<=arr[k+1];l++){
if(l>=k) s1=1; else s1=-1;
if(l<=k+j-1) s2=1; else s2=-1;
p=v[i][l]+s2*(h[i+1][k+j-2]-h[i+1][l-1])+s1*(h[i][l-1]-h[i][k-1]);
if(d[w][k][k+j-1]>p) d[w][k][k+j-1]=p, imsi[k]=l;
}
for(l=arr2[k];l<=arr2[k+1];l++){
if(l>=k) s1=1; else s1=-1;
if(l<=k+j-1) s2=1; else s2=-1;
p=v[i][l]+s2*(h[i][k+j-2]-h[i][l-1])+s1*(h[i+1][l-1]-h[i+1][k-1]);
if(d[w][k+j-1][k]>p) d[w][k+j-1][k]=p, imsi2[k]=l;
}
}
for(k=2;k<=c-j+2;k++) arr[k]=imsi[k-1];
for(k=2;k<=c-j+2;k++) arr2[k]=imsi2[k-1];
arr[1]=1; arr[c-j+3]=c; arr2[1]=1; arr2[c-j+3]=c;
}
}
for(i=1;i<=c;i++) for(j=1;j<=c;j++) D[1][i][j]=d[1][i][j];
for(i=2;i<=w;i++){
arr[2]=c;
for(j=c;j>=1;j--){
for(k=1;k<=c-j+1;k++){
D[i][k][k+j-1]=M;
for(l=arr[k];l<=arr[k+1];l++){
p=D[i-1][k][l]+d[i][l][k+j-1];
if(D[i][k][k+j-1]>p) D[i][k][k+j-1]=p, imsi[k]=l;
}
}
for(k=2;k<=c-j+2;k++) arr[k]=imsi[k-1];
arr[1]=1; arr[c-j+3]=c;
}
arr[2]=c;
for(j=c;j>=1;j--){
for(k=1;k<=c-j+1;k++){
D[i][k+j-1][k]=M;
for(l=arr[k];l<=arr[k+1];l++){
p=D[i-1][k+j-1][l]+d[i][l][k];
if(D[i][k+j-1][k]>p) D[i][k+j-1][k]=p, imsi[k]=l;
}
}
for(k=2;k<=c-j+2;k++) arr[k]=imsi[k-1];
arr[1]=1; arr[c-j+3]=c;
}
}
for(i=1;i<=c;i++) for(j=1;j<=c;j++) tree[x+t-1][i][j]=D[w][i][j];
ch[x+t-1]=1;
Merge(x+t-1);
}
void init(int R, int C, int H[5000][200], int V[5000][200]) {
r=R; c=C;
int i,j;
for(i=1;i<=r;i++){
for(j=1;j<=c-1;j++){
h[i][j]=h[i][j-1]+H[i-1][j-1];
}
}
for(i=1;i<=r-1;i++){
for(j=1;j<=c;j++){
v[i][j]=V[i-1][j-1];
}
}
for(t=1;t<(r-2)/10+1;t*=2);
for(i=1;i<=(r-2)/10+1;i++) update(i);
}
void changeH(int P, int Q, int W) {
int i,j,s; P++; Q++;
s=W-(h[P][Q]-h[P][Q-1]);
for(i=Q;i<=c-1;i++) h[P][i]+=s;
if(P==r) P--;
update((P-1)/10+1);
if((P-1)/10 && P%10==1) update((P-1)/10);
}
void changeV(int P, int Q, int W) {
v[P+1][Q+1]=W;
update(P/10+1);
}
int escape(int V1, int V2) {
return tree[1][V1+1][V2+1];
}
# | 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... |