#pragma GCC optimize ("Ofast") #include using namespace std; void *wmem; template inline void walloc1d(T **arr, int x, void **mem = &wmem){ static int skip[16]={0, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; (*mem) = (void*)( ((char*)(*mem)) + skip[((unsigned long long)(*mem)) & 15] ); (*arr)=(T*)(*mem); (*mem)=((*arr)+x); } inline void rd(int &x){ int k, m=0; x=0; for(;;){ k = getchar_unlocked(); if(k=='-'){ m=1; break; } if('0'<=k&&k<='9'){ x=k-'0'; break; } } for(;;){ k = getchar_unlocked(); if(k<'0'||k>'9'){ break; } x=x*10+k-'0'; } if(m){ x=-x; } } inline void wt_L(char a){ putchar_unlocked(a); } inline void wt_L(int x){ char f[10]; int m=0, s=0; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ putchar_unlocked('-'); } while(s--){ putchar_unlocked(f[s]+'0'); } } inline void wt_L(long long x){ char f[20]; int m=0, s=0; if(x<0){ m=1; x=-x; } while(x){ f[s++]=x%10; x/=10; } if(!s){ f[s++]=0; } if(m){ putchar_unlocked('-'); } while(s--){ putchar_unlocked(f[s]+'0'); } } template struct DijkstraHeap{ T *val; char *visited; int *hp, *place, size; void malloc(int N){ hp = (int*)std::malloc(N*sizeof(int)); place = (int*)std::malloc(N*sizeof(int)); visited = (char*)std::malloc(N*sizeof(char)); val = (T*)std::malloc(N*sizeof(T)); } void free(){ std::free(hp); std::free(place); std::free(visited); std::free(val); } void walloc(int N, void **mem=&wmem){ walloc1d(&hp, N, mem); walloc1d(&place, N, mem); walloc1d(&visited, N, mem); walloc1d(&val, N, mem); } void init(int N){ int i; size = 0; for(i=0;i<(N);i++){ place[i]=-1; } for(i=0;i<(N);i++){ visited[i]=0; } } void up(int n){ int m; while(n){ m=(n-1)/2; if(val[hp[m]]<=val[hp[n]]){ break; } swap(hp[m],hp[n]); swap(place[hp[m]],place[hp[n]]); n=m; } } void down(int n){ int m; for(;;){ m=2*n+1; if(m>=size){ break; } if(m+1val[hp[m+1]]){ m++; } if(val[hp[m]]>=val[hp[n]]){ break; } swap(hp[m],hp[n]); swap(place[hp[m]],place[hp[n]]); n=m; } } void change(int n, T v){ if(visited[n]||(place[n]>=0&&val[n]<=v)){ return; } val[n]=v; if(place[n]==-1){ place[n]=size; hp[size++]=n; up(place[n]); } else{ up(place[n]); } } int pop(void){ int res=hp[0]; place[res]=-1; size--; if(size){ hp[0]=hp[size]; place[hp[0]]=0; down(0); } visited[res]=1; return res; } } ; char memarr[96000000]; #define L 150 int X; int Y; int GX; int GY; int A[250][250]; int Q; int dist[L][250][250]; int main(){ DijkstraHeap hp; int KL2GvlyY, d, di[4]={-1,1,0,0}, dj[4]={0,0,-1,1}, i, j, k, ni, nj, si, sj; long long res; wmem = memarr; rd(X); rd(Y); rd(GX);GX += (-1); rd(GY);GY += (-1); for(i=0;i<(X);i++){ { int Lj4PdHRW; for(Lj4PdHRW=0;Lj4PdHRW<(Y);Lj4PdHRW++){ rd(A[i][Lj4PdHRW]); } } } hp.malloc(X*Y); for(k=0;k<(L);k++){ hp.init(X*Y); hp.change(GX*Y+GY, (k+1) * (k+1) + A[GX][GY]); while(hp.size){ i = hp.pop(); si = i / Y; sj = i % Y; for(d=0;d<(4);d++){ ni = si + di[d]; nj = sj + dj[d]; if(ni < 0 || nj < 0 || ni >= X || nj >= Y){ continue; } hp.change(ni*Y+nj, hp.val[i] + (k+1) * (k+1) + A[ni][nj]); } } for(i=0;i<(X);i++){ for(j=0;j<(Y);j++){ dist[k][i][j] = hp.val[i*Y+j]; } } } rd(Q); for(KL2GvlyY=0;KL2GvlyY<(Q);KL2GvlyY++){ rd(i);i += (-1); rd(j);j += (-1); rd(k); if(k <= L){ wt_L(dist[k-1][i][j]); wt_L('\n'); } else{ res = dist[L-1][i][j] % (L*L); res += (long long)(dist[L-1][i][j] / (L*L)) * k * k; wt_L(res); wt_L('\n'); } } return 0; } // cLay varsion 20190820-1 // --- original code --- // #define L 150 // // int X, Y, GX, GY, A[250][250], Q; // // int dist[L][250][250]; // // { // int i, j, k, d; // int di[4] = {-1,1,0,0}, dj[4] = {0,0,-1,1}; // int si, sj, ni, nj; // DijkstraHeap hp; // ll res; // // rd(X,Y,GX--,GY--); // rep(i,X) rd(A[i](Y)); // // hp.malloc(X*Y); // rep(k,L){ // hp.init(X*Y); // hp.change(GX*Y+GY, (k+1) * (k+1) + A[GX][GY]); // while(hp.size){ // i = hp.pop(); // si = i / Y; // sj = i % Y; // rep(d,4){ // ni = si + di[d]; // nj = sj + dj[d]; // if(ni < 0 || nj < 0 || ni >= X || nj >= Y) continue; // hp.change(ni*Y+nj, hp.val[i] + (k+1) * (k+1) + A[ni][nj]); // } // } // rep(i,X) rep(j,Y) dist[k][i][j] = hp.val[i*Y+j]; // } // // rd(Q); // rep(Q){ // rd(i--, j--, k); // if(k <= L){ // wt(dist[k-1][i][j]); // } else { // res = dist[L-1][i][j] % (L*L); // res += (ll)(dist[L-1][i][j] / (L*L)) * k * k; // wt(res); // } // } // }