Language: C++

#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <math.h>
#include <algorithm> // sort

#define REP(i, n) for (long long i = 0; i < (n); i++) 
typedef long long ll;
static const ll INF = 1000000000000000000LL;
using namespace std;

const int MOD = 1000000007;

bool customsort(const pair<int, pair<int,int>> &a, const pair<int, pair<int,int>> &b)
{
    return a.first < b.first;
}

bool uppersort(const pair<int, pair<int,int>> &a, const pair<int, pair<int,int>> &b)
{
    return a.first > b.first;
}


int main(){

    int H,W,D;
    cin >> H >> W >> D;
    vector <vector <int> > a(H,vector<int>(W));
    vector <pair<int, pair<int,int>>> jun(H*W);
    int cnt = 0;
    REP(i,H)
    {
        REP(j,W)
        {
            cin >> a[i][j];
            jun[cnt].first = a[i][j];
            jun[cnt].second.first = i;
            jun[cnt].second.second = j;
            ++cnt;
        }
    }
    //for(auto t:jun)
    //{
    //    cout << t.first << endl;
    //}

    //マップを値の大きい順にソート
    sort(jun.begin(),jun.end(),uppersort);
    int max_a = jun[0].first;

    //po[y][x][i]: 値iに到達するまでにかかる魔力
    vector <vector< vector<int>>> po(H,vector<vector<int>>(W, vector<int>(max_a+1, MOD)));
    
    for(int i = 0; i < H * W; ++i)
    {
        int y = jun[i].second.first;
        int x = jun[i].second.second;
        int p = jun[i].first;
    //現在値は、設定
        po[y][x][p] = 0;
        //1回でおわらす
        if(p + D <= max_a)
        {
            for(int j = 0; j < i; ++j)
            {
                int t_p = jun[j].first;
                if(t_p != p + D)continue;
                int t_y = jun[j].second.first;
                int t_x = jun[j].second.second;
                //魔力
                int ma = abs(t_y - y) + abs(t_x - x);

                while(p + D <= max_a)
                {
                    p += D;
                    //魔力|x−i|+|y−j|を消費することで
                    //マス(i,j)に置かれた駒をマス(x,y)に瞬間移動させる
                    po[y][x][p] = po[t_y][t_x][p] + ma;
                }
            }
        }
    }

    //小さい順ソート
    sort(jun.begin(),jun.end(),customsort);

    vector <vector <int> > freeza(max_a, vector<int>(max_a,MOD));

    for(auto t:jun)
    {
        //値
        int l = t.first;
        int y = t.second.first;
        int x = t.second.second;

        for(int i = pos; i <= max_a; ++i)
        {
            freeza[l][i] = min(freeza[l][i], pos[y][x][i]);
        }
    }
    

    int Q;
    cin >> Q;
    REP(i,Q)
    {
        int l,r;
        int res = MOD;
        cin >> l >> r;
        //lの位置のrを拾ってくる
        //pair<int,pair<int,int>> tmp_t(l,{0,0});
        //auto pos = lower_bound(jun.begin(),jun.end(),tmp_t) - jun.begin();

        //cout << pos << endl;
        //cout << jun[pos].first << endl;

        //while(pos < H * W && jun[pos].first == l)
        //{
        //    int y = jun[pos].second.first;
        //    int x = jun[pos].second.second;
//
        //    res = min(res, po[y][x][r]);
        //    ++pos;
        //}
        res = freeza[l][r];

        cout << res << endl;
    }


    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:98:21: error: 'pos' was not declared in this scope; did you mean 'po'?
   98 |         for(int i = pos; i <= max_a; ++i)
      |                     ^~~
      |                     po