Language: C++

#include <iostream>
#include <vector>
#include<list>
#include <algorithm>
#include <string>
#include <math.h>
#include<map>
#include <utility>

#include<queue>
using namespace std;
#define REP(i,k,n) for(int i=k;i<n;i++)
 
#define INF (1e9)
typedef long long unsigned int ll;
#define SIZE_OF_ARRAY(array) (sizeof(array)/sizeof(array[0]))
#define MOD 1000000007

template<typename T>
int vector_finder(std::vector<T> vec, T number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

template<typename T>
T gcd(T a,T b){
	if(b>0){
		return gcd(b, a % b);
	}else{
		return a;
	}
}

template<typename T>
T lcm(T a,T b){
	T g = gcd(a, b);
	return a * b / g;
}

void clear( std::queue<pair<int,int> > &q )
{
   std::queue<pair<int,int> > empty;
   std::swap( q, empty );
}
bool IsPrime(int num)
{
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false; // 偶数はあらかじめ除く

    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            // 素数ではない
            return false;
        }
    }

    // 素数である
    return true;
}


int H,W;



int main()
{
	cin>>H>>W;
  int D,Q;
  cin>>D;
  map<int,pair<int,int> >A;

  REP(i,0,H){
    REP(j,0,W){
      int a;
      cin>>a;
      A[a]=make_pair(i,j);
    }
  }
  cin>>Q;

  int ans[H*W+1];

  REP(i,0,D){
    ans[i]=0;
  }

  REP(i,D,H*W+1){
    auto p=A[i];
    auto pm=A[i-D];
    ans[i]=ans[i-D]+abs(p.first-pm.first)+abs(p.second-pm.second);
  }
  int an[Q];

  REP(i,0,Q){
    int l,r;
    cin>>l>>r;
    an[i]=ans[r]-ans[l];
  }

  REP(i,0,Q){
    cout<<an[i]<<endl;
  }



  return 0;
}#include <iostream>
#include <vector>
#include<list>
#include <algorithm>
#include <string>
#include <math.h>
#include<map>
#include <utility>

#include<queue>
using namespace std;
#define REP(i,k,n) for(int i=k;i<n;i++)
 
#define INF (1e9)
typedef long long unsigned int ll;
#define SIZE_OF_ARRAY(array) (sizeof(array)/sizeof(array[0]))
#define MOD 1000000007

template<typename T>
int vector_finder(std::vector<T> vec, T number) {
  auto itr = std::find(vec.begin(), vec.end(), number);
  size_t index = std::distance( vec.begin(), itr );
  if (index != vec.size()) { // 発見できたとき
    return 1;
  }
  else { // 発見できなかったとき
    return 0;
  }
}

template<typename T>
T gcd(T a,T b){
	if(b>0){
		return gcd(b, a % b);
	}else{
		return a;
	}
}

template<typename T>
T lcm(T a,T b){
	T g = gcd(a, b);
	return a * b / g;
}

void clear( std::queue<pair<int,int> > &q )
{
   std::queue<pair<int,int> > empty;
   std::swap( q, empty );
}
bool IsPrime(int num)
{
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false; // 偶数はあらかじめ除く

    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            // 素数ではない
            return false;
        }
    }

    // 素数である
    return true;
}


int H,W;



int main()
{
	cin>>H>>W;
  int D,Q;
  cin>>D;
  map<int,pair<int,int> >A;

  REP(i,0,H){
    REP(j,0,W){
      int a;
      cin>>a;
      A[a]=make_pair(i,j);
    }
  }
  cin>>Q;

  int ans[H*W+1];

  REP(i,0,D){
    ans[i]=0;
  }

  REP(i,D,H*W+1){
    auto p=A[i];
    auto pm=A[i-D];
    ans[i]=ans[i-D]+abs(p.first-pm.first)+abs(p.second-pm.second);
  }
  int an[Q];

  REP(i,0,Q){
    int l,r;
    cin>>l>>r;
    an[i]=ans[r]-ans[l];
  }

  REP(i,0,Q){
    cout<<an[i]<<endl;
  }



  return 0;
}

Compiler output:
a.cc:118:2: error: stray '#' in program
  118 | }#include <iostream>
      |  ^
a.cc:118:3: error: 'include' does not name a type
  118 | }#include <iostream>
      |   ^~~~~~~
a.cc:137:5: error: redefinition of 'template<class T> int vector_finder(std::vector<_Tp>, T)'
  137 | int vector_finder(std::vector<T> vec, T number) {
      |     ^~~~~~~~~~~~~
a.cc:20:5: note: 'template<class T> int vector_finder(std::vector<_Tp>, T)' previously declared here
   20 | int vector_finder(std::vector<T> vec, T number) {
      |     ^~~~~~~~~~~~~
a.cc:149:3: error: redefinition of 'template<class T> T gcd(T, T)'
  149 | T gcd(T a,T b){
      |   ^~~
a.cc:32:3: note: 'template<class T> T gcd(T, T)' previously declared here
   32 | T gcd(T a,T b){
      |   ^~~
a.cc:158:3: error: redefinition of 'template<class T> T lcm(T, T)'
  158 | T lcm(T a,T b){
      |   ^~~
a.cc:41:3: note: 'template<class T> T lcm(T, T)' previously declared here
   41 | T lcm(T a,T b){
      |   ^~~
a.cc:163:6: error: redefinition of 'void clear(std::queue<std::pair<int, int> >&)'
  163 | void clear( std::queue<pair<int,int> > &q )
      |      ^~~~~
a.cc:46:6: note: 'void clear(std::queue<std::pair<int, int> >&)' previously defined here
   46 | void clear( std::queue<pair<int,int> > &q )
      |      ^~~~~
a.cc:168:6: error: redefinition of 'bool IsPrime(int)'
  168 | bool IsPrime(int num)
      |      ^~~~~~~
a.cc:51:6: note: 'bool IsPrime(int)' previously defined here
   51 | bool IsPrime(int num)
      |      ^~~~~~~
a.cc:189:5: error: redefinition of 'int H'
  189 | int H,W;
      |     ^
a.cc:72:5: note: 'int H' previously declared here
   72 | int H,W;
      |     ^
a.cc:189:7: error: redefinition of 'int W'
  189 | int H,W;
      |       ^
a.cc:72:7: note: 'int W' previously declared here
   72 | int H,W;
      |       ^
a.cc:193:5: error: redefinition of 'int main()'
  193 | int main()
      |     ^~~~
a.cc:76:5: note: 'int main()' previously defined here
   76 | int main()
      |     ^~~~