Language: C++

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <queue>
using namespace std;
typedef long long LL;
const int inf = 1 << 30;
const LL INF = 1LL << 60;
const int MaxN = 100000;
const double eps = 1e-6;
const double PI = acos(-1.0);

int H, W, D;
int cnt, Q;
struct Point {
	int x, y, id;
	Point () {}
	Point (int _x, int _y, int _id) {
		x = _x; y = _y; id = _id;
	}
	friend bool operator < (const Point A, const Point B) {
		return A.id < B.id;
	}
}PP[MaxN + 5];

int prev[MaxN + 5];

int dis(Point A, Point B) {
	return abs(A.x - B.x) + abs(A.y - B.y);
}

int main()
{
	while(scanf("%d %d %d", &H, &W, &D) != EOF)
	{
		cnt = 0;
		for(int i = 1; i <= H; i++) {
			for(int j = 1; j <= W; j++) {
				int t;
				scanf("%d", &t);
				PP[++cnt] = Point(i, j, t);
			}
		}
		sort(PP + 1, PP + cnt + 1);
		scanf("%d", &Q);
		prev[0] = 0;
		for(int i = 1; i < cnt; i++)
			prev[i] = prev[i - 1] + dis(PP[i], PP[i + 1]);
		for(int cas = 1; cas <= Q; cas++) {
			int L, R;
			scanf("%d %d", &L, &R);
			if(D == 1) {
				printf("%d\n", max(0, prev[R - 1] - prev[L - 1]));
			}
			else {
				int cost = 0;
				int S = L, T = R;
				while(S != T) {
					cost += dis(PP[S + D], PP[S]);
					S += D;
				}
				printf("%d\n", cost);
			}
		}
	}
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:49:17: error: reference to 'prev' is ambiguous
   49 |                 prev[0] = 0;
      |                 ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/bits/specfun.h:43,
                 from /usr/include/c++/14/cmath:3906,
                 from a.cc:3:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:29:5: note:                 'int prev [100005]'
   29 | int prev[MaxN + 5];
      |     ^~~~
a.cc:51:25: error: reference to 'prev' is ambiguous
   51 |                         prev[i] = prev[i - 1] + dis(PP[i], PP[i + 1]);
      |                         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:29:5: note:                 'int prev [100005]'
   29 | int prev[MaxN + 5];
      |     ^~~~
a.cc:51:35: error: reference to 'prev' is ambiguous
   51 |                         prev[i] = prev[i - 1] + dis(PP[i], PP[i + 1]);
      |                                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:29:5: note:                 'int prev [100005]'
   29 | int prev[MaxN + 5];
      |     ^~~~
a.cc:56:55: error: reference to 'prev' is ambiguous
   56 |                                 printf("%d\n", max(0, prev[R - 1] - prev[L - 1]));
      |                                                       ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:29:5: note:                 'int prev [100005]'
   29 | int prev[MaxN + 5];
      |     ^~~~
a.cc:56:69: error: reference to 'prev' is ambiguous
   56 |                                 printf("%d\n", max(0, prev[R - 1] - prev[L - 1]));
      |                                                                     ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:244:5: note: candidates are: 'template<class _BidirectionalIterator> constexpr _BidirectionalIterator std::prev(_BidirectionalIterator, typename iterator_traits<_Iter>::difference_type)'
  244 |     prev(_BidirectionalIterator __x, typename
      |     ^~~~
a.cc:29:5: note:                 'int prev [100005]'
   29 | int prev[MaxN + 5];
      |     ^~~~